Various traversal of binary tree

Source: Internet
Author: User

Reprint Address http://blog.csdn.net/SJF0115/article/details/8645991

Tree structure is a kind of important nonlinear data structure, in which tree and two-fork trees are most commonly used.

A binary tree is an ordered tree with a maximum of two subtrees per node. Usually the root of the subtree is referred to as the left subtree and right subtree. Binary trees are often used as binary search trees and two-fork or binary-ordered trees. Each node of a binary tree has a maximum of two subtrees trees (no nodes with a degree greater than 2), and the subtree of the binary tree has left and right points, and the order cannot be reversed. The first layer of the binary tree has a maximum of 2 i-1 nodes, and a two-tree with a depth of K has a maximum of 2^ (k)-1 nodes; for any binary tree T, if its terminal node number (that is, the leaf node number) is N0, the 2 node is n2, then N0 = n2 + 1.

The chain storage structure of binary tree is a kind of important data structure, and its form is defined as follows:

[CPP]View Plaincopy
    1. Binary tree nodes
    2. typedef struct bitnode{
    3. //Data
    4. char data;
    5. //Left child hands
    6. struct Bitnode *lchild,*rchild;
    7. }bitnode,*bitree;

Binary Tree creation:

By reading a string, the algorithm for building a two-fork tree is as follows:

[CPP]View Plaincopy
  1. Create a two-fork tree by ordinal sequence
  2. int Createbitree (Bitree &t) {
  3. char data;
  4. ///Enter the value (one character) of the node in the binary tree in order of precedence, ' # ' indicates an empty tree
  5. scanf ("%c", &data);
  6. if (data = = ' # ') {
  7. T = NULL;
  8. }
  9. else{
  10. T = (bitree) malloc (sizeof (Bitnode));
  11. //Generate root node
  12. T->data = data;
  13. //Construct left sub-tree
  14. Createbitree (T->lchild);
  15. //Construct right sub-tree
  16. Createbitree (T->rchild);
  17. }
  18. return 0;
  19. }


Traversal of a binary tree:

Traversal is one of the most basic operations of the tree, so-called traversal of the binary tree, that is, according to a certain rules and order all the nodes of the binary tree, so that each node is visited once, and only be visited once. Because the binary tree is a nonlinear structure, the traversal of the tree is essentially the transformation of each node of the two-fork tree into a linear sequence to be represented.

Recursive algorithm:

[CPP]View Plaincopy
  1. Output
  2. void Visit (Bitree T) {
  3. if (t->data! = ' # ') {
  4. printf ("%c", t->data);
  5. }
  6. }
  7. First Order traversal
  8. void preorder (Bitree T) {
  9. if (T! = NULL) {
  10. //Access root node
  11. Visit (T);
  12. //access to left dial hand nodes
  13. Preorder (T->lchild);
  14. //Access right sub-node
  15. Preorder (T->rchild);
  16. }
  17. }
  18. Middle Sequence traversal
  19. void Inorder (Bitree T) {
  20. if (T! = NULL) {
  21. //access to left dial hand nodes
  22. Inorder (T->lchild);
  23. //Access root node
  24. Visit (T);
  25. //Access right sub-node
  26. Inorder (T->rchild);
  27. }
  28. }
  29. Post-post traversal
  30. void Postorder (Bitree T) {
  31. if (T! = NULL) {
  32. //access to left dial hand nodes
  33. Postorder (T->lchild);
  34. //Access right sub-node
  35. Postorder (T->rchild);
  36. //Access root node
  37. Visit (T);
  38. }
  39. }

Non-recursive algorithm:

<1> first-order traversal:

"Thinking": After access to T->data, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, and then sequentially traverse the right subtree of T.

[CPP]View Plaincopy
  1. /* Sequential traversal (non-recursive)
  2. Idea: After access to T->data, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, and then first traversing the right subtree of T.
  3. */
  4. void PreOrder2 (Bitree T) {
  5. Stack<bitree> Stack;
  6. //p is a traversal pointer
  7. Bitree p = T;
  8. //stack is not empty or p is not empty when the loop
  9. While (P | |!stack.empty ()) {
  10. if (P! = NULL) {
  11. //deposit in stack
  12. Stack.push (P);
  13. //Access root node
  14. printf ("%c", p->data);
  15. //traverse the left subtree
  16. p = p->lchild;
  17. }
  18. else{
  19. //Stack back
  20. p = stack.top ();
  21. Stack.pop ();
  22. //access to right sub-tree
  23. p = p->rchild;
  24. }
  25. }//while
  26. }

