Binary tree traversal (recursive, non-recursive, And clue Binary Tree) and recursive Binary Tree

Source: Internet
Author: User

Binary tree traversal (recursive, non-recursive, And clue Binary Tree) and recursive Binary Tree

As a basic data structure, binary trees are widely used and are everywhere in our lives. traversal of Binary Trees is very common in binary tree applications. Different from the linear storage structure, each node of a binary tree may have two Subtrees. We can see from the storage structure of the Binary Tree:

1 template <typename T>2 typedef struct BiTNode3 {4     T data;5     struct BiTNode *lchild, *rchild;6 }BiTree;

Root Node, left subtree, right subtree-basic unit of binary tree. Then, according to the recursive thinking (the data structure is Yan min edition): When a complex problem can be resolved into several subproblems, if some subproblems have the same feature attributes as the original issue, they can be used in the same analysis and processing method as the original issue. If these subproblems are solved, the original problems will be solved. Recursive traversal is to break down the original binary tree into several Subtrees.

1 template <typename T> 2 int PreOrderTraverse (BiTree & Tree, int (* Visit) (T & e) 3 {4 // The Visit function is interpreted as print e, if 1 is returned successfully, otherwise 0 is returned. 5 if (Tree) // determine whether the Tree is an empty Tree 6 {7 if (Visit (Tree. data) // print the node data 8 if (PreOrderTraverse (Tree. lchild, Visit) // traverse 9 to the left if (PreOrderTraverse (Tree. rchild, Visit) return 1; // left traversal exit traverse 10 return 0; 11} 12 else return 1; 13}

From the recursive working process, we can see that the top of layer I stack, the left subtree returns to the I-1 layer for the right subtree traversal, the return of the right subtree directly deletes the return of the top stack (depending on the Traversal method: First sequential traversal, middle-order traversal, and then sequential traversal ).

As a result, we can simulate recursive algorithms to write non-recursive traversal functions.

  

1 template <typename T> 2 void PreOrderTraverse (BiTNode & Tree, int (* Visit (T e) 3 {4 InitStack (S); // initialize stack S 5 BiTree p; 6 Push (S, Tree); // The root node is in the stack 7 while (! StackEmpty (S) 8 {9 while (GetTop (S, p) & p) 10 {11 Visit (p. data); 12 Push (S, (p. lchild); // left subtree pressure stack 13} // while14 Pop (S, * p); // empty pointer out of stack, return its node --------- 115 if (! StackEmpty (S) 16 {17 Pop (S, p); // ------------------------------- 218 Push (S, p. rchild); 19} // if20} // while21}

Stack is used to manage the traversal process. Because it is a sequential traversal, a node is Visit every time the stack is pressed to the left, when the left end is reached (the root node of each subtree in the left subtree is Visit from the root node in turn), the stack is pressed to the right. When the right leaf node is reached, the stack is rolled back due to the rollback of 17th rows, the top of the stack is changed to the root node of the upper layer. That is, when traversing the right subtree, you do not need to save the root pointer of the current layer. You can directly modify the pointer at the top of the stack. Some of them are unclear, but this is the same as the recursive algorithm, but it is just the way in and out of the management stack.

Note: A rough look may lead to a doubt: Pop stack bounce p has more than one layer. Here, the first Pop function exits the NULL pointer, and p indicates that its return value is also null. After the second Pop FUNCTION p is the top of the valid stack. For example, XiaoHong assembles an object to list the steps. When she wants to proceed with this step, she will move it from the list, this moment is the impact of the previous event.

These two types of traversal are used to linearly operate non-linear structures. They reference the concept of a two-way linked list, modify the existing data structure of a binary tree, and add two new sign domains-LTag and RTag.

1 template <typename T> 2 typedef struct BiThrNide 3 {4 T data; 5 int LTag, RTag; 6 BiThrTree * lchild, * rchild; 7} BiThrTree; 8 9 void InOrderTraverse (BiThrTree & Tree, int (* Visit (T e ))) 10 {11 // The lchild of the Tree points to the root node of the Tree 12 // The LTag and RTag are 0, then the pointer field points to the child node and 1 points to the frontend or successor 13 BiThrTree p; 14 while (p! = Tree) 15 {16 while (p. LTag = 0) p = p. lchild; // traverse to the left 17 if (! Visit (p. data) exit (ERROR); 18 while (p. RTag = 1 & p. rchild! = Tree) // No right subtree pointing to the successor, and the successor is not the header Node 19 {20 P = P. rchild; // traverse 21 Visit (p. data); 22} 23 p = p. rchild; 24} 25}

Because the storage structure of the clue binary tree is more plump than that of the common binary tree, the traversal process is better understood and will not be described here.

  

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.