📜  在BST中实现前向迭代器

📅  最后修改于: 2021-05-24 21:24:33             🧑  作者: Mango

给定二进制搜索树,任务是使用以下功能在其上实现正向迭代器。

  1. curr():返回指向当前元素的指针。
  2. next():迭代到二进制搜索树中的下一个最小元素。
  3. isEnd():如果没有要遍历的节点,则返回true,否则返回false。

迭代器按排序顺序(递增)遍历BST。我们将使用堆栈数据结构实现迭代器。
初始化:

curr()

时间复杂度: O(1)

下一个()

时间复杂度:平均所有通话为O(1)。在最坏的情况下,单次通话可以为O(h)。

isEnd()

时间复杂度: O(1)
此迭代器实现的最坏情况下,空间复杂度为O(h)。应该注意的是
迭代器指向堆栈的最顶层元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Node of the binary tree
struct node {
    int data;
    node* left;
    node* right;
    node(int data)
    {
        this->data = data;
        left = NULL;
        right = NULL;
    }
};
 
// Iterator for BST
class bstit {
private:
    // Stack to store the nodes
    // of BST
    stack q;
 
public:
    // Constructor for the class
    bstit(node* root)
    {
        // Initializing stack
        node* curr = root;
        while (curr != NULL)
            q.push(curr), curr = curr->left;
    }
 
    // Function to return
    // current element iterator
    // is pointing to
    node* curr()
    {
        return q.top();
    }
 
    // Function to iterate to next
    // element of BST
    void next()
    {
        node* curr = q.top()->right;
        q.pop();
        while (curr != NULL)
            q.push(curr), curr = curr->left;
    }
 
    // Function to check if
    // stack is empty
    bool isEnd()
    {
        return !(q.size());
    }
};
 
// Function to iterator to every element
// using iterator
void iterate(bstit it)
{
    while (!it.isEnd())
        cout << it.curr()->data << " ", it.next();
}
 
// Driver code
int main()
{
    node* root = new node(5);
    root->left = new node(3);
    root->right = new node(7);
    root->left->left = new node(2);
    root->left->right = new node(4);
    root->right->left = new node(6);
    root->right->right = new node(8);
 
    // Iterator to BST
    bstit it(root);
 
    // Function to test iterator
    iterate(it);
 
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
 
// Node of the binary tree
class TreeNode
{
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode(int x)
    {
        val = x;
    }
}
 
// Iterator for BST
class BSTIterator{
 
// Stack to store the nodes
// of BST
Stack s;
 
// Constructor for the class
public BSTIterator(TreeNode root)
{
     
    // Initializing stack
    s = new Stack<>();
    TreeNode curr = root;
     
    while (curr != null)
    {
        s.push(curr);
        curr = curr.left;
    }
}
 
// Function to return
// current element iterator
// is pointing to
TreeNode curr()
{
    return s.peek();
}
 
// Function to iterate to next
// element of BST
public void next()
{
    TreeNode temp = s.peek().right;
    s.pop();
     
    while (temp != null)
    {
        s.push(temp);
        temp = temp.left;
    }
}
 
// Function to check if
// stack is empty
public boolean isEnd()
{
    return !s.isEmpty();
}
 
// Function to iterator to every element
// using iterator
void iterate()
{
    while (isEnd())
    {
        System.out.print(curr().val + " ");
        next();
    }
}
}
 
class BinaryTree{
     
TreeNode root;
 
// Driver code
public static void main(String args[])
{
     
    // Let us construct a tree shown in
    // the above figure
    BinaryTree tree = new BinaryTree();
    tree.root = new TreeNode(5);
    tree.root.left = new TreeNode(3);
    tree.root.right = new TreeNode(7);
    tree.root.left.left = new TreeNode(2);
    tree.root.left.right = new TreeNode(4);
    tree.root.right.left = new TreeNode(6);
    tree.root.right.right = new TreeNode(8);
 
    // Iterator to BST
    BSTIterator it = new BSTIterator(tree.root);
 
    // Function to test iterator
    it.iterate();
}
}
 
// This code is contributed by nobody_cares


输出:
2 3 4 5 6 7 8

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。