# 1, Traversal of binary tree

Access each node in the tree according to a search path, so that each node is accessed. It is mainly divided into first order traversal, middle order traversal, second order traversal and sequence traversal

# 2, Preorder traversal

## 2.1 manual calculation

The exam usually gives the shape of a tree and writes its preorder traversal

## 2.2 code

- Recursive preorder traversal code

void PreOrder(BiTree T){ if(T!=NULL) visit(T);//Access root node PreOrder(T->lchild); //Recursive traversal of left subtree PreOrder(T->rchild); //Recursive traversal of right subtree }

- Non recursive preorder traversal code

void PreOrder2(BiTree T){ InitStack(S);//Initialize stack S; BiTree p = T;//Initialization p is a traversal pointer while(p||!IsEmpty(S)){ //Loop when stack is not empty or p not empty if(p){ //All the way to the left visit(p); //Access current node Push(S,p); //Stack current node p=p->lchild; //The left child is not empty. Go straight to the left } else{ //Exit the stack and turn to the right subtree of the stack node Pop(S,p); //Stack top element out of stack p=p->rchild;//Go to the right subtree, and p is the right child of the current node } //Return to the while loop and continue to enter the if else statement } }

# 3, Medium order traversal

## 3.1 manual calculation

## 3.2 code

- Recursive middle order traversal code

void InOrder(BiTree T){ if(T!=NULL) InOrder(T->lchild); //Recursive traversal of left subtree visit(T);//Access root node InOrder(T->rchild); //Recursive traversal of right subtree }

- Non recursive middle order traversal code

void InOrder2(BiTree T){ InitStack(S);//Initialize stack S; BiTree p = T;//Initialization p is a traversal pointer while(p||!IsEmpty(S)){ //Loop when stack is not empty or p not empty if(p){ //All the way to the left Push(S,p); //Stack current node p=p->lchild; //The left child is not empty. Go straight to the left } else{ //Exit the stack and turn to the right subtree of the stack node Pop(S,p); //Stack top element out of stack visit(p); //Access out of stack node p=p->rchild;//Go to the right subtree, and p is the right child of the current node } //Return to the while loop and continue to enter the if else statement } }

# 4, Postorder traversal

## 4.1 manual calculation

## 4.2 code

- Recursive postorder traversal code

void PostOrder(BiTree T){ if(T!=NULL) visit(T);//Access root node PostOrder(T->lchild); //Recursive traversal of left subtree PostOrder(T->rchild); //Recursive traversal of right subtree }

- Non recursive postorder traversal code

void PostOrder2(BiTree T){ InitStack(S);//Initialize stack S; BiTree p = T;//Initialization p is a traversal pointer while(p||!IsEmpty(S)){ //Loop when stack is not empty or p not empty if(p){ //All the way to the left Push(S,p); //Stack current node p=p->lchild; //The left child is not empty. Go straight to the left } else{ //towards the right GetTop(S,p); //Read stack top node (not out of stack) if(p->rchild &&p->rchild!=r) //If the right subtree exists and has not been accessed p = p->rchild; //turn right else{ //Otherwise, pop up the node and access it pop(S,p); //Pop up node visit(p->data); //Access this node r = p; //Record the most recently visited nodes p=NULL; //After the node is accessed, reset the p pointer } //else } //while }

# 5, Sequence traversal

## 5.1 manual calculation

## 5.2 code

void LevelOrder(BiTree T){ InitQueue(Q); //Initialize auxiliary queue BiTree p; EnQueue(Q,T); //Queue root node while(!IsEmpty (Q)){//Loop if queue is not empty DeQueue(Q,p);//Team leader node out of the team visit(p); //Access outbound node if(p->lchild !=NULL) //If the left subtree is not empty, the root node of the left subtree will join the queue EnQueue(Q,p->lchild); if(p->rchild !=NULL) //If the right subtree is not empty, the root node of the right subtree will join the queue EnQueue(Q,p->lchild); } }

# 6, Constructing binary tree from traversal

## 6.1 test method 1

Middle order traversal + before and after, any one of the sequence traversal can form a binary tree.

Exams often give two sequences (one of which must be traversed in time) and then draw a binary tree or push another traversal

- Examples

## 6.2 common pit digging multiple-choice questions

- Let a and b be two nodes on a binary tree. When traversing in middle order, the condition that a is in front of b is that a is to the left of b.
- The relative order of leaf nodes in the preorder, middle order and postorder traversal sequences of any binary tree does not change.
- N and m are two nodes on a binary tree. When traversing the middle order, the condition before n is m is that n is the ancestor of M
- N and m are two nodes on a binary tree. When traversing in post order, the condition that n is before M is that n is the descendant of M
- The pre order traversal is opposite to the post order traversal. For example, the pre order traverses ABC and the post order traverses CBA, indicating that the tree only has a degree of 1

# 7, Clue binary tree

## 7.1 basic concepts

- The so-called binary tree traversal is to arrange all nodes in the binary tree into a sequence.
- In ordinary binary tree traversal, especially binary linked list, it can only represent a parent-child relationship. Can not reflect the relationship between the precursor and the successor of the element.
- There are n+1 null pointers in a binary tree with n nodes.

## 7.2 node structure and structure code

typedef struct ThreadNode{ ElemType data; struct ThreadNode *lchild,*rchild；//Left and right child pointers int ltag,rtag； //Left and right clue signs 0 represent children, and 1 represents pointer }ThreadNode,*ThreadTree;

## 7.3 handwritten construction clue binary tree (regular test)

Classic example, this problem can basically solve most manual problems

## 7.4 traversal of clue binary tree

Insert the code slice here