<2> in-sequence traversal

"Idea": T is to traverse the root of the tree pointer, the middle sequence traversal requires that after traversing the left dial hand tree, access to the root, and then traverse the right subtree.
First, the t into the stack, traversing the left subtree, after traversing the Zuozi return, the top element of the stack should be T, out of the stack, access to T->data, and then the right subtree of t traversal.

[CPP]View Plaincopy
  1. void InOrder2 (Bitree T) {
  2. Stack<bitree> Stack;
  3. //p is a traversal pointer
  4. Bitree p = T;
  5. //stack is not empty or p is not empty when the loop
  6. While (P | |!stack.empty ()) {
  7. if (P! = NULL) {
  8. //deposit in stack
  9. Stack.push (P);
  10. //traverse the left subtree
  11. p = p->lchild;
  12. }
  13. else{
  14. //fallback, Access root node
  15. p = stack.top ();
  16. printf ("%c", p->data);
  17. Stack.pop ();
  18. //access to right sub-tree
  19. p = p->rchild;
  20. }
  21. }//while
  22. }


<3> Post-traversal

"Thinking": T is to traverse the root of the tree pointer, post-order traversal requirements after traversing the left and right subtree, and then access to the root. It is necessary to determine whether the left and right subtrees of the root node have been traversed.

[CPP]View Plaincopy
  1. Post-post traversal (non-recursive)
  2. typedef struct bitnodepost{
  3. Bitree Bitree;
  4. char tag;
  5. }bitnodepost,*bitreepost;
  6. void PostOrder2 (Bitree T) {
  7. Stack<bitreepost> Stack;
  8. //p is a traversal pointer
  9. Bitree p = T;
  10. Bitreepost BT;
  11. //stack is not empty or p is not empty when the loop
  12. While (P! = NULL | |!stack.empty ()) {
  13. //traverse the left subtree
  14. While (P! = NULL) {
  15. BT = (bitreepost) malloc (sizeof (bitnodepost));
  16. Bt->bitree = p;
  17. //Visited left dial hand tree
  18. Bt->tag = ' L ';
  19. Stack.push (BT);
  20. p = p->lchild;
  21. }
  22. ///left/right sub-tree access complete access to root node
  23. While (!stack.empty () && (Stack.top ())->tag = = ' R ') {
  24. BT = Stack.top ();
  25. //Stack back
  26. Stack.pop ();
  27. bt->bitree;
  28. printf ("%c", bt->bitree->data);
  29. }
  30. //Traverse Right sub-tree
  31. if (!stack.empty ()) {
  32. BT = Stack.top ();
  33. //access to right sub-tree
  34. Bt->tag = ' R ';
  35. p = bt->bitree;
  36. p = p->rchild;
  37. }
  38. }//while
  39. }

<4> Hierarchy Traversal

"Thinking": Each node is accessed hierarchically from top to bottom, from left to right, and the queue is used during the hierarchical traversal process.

[CPP]View Plaincopy
  1. Hierarchical traversal
  2. void Levelorder (Bitree T) {
  3. Bitree p = T;
  4. //Queue
  5. Queue<bitree> queue;
  6. //The root node is enqueued
  7. Queue.push (P);
  8. //Queue not empty loop
  9. While (!queue.empty ()) {
  10. //Enemy elements out of the team
  11. p = Queue.front ();
  12. //Access node of P point
  13. printf ("%c", p->data);
  14. //Exit queue
  15. Queue.pop ();
  16. //Left dial hand tree is not empty, the left sub-tree is enqueued
  17. if (p->lchild! = NULL) {
  18. Queue.push (P->lchild);
  19. }
  20. //Right subtree is not empty, the right subtree is enqueued
  21. if (p->rchild! = NULL) {
  22. Queue.push (P->rchild);
  23. }
  24. }
  25. }



Test Case:

Input:

abc# #DE #g# #F # # #

Output:

Various traversal of binary tree

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.