Traverse Binary Tree)Traverse Binary Tree: accesses all nodes in the Binary Tree in the specified Order (regular), so that each node is accessed once and only once.
Access: it refers to processing a node. For example, output information and modify node values.
Order (rule): a binary tree is a non-linear structure. Each node can have two left and right Subtrees. Therefore, after accessing a node, the next accessed node faces different choices. Therefore, we need to find an order (rule) so that the nodes on the binary tree can be arranged on a linear queue for convenient traversal.
Basic components of a binary tree: root node, left subtree, and right subtree. If the three parts can be traversed in sequence, the binary tree is traversed.
If L, D, and R are used to traverse the left subtree, the root node, and the right subtree, there are six traversal schemes: DLR, LDR, LRD, DRL, RDL, and RLD. If left and right are specified, only the first three cases are described as follows:
DLR -- first (Root) traversal.
LDR -- traverse in the (Root) Order.
LRD -- Post (Root) sequential traversal.
Known Binary Tree, write the first sequence, middle sequence, and back Sequence
Determine the binary tree by knowing the first and middle order sequences
Determine the binary tree based on the known post-order sequence and Middle-Order Sequence
Traversal AlgorithmFor binary tree traversal, the recursive and non-recursive Traversal Algorithms are discussed respectively.
Recursive Traversal Algorithms have a very clear structure, but beginners are often unacceptable or skeptical and do not dare to use them. In fact, recursive algorithms are controlled by the system by using stacks.
The control in non-recursive algorithms is defined by the designer and implemented using stacks.
Traverse Binary Trees in sequence1 Recursive Algorithm
The recursive definition of an algorithm is:
If the binary tree is empty, the traversal ends. Otherwise
(1) access the root node;
(2) traverse the left subtree in sequence (this algorithm is called recursively );
(3) traverse the right subtree in sequence (this algorithm is called recursively ).
Recursive Algorithm void PreorderTraverse (BTNode * T) {if (T = NULL) return; visit (T-> data ); /* access the root node */PreorderTraverse (T-> Lchild); // first traverse the left subtree PreorderTraverse (T-> Rchild ); // traverse the right subtree first} Description: 1. The visit () function is the data domain of the Access Node. Its requirements depend on the specific problem. It can be the simplest print output. 2. The tree adopts the storage structure of the binary linked list and points it with the pointer variable T.
2 Non-Recursive Algorithms
If T is a pointer variable pointing to the root node of a binary tree, the non-recursive algorithm is:
If the binary tree is empty, return; otherwise, p = T;
(1) access the node pointed to by p;
(2) q = p-> Rchild. If q is not empty, q is pushed to the stack;
(3) p = p-> Lchild. If p is not empty, convert (1). Otherwise, convert (4 );
(4) Roll Back to p and turn to (1) until the stack is empty.
Algorithm Implementation:
#define MAX_STACK_SIZE 50void PreorderTraverse( BTNode *T){ BTNode *Stack[MAX_STACK_SIZE ] ,*p=T, *q ;int top=0 ;if (T==NULL) printf(“ Binary Tree is Empty!\n”) ;else { do { visit( p-> data ) ; q=p->Rchild ; if ( q!=NULL ) stack[top++]=q ; p=p->Lchild ; if (p==NULL&& top!=0) {top-- ;p=stack[top] ; } } while (p!=NULL) ;}}