Binary Tree Traversal in Preorder, Intermediate Order and Postorder

Posted by bertfour on Mon, 27 May 2019 01:17:46 +0200

As long as the computer is engaged in, the binary tree traversal in the data structure is not unfamiliar, but if the opportunity used is not much, then it will gradually fade away. Reviewing the past is the best way to learn. Now, review the knowledge in this regard.

First of all, I want to change the names of these traversals (pre-radical traversal, mid-radical traversal, post-radical traversal); the former, middle and post-radical traversal are originally relative to the root node, and a word less will lead to many unnecessary misunderstandings.

 

 

1. Pre-root order traversal: first traverse the root node, then traverse the left subtree, and finally traverse the right subtree.

ABDHECFG

2. Medium root order traversal: first traverse the left subtree, then traverse the root node, and finally traverse the right subtree.

HDBEAFCG

3. Later root order traversal: first traverse the left subtree, then traverse the right subtree, and finally traverse the root node.

HDEBFGCA

 

The process of constructing a binary tree is as follows:
1. Establish the root node according to the first element of the pre-radical sequence.
2. Find the element in the mid-root sequence and determine the mid-root sequence of the left and right subtrees of the root node.
3. Determine the pre-radical sequence of left and right subtrees in the pre-radical sequence;
4. The left subtree is constructed from the front and middle root sequence of the left subtree.
5. The right subtree is constructed from the front and middle root sequence of the right subtree.

The process of constructing a binary tree is as follows:
1. Establish the root node according to the last element of the post-radical sequence.
2. Find the element in the mid-root sequence and determine the mid-root sequence of the left and right subtrees of the root node.
3. Determine the post-root sequence of left and right subtrees in the post-root sequence;
4. The left subtree is constructed from the posterior and middle root sequence of the left subtree.
5. The right subtree is constructed from the posterior and middle root sequence of the right subtree.

 

Print the post-root code according to the pre-and middle-root order:

 

  1. #include "stdafx.h"  
  2. #include <stdlib.h>  
  3. #include <iostream>  
  4.   
  5. char preArray[] = "ABDHECFG";  
  6. char midArray[] = "HDBEAFCG";  
  7.   
  8. typedef struct BinaryTree BiTree;  
  9. struct BinaryTree   
  10. {  
  11.     char data;  
  12.     BiTree *LTree,*RTree;  
  13. };  
  14.   
  15.   
  16. void CreateBTree(BiTree **node,int mid_header,int mid_tail,int pre_header,int pre_tail)  
  17. {  
  18.       
  19.     if(pre_header <= pre_tail || mid_header <= mid_tail)  
  20.     {  
  21.         BiTree* child;  
  22.         child = (BiTree*)malloc(sizeof(BiTree));  
  23.         child->data = preArray[pre_header];  
  24.         child->LTree = NULL;  
  25.         child->RTree = NULL;  
  26.         *node = child;  
  27.         int mid_num;  
  28.         for (mid_num = 0;preArray[pre_header] != midArray[mid_num];mid_num++);  
  29.         CreateBTree(&child->LTree,mid_header,mid_num-1,pre_header+1,pre_header+mid_num-mid_header);//create left tree  
  30.         CreateBTree(&child->RTree,mid_num+1,mid_tail,pre_tail-mid_tail+mid_num+1,pre_tail);  
  31.     }  
  32.     else  
  33.     {  
  34.         *node=NULL;  
  35.     }  
  36.       
  37. }  
  38.   
  39. void ShowBTree(BiTree* p)  
  40. {  
  41.     if (p)  
  42.     {  
  43.         ShowBTree(p->LTree);  
  44.         ShowBTree(p->RTree);  
  45.         printf("%c",p->data);  
  46.     }  
  47. }  
  48.   
  49. int _tmain(int argc, _TCHAR* argv[])  
  50. {  
  51.       
  52.     BiTree *rootNode = NULL;  
  53.     rootNode = (BiTree*)malloc(sizeof(BiTree));  
  54.     rootNode->data = preArray[0];  
  55.     rootNode->LTree = NULL;  
  56.     rootNode->RTree = NULL;  
  57.     int mid_num = 0;  
  58.     for (mid_num = 0;preArray[0] != midArray[mid_num]; mid_num++);  
  59.     CreateBTree(&rootNode->LTree,0,mid_num-1,1,mid_num);//create left tree  
  60.     CreateBTree(&rootNode->RTree,mid_num+1,strlen(midArray)-1,mid_num+1,strlen(preArray)-1);//create right tree  
  61.       
  62.     ShowBTree(rootNode);  
  63.     system("pause");  
  64.     return 0;  
  65. }  

Topics: less