In some applications of binary tree, it is often required to find a node with some characteristics in the tree, or to do some processing of all nodes in the tree. This raises the question of traversing the binary tree (traversing binary trees), that is, how to visit each node in a tree by a path, so that each node is accessed once and only once. The meaning of "access" is very wide, it can be a variety of processing nodes, such as output node information.
Recalling the recursive definition of binary tree, the binary tree consists of three units of root node and Saozi right subtree. As a result, traversing the three units can traverse the entire binary tree. Here are a few common two-fork tree traversal algorithms.
1. Traversing the binary tree in the first order
The operation of the first-order Traverse Fork Village is defined as:
If the binary tree is empty, the operation is empty; otherwise:
1. Access the root node;
2. The first sequence traverses the left subtree;
3. First-order traversal of the right subtree.
Recursive algorithm
void preorderrecursion (Bitree t) { if (t) { printf (" ", t->data); Preorderrecursion (T-lchild); Preorderrecursion (T-rchild);} }
Non-recursive algorithm
Suppose T is a pointer variable that points to a binary root node, and a non-recursive algorithm is:
If the binary tree is empty, return it; otherwise, make p=t
1. Access the node to which P points;
2. Make q=p->rchild, if q is not empty, then q into the stack;
3. Make P=p->lchild, if p is not empty, then turn 1, otherwise turn 4;
4. Rewind to P, turn 1 until the stack is empty.
The reference code looks like this:
void preorderiteration (Bitree T) { stack<BiTree> St; St.push (T); while (! st.empty ()) { = st.top (); St.pop (); printf ("", t->data); if (t->rchild) St.push (t->rchild); if (t->lchild) St.push (t->lchild); }
2. Middle sequence traversal binary tree
The operation of the middle sequence traversal binary tree is defined as:
If the binary tree is empty, the operation is empty; otherwise:
1. The middle sequence traverses the left subtree;
2. Access the root node;
3. The middle sequence traverses the right sub-tree.
Recursive algorithm
void inorderrecursion (Bitree t) { if (t) { inorderrecursion (t-lchild); printf ("", t->data); Inorderrecursion (T-rchild);} }
Non-recursive algorithm
Suppose T is a pointer variable that points to the root node of a binary tree, and the non-recursive algorithm is:
If the binary tree is empty, return it; otherwise, make p=t
1. If p is not empty, then the P-in stack,p=p->lchild;
2. Otherwise (that is, p is empty), the stack to p, access to the node point P;
3. Make p=p->rchild, turn 1.
The reference code looks like this:
void Inorderiteration (Bitree T) {stack <bitree> St; while (T | |! St.empty ()) { if (T!= = T->lchild; else {T =
St.top (); St.pop (); printf (
%c ", T->data); T = T->rchild; } }}
3. Post-secondary traversal of binary tree
The operation of traversing a binary tree is defined as:
If the binary tree is empty, the operation is empty; otherwise:
1. Post-sequential traversal of the left sub-tree;
2. Go through the right sub-tree;
3. Access root node
Recursive algorithm
void postorderrecursion (Bitree t) { if (t) { postorderrecursion (t) Lchild); Postorderrecursion (T-rchild); printf ("", t->data);} }
Non-recursive algorithm
4. Hierarchical traversal of binary trees
The hierarchical traversal of the binary tree is the traversal from the root node, in the hierarchy order "top-down, from left to right" to access the various nodes in the tree.
To ensure that it is traversed hierarchically, a queue must be set, initialized to null. Set T is a pointer variable that points to the root node, and the hierarchy traversal non-recursive algorithm is:
If the binary tree is empty, it returns; otherwise, the p=t,p is queued:
1. Team first elements out of the team to P;
2. Access the node to which P points;
3. Queue the left and right child nodes of the node to which P points.
Until the team is empty.
The reference code looks like this:
Traversal of binary tree with "data structure"