Binary Search Tree Iterator

Source: Internet
Author: User

Implement an iterator over a binary search tree (BST). Your iterator is initialized with the root node of a BST.

Calling would return the next smallest number in the next() BST.

Note: next() hasNext()and should run in average O (1) time and uses O (h) memory, where H is the height of the tree.

Problem Solving Ideas:

Implement a class iterator based on binary search tree. Initialize the root node of this tree to root iterator. Call the next () function to return the next smallest value of the tree.

Method:Two fork search tree (binary search trees), (also:Binary search Tree, binary sort tree) it is either an empty tree, or it has the following propertiesTwo-fork Tree: If the left subtree is not empty, the value of all nodes on the left subtree is less than the value of its root node, and if its right subtree is not empty, the value of all nodes on the right subtree is greater than the value of its root node, and its left and right subtrees are respectivelyBinary sort Tree. For example:
from this we can know that the two-fork search tree in the sequential traversal, you can get ascending sequence. So in this question we use the iterative method to achieve the two-fork search tree in the middle sequence traversal, referring to my blog http://blog.csdn.net/sinat_24520925/article/details/45621865, We use stacks to store the nodes that are read every time, and the elements that we read are the same. So all we have to do is read the top element of the stack at a time and we know the next () node of the binary search tree. The code is as follows:
/** * Definition for binary tree * struct TreeNode {*     int val; *     TreeNode *left; *     TreeNode *right; *     Tre Enode (int x): Val (x), left (null), right (NULL) {} *}; */class bstiterator {    stack<treenode*> qu;public:    bstiterator (TreeNode *root) {        Push_in_stack ( root);    }    /** @return Whether we have a next smallest number *    /bool Hasnext () {        return!qu.empty ();            }    /** @return The next smallest number *    /int Next () {        TreeNode *tmpnode = Qu.top ();        Qu.pop ();        Push_in_stack (tmpnode->right);        Return tmpnode->val;    } Private:void Push_in_stack (treenode* node) {    while (node)    {        Qu.push (node);        node=node->left;     }}};/ * * Your Bstiterator'll be a called like this: * bstiterator i = bstiterator (root); * while (I.hasnext ()) cout << i.next (); */

It is important to note that this is not the minimum value of the tree that outputs root as root, but the next minimum value to be output continuously. So in the int next () function, we cannot output the top element of the stack and exit directly to complete the work. Instead, you insert the right subtree of the output element into the stack, and continue to read the minimum value of the subtree that is the root node of the top element of the stack, which is the next smallest value of the tree. This enables the requirement (a constant output of the next minimum value):
while (I.hasnext ()) cout << i.next ();


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Binary Search Tree Iterator

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.