Preorder, inorder, and postorder traversal of a binary tree


Preorder traversal - visit in the order of "root node - left child - right child".

(1) Recursive method
void preOrder1(BinTree *root) //Recursive preorder traversal { if(root!=NULL) { cout<<root->data<<" "; preOrder1(root->lchild); preOrder1(root->rchild);
} }


(2) Non- recursive method
Using the stack, the principle of first-in, last-out. First push the root node to the bottom of the stack, and finally output void preOrder2(BinTree *root) step by step from the leaf node

//non-recursive preorder traversal
{
    stack<BinTree*> s; //Create an empty stack, which stores the The node of the tree
    BinTree *p=root; //Define the pointer used to point to the currently visited node
    while(p!=NULL||!s.empty()) //  Until the current node p is NULL and the stack is empty , loop end  
    {
        while(p!=NULL)
        {
            cout<<p->data<<" ";  //Start from the root node, output the current node  
            s.push(p); //Push the node into the stack
            p=p- >lchild; // Set its left child as the current node until it has no left child and the current node is NULL  
        }
        if(!s.empty()) //If the current node pCur is NULL and the stack is not empty, the stack will be top node pop
        {
            p=s.top(); s.pop(
            ); // pop the stack
            p=p->rchild; //set its right child as the current node at the same time, loop judgment until pCur is not empty  
        }
    } Inorder



traversal - Visit in the order of "left child - root node - right child".
(1) Recursive method
    void preOrder1(BinTree *root) //Recursive in-order traversal
     {
         if(root!=NULL)
        {
preOrder1(root->lchild);           
cout<<root->data<<" ";
            preOrder1(root ->rchild);
        }
     }

(2) Non- recursive method
Using the stack, the principle of first in, last out. First push the root node to the bottom of the stack, and finally output from the leaf node step by step, the difference is the output position

void preOrder2(BinTree *root) //Non-recursive preorder traversal
{
    stack<BinTree*> s;            //Create an empty stack, which stores the node of the tree
    BinTree *p=root; //Define a pointer to the currently visited node
    while(p!=NULL||!s.empty()) //  Until the current node p is NULL and the stack is empty, the loop ends  
    {
        while(p!=NULL)
        {
          
            s.push(p); // push the node to the stack
            p=p->lchild; // set its left child to The current node, until it has no left child, and the current node is NULL  
        }
        if(!s.empty()) //If the current node pCur is NULL and the stack is not empty, pop the top node from the stack
        {
            p=s.top ();
cout<<p->data<<" "; 
            s.pop(); // output the current node 
            p=p->rchild;      //set its right child as the current node at the same time, loop judgment until pCur does not Empty  
        }
    }




Post-order traversal - visit in the order of "left child - right child - root node".

(1) Recursive method
    void preOrder1(BinTree *root) //Recursive in-order traversal

{
if(root!=NULL)
{
preOrder1(root->left);
  preOrder1(root->right);
cout<<root->data<<" ";
}

return 0;
}


(2) Non-recursive method
Use the stack, first in last out principle. First push the root node to the bottom of the stack, and finally output from the leaf node step by step, the difference is the output position

void postOrder3(BinTree *root) //Non-recursive post-order traversal
{
     stack<BinTree*> s;
    BinTree *cur; / /The current node
     BinTree *pre=NULL; //The last visited node
    s.push(root);
     while(!s.empty())
    {
         cur=s.top();
         if((cur->lchild==NULL&&cur->rchild==NULL)||
            (pre!=NULL&&(pre==cur->lchild||pre==cur->rchild)))
        {
             cout<<cur- >data<<" "; //If the current node has no child nodes or the child nodes have been visited
            s.pop();
             pre=cur;
        }
         else
        {
             if(cur->rchild!=NULL)
                 s. push(cur->rchild);
             if(cur->lchild!=NULL)   
                 s.push(cur->lchild);
        }
    }   
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324737939&siteId=291194637