Create a binary tree, recursively traverse, non-recursively traverse

Source: Internet
Author: User

A binary tree (binarytree) is a finite set of n (n ≥ 0) nodes. It is also an empty set (n = 0 ), it may also be composed of a root node and a binary tree of two left and right sub-trees separated from each other.

A binary tree is not a tree, but the same structure as a tree.

 

This is the creation of a binary tree, recursive traversal, non-recursive traversal. Without comments, some comments can also be used for functions. I personally feel quite good.

 

 /********************* <Br/> Create a binary tree, recursively traverse, non-recursive traversal, it can be said that it is a binary tree algorithm <br/> compiled by using Dev C ++ <br/> 2010/08/08 </P> <p> *********** * ***********/<br/> # include <stdio. h> <br/> # include <stdlib. h> <br/> # define maxsize 100 <br/> int increment = 10; <br/> typedef struct bitnode <br/>{< br/> char data; <br/> struct bitnode * lchild; <br/> struct bitnode * rchild; <br/>} * bitree; <br/> typedef struct queue_bitnode <br/>{< br/> bitr Ee v; <br/> struct queue_bitnode * Next; <br/>}; <br/> typedef struct queue_bitree <br/>{< br/> queue_bitnode * front; <br/> queue_bitnode * rear; <br/>}; <br/> typedef struct stack_bitnode <br/>{< br/> bitree V; <br/> }; <br/> typedef struct stack_bitree <br/> {<br/> stack_bitnode * base; <br/> stack_bitnode * Top; <br/> int stacksize; <br/>}; <br/> void create_bitree (bitree & T) <br/>{< br/> char ch; <br/> Printf ("Enter the element value to complete the binary tree/N"); <br/> scanf ("% C", & Ch ); <br/> If (CH = '') <br/> T = NULL; <br/> else {<br/> T = (bitnode *) malloc (sizeof (bitnode); <br/> T-> DATA = CH; <br/> create_bitree (t-> lchild ); <br/> create_bitree (t-> rchild); <br/>}< br/> void preordertraverse (bitree & T) <br/>{< br/> If (t) <br/>{< br/> printf ("% C", T-> data ); <br/> preordertraverse (t-> lchild); <br/> preordertraverse (t-> rchild); <B R/>}< br/> void inordertraverse (bitree & T) <br/>{< br/> If (t) <br/>{< br/> inordertraverse (t-> lchild); <br/> printf ("% C", T-> data ); <br/> inordertraverse (t-> rchild); <br/>}< br/> void postordertraverse (bitree & T) <br/>{< br/> If (t) <br/>{< br/> postordertraverse (t-> lchild ); <br/> postordertraverse (t-> rchild); <br/> printf ("% C", T-> data ); <br/>}< br/> void init_queue_bitree (Qu Eue_bitree & Q) <br/>{< br/> q. Front = Q. Rear = (queue_bitnode *) malloc (sizeof (bitnode); <br/> If (! Q. front) <br/> exit (0); <br/> q. front-> next = NULL; <br/>}< br/> void enqueue_bitree (queue_bitree & Q, bitree t) <br/>{< br/> struct queue_bitnode * P; <br/> P = (queue_bitnode *) malloc (sizeof (bitnode); <br/> If (! P) <br/> exit (0); <br/> P-> V = T; <br/> P-> next = NULL; <br/> q. rear-> next = P; <br/> q. rear = P; <br/>}< br/> void dequeue_bitree (queue_bitree & Q, bitree & T) <br/>{< br/> If (Q. rear = Q. front) <br/>{< br/> printf ("Empty queue/N"); <br/> exit (0 ); <br/>}< br/> queue_bitnode * P; <br/> P = Q. front-> next; <br/> T = p-> V; <br/> q. front-> next = p-> next; <br/> If (P = Q. rear) <br/> q. rear = Q. front; <br/> free (p); <br/>}< br/> int em Pty_queue_bitree (queue_bitree & Q) <br/>{< br/> If (Q. rear = Q. front) <br/> return 1; <br/> else return 0; <br/>}< br/> void layordertraverse (bitree T) <br/>{< br/> queue_bitree Q; <br/> init_queue_bitree (Q); <br/> If (t) <br/> enqueue_bitree (Q, t ); <br/> while (! Empty_queue_bitree (q) <br/>{< br/> dequeue_bitree (Q, T); <br/> printf ("% C", T-> data ); <br/> If (t-> lchild) <br/> enqueue_bitree (Q, T-> lchild); <br/> If (t-> rchild) <br/> enqueue_bitree (Q, T-> rchild ); <br/>}</P> <p >}< br/> // number of leaves in a binary tree <br/> int leaf_bitree (bitree & T) <br/>{</P> <p> If (! T) <br/> return 0; <br/> else if (! T-> lchild &&! T-> rchild) <br/> return 1; <br/> else <br/> return leaf_bitree (t-> lchild) + leaf_bitree (t-> rchild ); </P> <p >}< br/> // depth of the Binary Tree <br/> int depth_bitree (bitree & T) <br/>{< br/> int depthl, depthr; <br/> int depth = 0; <br/> If (! T) <br/> depth = 0; <br/> else {<br/> depthl = depth_bitree (t-> lchild ); <br/> depthr = depth_bitree (t-> rchild); <br/> depth = (depthl> depthr? Depthl: depthr) + 1; <br/>}< br/> return depth; <br/>}< br/>/* <br/> typedef struct stack_bitnode <br/> {<br/> bitree V; <br/>}< br/> typedef struct stack_bitree <br/>{< br/> stack_bitnode * base; <br/> stack_bitnode * Top; <br/> int stacksize; <br/>}; <br/> */<br/> void init_stack_bitree (stack_bitree & S) <br/>{< br/> S. base = (stack_bitnode *) malloc (sizeof (stack_bitnode) * maxsize); <br/> If (! S. base) <br/> exit (0); <br/> S. top = S. base; <br/> S. stacksize = maxsize; <br/>}< br/> void push (stack_bitree & S, bitree t) <br/>{< br/> If (S. top-s.base> = S. stacksize) <br/>{< br/> S. base = (stack_bitnode *) realloc (S. base, sizeof (stack_bitnode) * (S. stacksize + increment); <br/> If (! S. base) <br/> exit (0); <br/> S. top = S. base + S. stacksize; <br/> S. stacksize + = increment; <br/>}< br/> S. top-> V = T; <br/> S. top ++; <br/> // printf ("/n /N"); <br/>}< br/> void POP (stack_bitree & S, bitree & T) <br/>{< br/> If (S. base = S. top) <br/>{< br/> printf ("Empty queue/N"); <br/> exit (0 ); <br/>}< br/> T = (-- S. top)-> V; <br/> // printf ("/n /N"); <br/>}< br/> int empty_stack_bitree (stack_bitree S) <br/> {<br/> If (S. base = S. top) <br/> return 1; <br/> else return 0; <br/>}< br/> int gettop_stack_bitree (stack_bitree S, bitree & T) <br/>{< br/> If (S. top = S. base) <br/> return 0; <br/> T = (-- S. top)-> V; <br/> return 1; <br/>}< br/>/* <br/> // non-recursive first-order traversal <br/> void preordertraverse_nonrecur (bitree t) // when traversing, access a node, load it into the stack, and traverse its left subtree. Then, <br/> // The root tree is the last one to output the stack, traverse the right subtree <br/>{< br/> bitree P; <br/> P = T; <br/> stack_bitree s; <br /> Init_stack_bitree (s); <br/> while (p |! Empty_stack_bitree (s) <br/>{< br/> If (p) <br/>{< br/> printf ("% C", p-> data ); <br/> push (S, P); <br/> P = p-> lchild; <br/>}< br/> else {<br/> POP (S, P); <br/> P = p-> rchild; <br/>}< br/>} */<br/> void preordertraverse_nonrecur (bitree t) // you can access the Node during traversal, then, the right subtree of the node is added to the stack and the Access continues. <Br/> {<br/> bitree P; <br/> P = T; <br/> stack_bitree s; <br/> init_stack_bitree (s ); <br/> while (p |! Empty_stack_bitree (s) <br/>{< br/> If (p) <br/>{< br/> printf ("% C", p-> data ); <br/> push (S, P-> rchild); <br/> P = p-> lchild; <br/>}< br/> else {<br/> POP (S, P ); <br/>}</P> <p> // non-recursive sequential traversal <br/> void inordertraverse_nonrecur (bitree T) <br/> {<br/> bitree P; <br/> P = T; <br/> stack_bitree s; <br/> init_stack_bitree (s ); <br/> while (p |! Empty_stack_bitree (s) <br/>{< br/> If (p) <br/>{</P> <p> push (S, P ); <br/> P = p-> lchild; <br/>}< br/> else {<br/> POP (S, P ); <br/> printf ("% C", p-> data); <br/> P = p-> rchild; <br/>}< br/>/* <br/> void inordertraverse_nonrecur (bitree T) <br/>{< br/> stack_bitree s; <br/> init_stack_bitree (s); <br/> bitp REE; <br/> P = T; <br/> push (S, P); // The root is first added to the stack <br/> while (! Empty_stack_bitree (s) <br/>{< br/> while (gettop_stack_bitree (S, P) & P) <br/>{< br/> push (S, p-> lchild); <br/> // printf ("Pressure stack 1/N "); // keep pressing to the bottom left subtree <br/>}< br/> POP (S, P ); // return NULL pointer // return left subtree as left child <br/> // printf ("rollback 2/N"); <br/> If (! Empty_stack_bitree (s) <br/>{< br/> POP (S, P); // leave the left child, visit the left child <br/> // printf ("/N"); <br/> printf ("% C", p-> data ); <br/> push (S, P-> rchild); // press the right child <br/> // printf ("Press stack 3/N "); <br/>}< br/>}*/<br/> int main () <br/>{< br/> bitree T; <br/> create_bitree (t); </P> <p> printf ("sequential traversal/N"); <br/> preordertraverse (t ); <br/> printf ("/N"); <br/> printf ("sequential traversal/N"); <br/> inordertraverse (t ); <br/> printf ("/N"); <br/> printf ("post-order traversal/N"); <br/> postordertraverse (t ); <br/> printf ("/N"); <br/> printf ("hierarchical traversal/N"); <br/> layordertraverse (t ); </P> <p> printf ("/N"); <br/> // printf ("% C/N", T-> data ); <br/> printf ("number of leaves in the tree % d/N", leaf_bitree (t); <br/> printf ("depth of the tree % d/N ", depth_bitree (t); <br/> printf ("first-order traversal of non-recursive/N"); <br/> // preordertraverse_nonrecur (t ); <br/> printf ("non-recursive traversal in the middle order/N"); <br/> inordertraverse_nonrecur (t); <br/> system ("pause "); <br/>}< br/> // ABCD e FG Hi <br/>

 

Haha, then add the non-Recursive Algorithms for subsequent traversal. In some of the previously seen post-Order Non-recursive algorithms, we found that we had to reconstruct the tree and node structure, so we stopped, the code is easy to find.

Void postordertraverse (bitree & T) <br/>{< br/> bitree P = T; <br/> stack_bitree s; <br/> init_stack_bitree (s ); <br/> bitree pre; <br/> while (P! = NULL) |! Empty_stack_bitree (s) <br/>{< br/> If (P! = NULL) <br/>{< br/> push (S, P); <br/> P = p-> lchild; <br/>}< br/> else {<br/> P = gettop_stack_bitree (s); <br/> If (P-> rchild! = NULL) & (pre! = P-> rchild) <br/>{< br/> P = p-> rchild; <br/>}< br/> else {<br/> P = pre = gettop_stack_bitree (s); <br/> printf ("% C ", p-> data); <br/> POP (S, P); <br/> P = NULL; <br/>}< br/>}

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.