In-order non-recursive algorithm

In-order non-recursive algorithm

First, we initialize a stack and let the root pointer into the stack. Because it is an in-order traversal, we first need to find the leftmost node of the tree, and code mark 1 accomplishes this task. Then the code mark 1 loop stop condition is not satisfied, at this time the pointer p obtained by GetTop(S,p) is empty, because it reaches the leftmost side, p->lchild is empty, so we need to put this empty into the stack The pointer is given to Pop, which is the function of code mark 2.

   下面是关键一步了,我们需要访问当前栈顶结点了。Pop(S,p)删除栈顶结点并赋给p,然后Visit函数代表我们要对这个结点进行的 操作,这是代码标记4的作用。至于代码标记5就很明显了,将右结点进栈,重新进行这个操作,即:先找最左边结点。。。。

   我们再用一个实例来捋一遍:

According to our code, the first loop: first find node B, execute visit, and then C enters the stack. At this time, there are two nodes A and C in the stack. The second loop: then find node C, C Pop out of the stack and execute visit, and then the right node is empty, push into the stack, and the third loop: Then because the top element of the stack is empty, Pop is dropped. At this time, the stack is not empty, and A is left. Pop out and execute visit, D Push the stack, and then the fourth loop: D pops the stack, E pushes the stack, the left node of E is empty, pushes the stack, then pops the stack, E pops the stack again, executes visit, and then the right empty node of E enters the stack, The fifth cycle: the empty node is popped out of the stack and then empty, after the pop is popped off, then the stack is popped out, D is executed, and then F is pushed into the stack, and the sixth cycle: the empty node of F is pushed into the stack, popped off, and F is out Stack, and then execute visit, the empty node of F is pushed into the stack, and the seventh loop: Then because the stack is not empty twice, the loop ends.

Insert picture description here

method one:

//中序遍历
void InOrderWithoutRecursion1(BTNode* root)
{
    
    
    //空树
    if (root == NULL)
        return;
    //树非空
    BTNode* p = root;
    stack<btnode*> s;
    while (!s.empty() || p)
    {
    
    
        //一直遍历到左子树最下边,边遍历边保存根节点到栈中
        while (p)
        {
    
    
            s.push(p);
            p = p->lchild;
        }
        //当p为空时,说明已经到达左子树最下边,这时需要出栈了
        if (!s.empty())
        {
    
    
            p = s.top();
            s.pop();
            cout << setw(4) << p->data;
            //进入右子树,开始新的一轮左子树遍历(这是递归的自我实现)
            p = p->rchild;
        }
    }
}</btnode*>

Method Two:

//中序遍历
void InOrderWithoutRecursion2(BTNode* root)
{
    
    
    //空树
    if (root == NULL)
        return;
    //树非空
    BTNode* p = root;
    stack<btnode*> s;
    while (!s.empty() || p)
    {
    
    
        if (p)
        {
    
    
            s.push(p);
            p = p->lchild;
        }
        else
        {
    
    
            p = s.top();
            s.pop();
            cout << setw(4) << p->data;
            p = p->rchild;
        }
    }
}</btnode*>

Method three:

public void InOrderWithoutRecursion(TreeNode T){
    
      
          
        Stack<TreeNode> stack = new Stack<TreeNode>();  
        TreeNode p;  
        while(T!=null||!stack.empty()){
    
      
              
            while(T!=null){
    
                 //将结点压进栈中  
                stack.push(T);  
                T = T.lchild;  
            }  
              
            if(!stack.empty()){
    
             //将栈中的结点弹出  
                p = stack.peek();    
                stack.pop();  
                System.out.println(p.data);                   
                T = p.rchild;             
            }  
        }  
                  
    }  

Guess you like

Origin blog.csdn.net/a675891/article/details/109297317