Binary tree traversal

Source: Internet
Author: User

Binary tree traversal

Binary tree traversal is generally divided into three types of traversal methods: first-order traversal, middle-order traversal, and later-order traversal.

In the middle-order traversal, a node is a node in the bottom-right corner of its left subtree, and a node in the bottom-left corner of its right subtree.
In the post-order traversal,
• If the node is the root node, its successor is blank;
• If the node is the right subtree of the parent tree, or the left subtree but the parent tree does not have the right subtree, the node is the parent node;
• If the node is the left subtree of the parent and the parent has the right subtree, the node is the first node that the right subtree traverses in the descending order.

 

Binary tree traversal is implemented as follows:

1 # include <stack> 2 # include <queue> 3 4/* 5 * @ struct 6 * @ brief Binary Tree node 7 */8 typedef struct binary_tree_node_t 9 {10 binary_tree_node_t * lchild; /* left child */11 binary_tree_node_t * rchild;/* right child */12 void * data;/* node data */13} binary_tree_node_t; 14 15/*** @ brief first-order traversal and recursion. 17 * @ param [in] root Node 18 * @ param [in] function pointer for visit to access data elements 19 * @ return No 20 */21 void pre_order_r (const binary_tree_no De_t * root, int (* visit) (void *) 22 {23 if (root! = NULL) 24 {25 (void) visit (root-> data); 26 pre_order_r (root-> lchild, visit); 27 pre_order_r (root-> rchild, visit ); 28} 29} 30 31/** 32 * @ brief sequential traversal, recursion. 33 */34 void in_order_r (const binary_tree_node_t * root, int (* visit) (void *) 35 {36 if (root! = NULL) 37 {38 pre_order_r (root-> lchild, visit); 39 (void) visit (root-> data); 40 pre_order_r (root-> rchild, visit ); 41} 42} 43 44/** 45 * @ brief post-order traversal, recursion. 46 */47 void post_order_r (const binary_tree_node_t * root, int (* visit) (void *) 48 {49 if (root! = NULL) 50 {51 pre_order_r (root-> lchild, visit); 52 pre_order_r (root-> rchild, visit); 53 (void) visit (root-> data ); 54} 55} 56 57/** 58 * @ brief first traversal, non-recursion. 59 */60 void pre_order (const binary_tree_node_t * root, int (* visit) (void *) 61 {62 const binary_tree_node_t * p; 63 std :: stack <const binary_tree_node_t *> s; 64 p = root; 65 if (p! = NULL) 66 {67 s. push (p); 68} 69 70 while (! S. empty () 71 {72 p = s. top (); 73 s. pop (); 74 visit (p-> data); 75 if (p-> rchild! = NULL) 76 {77 s. push (p-> rchild); 78} 79 if (p-> lchild! = NULL) 80 {81 s. push (p-> lchild); 82} 83} 84} 85 86/** 87 * @ brief middle-order traversal, non-recursion. 88 */89 void in_order (const binary_tree_node_t * root, int (* visit) (void *) 90 {91 const binary_tree_node_t * p; 92 std :: stack <const binary_tree_node_t *> s; 93 p = root; 94 while (! S. empty () | p! = NULL) 95 {96 if (p! = NULL) 97 {98 s. push (p); 99 p = p-> lchild; 100} 101 else 102 {103 p = s. top (); 104 s. pop (); 105 visit (p-> data); 106 p = p-> rchild; 107} 108} 109 110 111/** 112 * @ brief post-order traversal, non-recursion. 113 */114 void post_order (const binary_tree_node_t * root, int (* visit) (void *) 115 {116/* p, accessed node, q, the accessed node */117 const binary_tree_node_t * p, * q; 118 std: stack <const binary_tree_node_t *> s; 119 p = root; 120 do {121 whil E (p! = NULL) 122 {123/* to the Left Bottom */124 s. push (p); 125 p = p-> lchild; 126} 127 q = NULL; 128 while (! S. empty () 129 {130 p = s. top (); 131 s. pop (); 132/* The right child does not exist or has been accessed. */133 if (p-> rchild = q) 134 {135 visit (p-> data); 136 q = p;/* Save the accessed node */137} 138 else 139 {140/* the current node cannot be accessed, requires a second stack */141 s. push (p); 142/* process the right subtree */143 p = p-> rchild; 144 break; 145} 146} 147} while (! S. empty (); 148} 149 150/** 151 * @ brief hierarchical traversal, that is, BFS.152 * 153 * is exactly the same as the first-order traversal, the only difference is that the stack is changed to queue 154 */155 void level_order (const binary_tree_node_t * root, int (* visit) (void *) 156 {157 const binary_tree_node_t * p; 158 std: queue <const binary_tree_node_t *> q; 159 p = root; 160 if (p! = NULL) 161 {162 q. push (p); 163} 164 while (! Q. empty () 165 {166 p = q. front (); 167 q. pop (); 168 visit (p-> data); 169 if (p-> lchild! = NULL) 170 {171/* It doesn't matter if it is left, right, right, or right */172 q. push (p-> lchild); 173} 174 if (p-> rchild! = NULL) 175 {176 q. push (p-> rchild); 177} 178} 179}

 

Related Article

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.