Binary Tree of Basic Data Structure
Implement binary tree traversal in C Language
Binary Tree node Definition
/* The time complexity of first-order, middle-order, and later-order traversal is O (n), and each node is only accessed once. The worst time complexity of the sequence is O (n ^ 2). When the binary tree is basically balanced, the time complexity is O (n) n as the number of nodes */typedef int tree_node_element; /*** @ author Wei Xuan * @ time 2015/07/11 * @ brief binary tree's node data structure **/typedef struct binary_tree_node {binary_tree_node * left; // left child binary_tree_node * right; // right child tree_node_element; // data node // constructor binary_tree_node (tree_node_element x): element (x), left (NULL), right (NULL) {}} binary_tree_node;
Binary tree recursive Traversal
/*** @ Author Wei Xuan * @ time 2015/07/11 * @ the forward traversal of the brief binary tree, recursive root-left-right * @ param root Node * @ param visit function pointer to access the root node * @ return no return */void pre_order_r (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {if (root = NULL) return; visit (root); pre_order_r (root-> left, visit ); pre_order_r (root-> right, visit);}/*** @ author Wei Xuan * @ time 2015/07/11 * @ moderate-order traversal of the brief binary tree, recursive version left-root-right * @ param root Node * @ param visit function pointer to access the root node * @ return none **/void in_order_r (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {if (root = NULL) return; in_order_r (root-> left, visit); visit (root ); in_order_r (root-> right, visit);}/*** @ author Wei Xuan * @ time 2015/07/11 * @ brief post-order traversal of the binary tree, recursive version left-right-root * @ param root Node * @ param visit function pointer for accessing the root node * @ return none **/void post_order_r (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {if (root = NULL) return; post_order_r (root-> left, visit); post_order_r (root-> right, visit); visit (root );}
Non-recursive traversal of Binary Trees
/*** @ Author Wei Xuan * @ time 2015/07/11 * @ pre-sequential traversal of the brief Binary Tree, non-recursive version-use the stack to complete, the essence of recursion is the function pointer * @ param root node * @ param visit to access the root node * @ return **/void pre_order (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {const binary_tree_node * temp = root; std: stack
AvgSatck; // first, the root node enters the stack if (temp! = NULL) avgSatck. push (temp); while (! AvgSatck. empty () {temp = avgSatck. top (); // change temp avgSatck here. pop (); visit (temp); // the stack is empty after the root node is printed in the first step. If (temp-> right! = NULL) avgSatck. push (temp-> right); if (temp-> left! = NULL) avgSatck. push (temp-> left) ;}}/*** @ author Wei Xuan * @ time 2015/07/11 * @ brief median traversal of a binary tree, non-recursive version -- stack-based, the essence of recursion is the function pointer * @ param root node * @ param visit to access the root node * @ return **/void in_order (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {const binary_tree_node * temp; std: stack
AvgSatck; temp = root; while (! AvgSatck. empty () | temp! = NULL) {if (temp! = NULL) {avgSatck. push (temp); temp = temp-> left;} else {temp = avgSatck. top (); avgSatck. pop (); visit (temp); temp = temp-> right ;}}/ *** @ author Wei Xuan * @ time 2015/07/11 * @ brief post-order traversal of the binary tree, non-recursive version-stack-based, the essence of recursion is the function pointer * @ param root node * @ param visit to access the root node * @ return **/void post_order (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {const binary_tree_node * temp1, * temp2; std: stack
AvgSatck; temp1 = root; do {while (temp1! = NULL) {avgSatck. push (temp1); temp1 = temp1-> left;} temp2 = NULL; while (! AvgSatck. empty () {temp1 = avgSatck. top (); avgSatck. pop (); if (temp1-> right = temp2) {visit (temp1); temp2 = temp1;} else {avgSatck. push (temp1); temp1 = temp1-> right; break ;}} while (! AvgSatck. empty ());}
The hierarchical traversal of a binary tree traverses non-recursive pre-sequential traversal code, except that the auxiliary storage structure must be changed to a queue.
/*** @ Author Wei Xuan * @ time 2015/07/11 * @ the level traversal of the brief binary tree is exactly the same as that of the forward traversal, however, use the queue storage function * @ param root node * @ param visit to access the root node function pointer * @ return **/void level_order (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {const binary_tree_node * temp = root; std: queue
AvgQueue; if (temp! = NULL) avgQueue. push (temp); while (! AvgQueue. empty () {temp = avgQueue. front (); avgQueue. pop (); visit (temp); if (temp-> left! = NULL) avgQueue. push (temp-> left); if (temp-> right! = NULL) avgQueue. push (temp-> right );}}
Complete Project
# Include
# Include
# Include
# Include
/* The time complexity of first-order, middle-order, and later-order traversal is O (n), and each node is only accessed once. The worst time complexity of the sequence is O (n ^ 2). When the binary tree is basically balanced, the time complexity is O (n) n as the number of nodes */typedef int tree_node_element; /*** @ author Wei Xuan * @ time 2015/07/11 * @ brief binary tree's node data structure **/typedef struct binary_tree_node {binary_tree_node * left; // left child binary_tree_node * right; // right child tree_node_element; // data node // constructor binary_tree_node (tree_node_element x): element (x), left (NULL), right (NULL) {}} binary_tree_node; /* binary tree traversal, including recursive and non-recursive versions * // *** @ author Wei Xuan * @ time 2015/07/11 * @ brief Binary Tree forward traversal, recursive root-left-right * @ param root Node * @ param visit function pointer to access the root node * @ return no return */void pre_order_r (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {if (root = NULL) return; visit (root); pre_order_r (root-> left, visit ); pre_order_r (root-> right, visit);}/*** @ author Wei Xuan * @ time 2015/07/11 * @ moderate-order traversal of the brief binary tree, recursive version left-root-right * @ param root Node * @ param visit function pointer to access the root node * @ return none **/void in_order_r (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {if (root = NULL) return; in_order_r (root-> left, visit); visit (root ); in_order_r (root-> right, visit);}/*** @ author Wei Xuan * @ time 2015/07/11 * @ brief post-order traversal of the binary tree, recursive version left-right-root * @ param root Node * @ param visit function pointer for accessing the root node * @ return none **/void post_order_r (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {if (root = NULL) return; post_order_r (root-> left, visit); post_order_r (root-> right, visit); visit (root);}/* Non-recursive version of binary tree traversal * // *** @ author Wei Xuan * @ time 2015/07/11 * @ brief pre-sequential traversal of binary tree, non-recursive version-stack-based, the essence of recursion is the function pointer * @ param root node * @ param visit to access the root node * @ return **/void pre_order (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {const binary_tree_node * temp = root; std: stack
AvgSatck; // first, the root node enters the stack if (temp! = NULL) avgSatck. push (temp); while (! AvgSatck. empty () {temp = avgSatck. top (); // change temp avgSatck here. pop (); visit (temp); // the stack is empty after the root node is printed in the first step. If (temp-> right! = NULL) avgSatck. push (temp-> right); if (temp-> left! = NULL) avgSatck. push (temp-> left) ;}}/*** @ author Wei Xuan * @ time 2015/07/11 * @ brief median traversal of a binary tree, non-recursive version -- stack-based, the essence of recursion is the function pointer * @ param root node * @ param visit to access the root node * @ return **/void in_order (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {const binary_tree_node * temp; std: stack
AvgSatck; temp = root; while (! AvgSatck. empty () | temp! = NULL) {if (temp! = NULL) {avgSatck. push (temp); temp = temp-> left;} else {temp = avgSatck. top (); avgSatck. pop (); visit (temp); temp = temp-> right ;}}/ *** @ author Wei Xuan * @ time 2015/07/11 * @ brief post-order traversal of the binary tree, non-recursive version-stack-based, the essence of recursion is the function pointer * @ param root node * @ param visit to access the root node * @ return **/void post_order (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {const binary_tree_node * temp1, * temp2; std: stack
AvgSatck; temp1 = root; do {while (temp1! = NULL) {avgSatck. push (temp1); temp1 = temp1-> left;} temp2 = NULL; while (! AvgSatck. empty () {temp1 = avgSatck. top (); avgSatck. pop (); if (temp1-> right = temp2) {visit (temp1); temp2 = temp1;} else {avgSatck. push (temp1); temp1 = temp1-> right; break ;}} while (! AvgSatck. empty ();}/*** @ author Wei Xuan * @ time 2015/07/11 * @ brief the level traversal of the binary tree is exactly the same as that of the forward traversal, however, use the queue storage function * @ param root node * @ param visit to access the root node function pointer * @ return **/void level_order (const binary_tree_node * root, int (* visit) (const binary_tree_node *) {const binary_tree_node * temp = root; std: queue
AvgQueue; if (temp! = NULL) avgQueue. push (temp); while (! AvgQueue. empty () {temp = avgQueue. front (); avgQueue. pop (); visit (temp); if (temp-> left! = NULL) avgQueue. push (temp-> left); if (temp-> right! = NULL) avgQueue. push (temp-> right );}} /*** @ author Wei Xuan * @ time 2015/07/12 * @ brief achieves the height of a binary tree * @ param * @ return height **/int getHeight (const binary_tree_node * root) {if (root = NULL) return 0; return getHeight (root-> left)> getHeight (root-> right )? GetHeight (root-> left): getHeight (root-> right) + 1 ;} /*** @ author Wei Xuan * @ time 2015/07/12 * @ brief visit function **/int visit (const binary_tree_node * node) {if (node! = NULL) {printf_s (% d, node-> element); return node-> element;} int main () {binary_tree_node root (1); binary_tree_node n1 (2 ); binary_tree_node n2 (3); binary_tree_node n3 (4); binary_tree_node n4 (5); binary_tree_node n5 (6); binary_tree_node n6 (7); binary_tree_node * proot = & root; binary_tree_node * pn1 = & n1; binary_tree_node * pn2 = & n2; binary_tree_node * pn3 = & n3; binary_tree_node * pn4 = & n4; binary_tree_node * pn5 = & n5; binary_tree_node * pn6 = & n6; proot-> left = pn1; proot-> right = pn2; pn1-> left = pn3; pn1-> right = pn4; pn2-> left = pn5; pn2-> right = pn6; pn3-> left = NULL; pn3-> right = NULL; pn4-> left = NULL; pn4-> right = NULL; pn5-> left = NULL; pn5-> right = NULL; pn6-> left = NULL; pn6-> right = NULL; pre_order_r (proot, visit); puts (); pre_order (proot, visit); puts (); in_order_r (proot, visit); puts (); in_order (proot, visit); puts (); post_order_r (proot, visit); puts (); post_order (proot, visit); puts (); level_order (proot, visit); puts (); int h = getHeight (proot); return 0 ;}
Binary Tree Features
Among the three Traversal Algorithms, the basic nodes are only accessed once, and the time complexity is O (n). At the same time, the space complexity is also O (n)
The time complexity of layered traversal is related to the balance of Binary Trees. In extreme cases, the time complexity is O (N ^ 2), and the time complexity of balanced binary trees is O (n)