Algorithm 9---Algorithm 9---binary tree traversal

Source: Internet
Author: User

Here we go through the recursive and non-recursive traversal of the binary tree

I. Pre-sequence traversal

The pre-order traversal is accessed in the order of the root node-left child-right child.

1. Recursive implementations
1 voidPreOrder1 (Bintree *root)//recursive pre-sequence traversal2 {3     if(root!=NULL)4     {5cout<<root->data<<" ";6PreOrder1 (root->lchild);7PreOrder1 (root->rchild);8     }9}

2. Non-recursive implementations

Prioritize access to the root node, and then access the left child and right child, respectively, based on the order of the pre-order traversal access. That is, for any node, it can be regarded as a root node, so it is directly accessible, after access, if the left child is not empty, the same rules access its left subtree, and then access its right subtree when its left subtree is accessed. So the process is as follows:

For any node P:

1) Access the node P and the node p into the stack;

2) Determine whether the left child of the node P is empty, if it is empty, take the top node of the stack and perform a stack operation, and the right child of the top node of the stack is set to the current node p, loop to 1); If not empty, the left child of P is placed as the current node p;

3) until p is null and the stack is empty, the traversal ends.
1 voidPreOrder2 (Bintree *root)//non-recursive pre-sequence traversal2 {3Stack<bintree*>s;4Bintree *p=Root;5      while(p!=null| |!s.empty ())6     {7          while(p!=NULL)8         {9cout<<p->data<<" ";Ten S.push (p); OneP=p->Lchild; A         } -         if(!s.empty ()) -         { thep=s.top (); - S.pop (); -P=p->Rchild; -         } +     } -}

Two. Middle Sequence traversal

The middle sequence traversal is accessed in the order of "left child-root node-right child".

1. Recursive implementations

1 voidInOrder1 (Bintree *root)//Recursive middle order traversal2 {3     if(root!=NULL)4     {5InOrder1 (root->lchild);6cout<<root->data<<" ";7InOrder1 (root->rchild);8     }9}

2. Non-recursive implementations

According to the order of the sequence traversal, for any node, priority access to the left child, and the left child node can be regarded as a node, and then continue to access their left child node, until the left child node is found to be empty nodes to access, and then follow the same rules to access its right subtree. So the process is as follows:

For any node p,

1) If the left child is not empty, the p is put into the stack and the left child of P is placed as the current p, then the same processing is done for the current node p;

2) If the left child is empty, take the top element of the stack and perform a stack operation, access the top node of the stack, and then place the current p as the right child of the top node of the stack.

3) until p is null and the stack is empty the traversal ends

1 voidInOrder2 (Bintree *root)//non-recursive mid-order traversal2 {3Stack<bintree*>s;4Bintree *p=Root;5      while(p!=null| |!s.empty ())6     {7          while(p!=NULL)8         {9 S.push (p);TenP=p->Lchild; One         } A         if(!s.empty ()) -         { -p=s.top (); thecout<<p->data<<" "; - S.pop (); -P=p->Rchild; -         } +     }     -}

Three. Post-traversal

Sequential traversal is accessed in the order of "left child-right child-root node".

1. Recursive implementations

1 voidPostOrder1 (Bintree *root)//recursive post-sequential traversal2 {3     if(root!=NULL)4     {5PostOrder1 (root->lchild);6PostOrder1 (root->rchild);7cout<<root->data<<" ";8     }    9}

2. Non-recursive implementations

The non-recursive implementation of the post-sequential traversal is the most difficult of three traversal methods. Because in the post-order traversal, to ensure that both the left child and the right child have been visited and the left child in front of the right child access to access to the root node, which brings the process of control problems. Here are two ideas.

The first way of thinking: for any node p, put it into the stack, and then along its left subtree search down, until the search for a node without the left child, the node appears at the top of the stack, but at this time can not be stacked and access, so its right child is also visited. So next follow the same rules to the right subtree of the same processing, when the end of the access to their right child, the node appears on the top of the stack, it can be out of the stack and access. This guarantees the correct order of access. As you can see, in this process, each node appears at the top of the stack two times, and can only be accessed the second time it appears on top of the stack. Therefore, it is necessary to set more than one variable to identify whether the node appears first on the top of the stack.

1 voidPostOrder2 (Bintree *root)//non-recursive post-traversal2 {3Stack<btnode*>s;4Bintree *p=Root;5Btnode *temp;6   while(p!=null| |!s.empty ())7 {8   while(P!=null)//search down the Zuozi until a node with no left subtree appears9  {TenBtnode *btn= (Btnode *)malloc(sizeof(Btnode)); OneBtn->btnode=p; Abtn->isfirst=true; - S.push (BTN); -P=p->Lchild; the } -  if(!s.empty ()) - { -temp=s.top (); + S.pop (); -  if(temp->isfirst==true)//represents the first time it appears on top of the stack +  { Atemp->isfirst=false; at S.push (temp); -P=temp->btnode->Rchild; - } -  Else //second appears on top of the stack -  { -cout<<temp->btnode->data<<" "; inp=NULL; - } to } + } -}

The second way of thinking: to ensure that the root node in the left child and right child access before access, so for any node p, first put it into the stack. If p does not exist on the left child and right child, it can be accessed directly, or p has left child or right child, but the left child and right child have been visited, you can also directly access the node. In either case, the right child of P and the left child are placed in the stack, which ensures that the left child is accessed in front of the right child, and the left child and right child are accessed in front of the root node each time the top element of the stack is taken.

1 voidPostOrder3 (Bintree *root)//non-recursive post-traversal2 {3Stack<bintree*>s;4Bintree *cur;//Current Node5Bintree *pre=null;//node of the previous visit6 S.push (root);7      while(!s.empty ())8     {9Cur=s.top ();Ten         if((cur->lchild==null&&cur->rchild==null) | | One(pre!=null&& (pre==cur->lchild| | Pre==cur->rchild ))) A         { -cout<<cur->data<<" ";//If the current node has no child nodes or if the child has been visited - S.pop (); thePre=cur; -         } -         Else -         { +             if(cur->rchild!=NULL) -S.push (cur->rchild); +             if(cur->lchild!=NULL) AS.push (cur->lchild); at         } -     }    -}

Algorithm 9---Algorithm 9---binary tree traversal

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.