6.3 clue binary tree (clue-Based Binary Tree)

Source: Internet
Author: User

6.3 clue binary tree (clue-Based Binary Tree)

Problem Introduction: When the binary linked list is used as the storage structure, only the left and right Child Information of the node can be obtained, and the information of the front and back ends cannot be obtained.

Solution: generate Binary Tree clues.

Implementation Principle: A binary tree with N nodes has n + 1 null pointer domain. These NULL pointer domains are used to store the frontend and successor information of the node.

Essence: the essence of clues is to change the NULL pointer in the binary linked list to the leading and successor clues.


(1) Storage representation of Binary Trees

Enum {link, thread}; // link = 0; thread = 1; typedef struct bintree {char data; struct bintree * lchild, * rchild; int ltag, rtag; // flag domain. Indicator pointer or clue} binode, * bitree;


(2) create a binary tree

See section 6.1 create a binary tree.


(3) sequencing and traversal (note the content of comments)

Bitree inorder_threading (bitree thrt, bitree t) {thrt = (bitree) malloc (LEN); // create a header pointer if (! Thrt) Exit (0); thrt-> rtag = thread; // The right pointer refers to thrt-> rchild = thrt; thrt-> ltag = link; If (! T) // If the binary tree is null, the left pointer refers to thrt-> lchild = thrt; else {thrt-> lchild = T; Pre = thrt; in_threading (t ); // perform the Middle-order traversal to pre-> rtag = thread; // pre-> rchild = thrt; thrt-> rchild = pre ;} return thrt;} void in_threading (bitree p) {If (p) {in_threading (p-> lchild); // left subtree (if (! P-> lchild) // lead {P-> ltag = thread; P-> lchild = pre;} If (! Pre-> rchild) // subsequent clues {pre-> rtag = thread; pre-> rchild = P;} Pre = P; // keep the pre pointing to the P precursor in_threading (p-> rchild); // right subtree clue }}
Void inorder_traverse (bitree thrt) {bitree P = thrt-> lchild; while (P! = Thrt) // when the tree is null or the traversal ends, P = thrt {While (p-> ltag = link) {P = p-> lchild ;} printf ("% C", p-> data); If (p-> rtag = thread & P-> rchild! = Thrt) {P = p-> rchild; printf ("% C", p-> data);} p = p-> rchild ;}}


(4) sequencing and traversing

BiTree preorder_threading(BiTree thrt,BiTree t){thrt = (BiTree )malloc(len);if(! thrt)exit(0);thrt -> rtag = thread;thrt -> rchild = thrt;thrt -> ltag = link;if(!t)thrt -> lchild = thrt;else{thrt -> lchild = t;pre = thrt;pre_threading(t);pre -> rtag = thread;pre -> rchild = thrt;thrt -> rchild = pre;}return thrt;}void pre_threading(BiTree p){if(p){if(! p->lchild){p -> ltag = thread;p -> lchild = pre;}if(!p -> rchild)p -> rtag = thread;if(pre && pre->rtag == thread)pre -> rchild = p;pre = p;if(p->ltag == link)pre_threading(p -> lchild);if(p->rtag == link)pre_threading(p -> rchild);}}
void preorder_traverse(BiTree thrt){BiTree p = thrt -> lchild;printf("%c ",p->data);while(p->rchild != thrt){if(p->ltag == link)p = p->lchild;elsep = p->rchild;printf("%c ",p->data);}}


(5) Post-order clue and traversal
Bitree postorder_threading (bitree thrt, bitree t) {thrt = (bitree) malloc (LEN); If (! Thrt) Exit (0); thrt-> rtag = thread; thrt-> rchild = thrt; thrt-> ltag = link; If (! T) thrt-> lchild = thrt; else {thrt-> lchild = T; Pre = thrt; post_threading (t); pre-> rtag = thread; pre-> rchild = thrt; thrt-> rchild = pre;} return thrt;} void post_threading (bitree p) {If (p) {post_threading (p-> lchild ); // left subtree (p-> rchild); // right subtree (if (! P-> lchild) {P-> ltag = thread; P-> lchild = pre;} If (! Pre-> rchild) {pre-> rtag = thread; pre-> rchild = P;} Pre = P ;}}
Void postorder_traverse (bitree thrt) {bitree P = thrt; while (p-> ltag = link | p-> rtag = link) // a left child first accesses the left child, and no left child first accesses the right child {While (p-> ltag = link) P = p-> lchild; if (p-> rtag = link) // access the right child P = p-> rchild;} printf ("% C ", p-> data); While (P! = T) // P is not the root node {If (p-> rtag = link) // If P has a sibling's left child {If (pre-> rtag = thread | P = pre-> rchild) // If P is the right or only left child of both parents, P = pre; else {P = pre-> rchild; // The next step is to traverse the first node of access on the right subtree of the parent node in the descending order. While (p-> ltag = link | p-> rtag = link) {While (p-> ltag = link) P = p-> lchild; if (p-> rtag = link) P = p-> rchild ;}} else P = p-> rchild; // P points to the subsequent printf ("% C ", p-> data );}}


(6) Total program source code (. c)

/* Example input: ABC # de # G # F #### */# include <stdio. h> # include <stdlib. h> # define Len (sizeof (binode) # define OK 1 Enum {link, thread}; typedef struct bintree {char data; struct bintree * lchild, * rchild; int ltag, rtag;} binode, * bitree; bitree T, thrt, pre; int bintree_creat (bitree * q); void welcome (bitree T); bitree preorder_threading (bitree thrt, bitree t ); // void pre_threading (bitree P); void preorder_traverse (Bi Tree thrt); bitree inorder_threading (bitree thrt, bitree T); // void in_threading (bitree P); void inorder_traverse (bitree thrt); bitree postorder_threading (bitree thrt, bitree T); // void post_threading (bitree P); void postorder_traverse (bitree thrt); int main () {printf ("now create the bintree first, please input: \ n "); bintree_creat (& T); Welcome (t); Return 0;} int bintree_creat (bitree * q) {char ch; CH = getchar (); If (CH = '#') (* q) = NULL; else {(* q) = (bitree) malloc (LEN); If (* q )) {(* q)-> DATA = CH; (* q)-> ltag = link; (* q)-> rtag = link; bintree_creat (& (* q) -> lchild); bintree_creat (& (* q)-> rchild) ;}} Return OK;} void welcome (bitree t) {int I; printf ("input 1: preorder_threading the bintree. \ n "); printf (" input 2: inorder_threading the bintree. \ n "); printf (" input 3: postorder_threading the bintree. \ n "); scanf (" % d ", & I ); Switch (I) {Case 1: thrt = preorder_threading (thrt, T); preorder_traverse (thrt); printf ("\ n"); break; Case 2: thrt = inorder_threading (thrt, T); inorder_traverse (thrt); printf ("\ n"); break; Case 3: thrt = postorder_threading (thrt, t ); postorder_traverse (thrt); printf ("\ n"); break; default: printf ("input error! "); Exit (1) ;}} bitree preorder_threading (bitree thrt, bitree t) {thrt = (bitree) malloc (LEN); If (! Thrt) Exit (0); thrt-> rtag = thread; thrt-> rchild = thrt; thrt-> ltag = link; If (! T) thrt-> lchild = thrt; else {thrt-> lchild = T; Pre = thrt; pre_threading (t); pre-> rtag = thread; pre-> rchild = thrt; thrt-> rchild = pre;} return thrt;} void pre_threading (bitree p) {If (! P-> lchild) {P-> ltag = thread; P-> lchild = pre;} If (! P-> rchild) P-> rtag = thread; If (pre & Pre-> rtag = thread) pre-> rchild = P; Pre = P; if (p-> ltag = link) pre_threading (p-> lchild); If (p-> rtag = link) pre_threading (p-> rchild );}} void preorder_traverse (bitree thrt) {bitree P = thrt-> lchild; printf ("% C", p-> data); While (p-> rchild! = Thrt) {If (p-> ltag = link) P = p-> lchild; elsep = p-> rchild; printf ("% C ", p-> data) ;}} bitree inorder_threading (bitree thrt, bitree t) {thrt = (bitree) malloc (LEN); // create a header pointer if (! Thrt) Exit (0); thrt-> rtag = thread; // The right pointer refers to thrt-> rchild = thrt; thrt-> ltag = link; If (! T) // If the binary tree is null, the left pointer refers to thrt-> lchild = thrt; else {thrt-> lchild = T; Pre = thrt; in_threading (t ); // perform the Middle-order traversal to pre-> rtag = thread; // pre-> rchild = thrt; thrt-> rchild = pre ;} return thrt;} void in_threading (bitree p) {If (p) {in_threading (p-> lchild); // left subtree (if (! P-> lchild) // lead {P-> ltag = thread; P-> lchild = pre;} If (! Pre-> rchild) // subsequent clues {pre-> rtag = thread; pre-> rchild = P;} Pre = P; // keep the pre pointing to the P precursor in_threading (p-> rchild); // right subtree clue} void inorder_traverse (bitree thrt) {bitree P = thrt-> lchild; while (P! = Thrt) // when the tree is null or the traversal ends, P = thrt {While (p-> ltag = link) {P = p-> lchild ;} printf ("% C", p-> data); If (p-> rtag = thread & P-> rchild! = Thrt) {P = p-> rchild; printf ("% C", p-> data);} p = p-> rchild;} bitree postorder_threading (bitree thrt, bitree t) {thrt = (bitree) malloc (LEN); If (! Thrt) Exit (0); thrt-> rtag = thread; thrt-> rchild = thrt; thrt-> ltag = link; If (! T) thrt-> lchild = thrt; else {thrt-> lchild = T; Pre = thrt; post_threading (t); pre-> rtag = thread; pre-> rchild = thrt; thrt-> rchild = pre;} return thrt;} void post_threading (bitree p) {If (p) {post_threading (p-> lchild ); // left subtree (p-> rchild); // right subtree (if (! P-> lchild) {P-> ltag = thread; P-> lchild = pre;} If (! Pre-> rchild) {pre-> rtag = thread; pre-> rchild = P;} Pre = P ;}} void postorder_traverse (bitree thrt) {bitree P = thrt; while (p-> ltag = link | p-> rtag = link) // a left child accesses the left child first, no left child first accesses right child {While (p-> ltag = link) P = p-> lchild; If (p-> rtag = link) // access the right child P = p-> rchild;} printf ("% C", p-> data); While (P! = T) // P is not the root node {If (p-> rtag = link) // If P has a sibling's left child {If (pre-> rtag = thread | P = pre-> rchild) // If P is the right or only left child of both parents, P = pre; else {P = pre-> rchild; // The next step is to traverse the first node of access on the right subtree of the parent node in the descending order. While (p-> ltag = link | p-> rtag = link) {While (p-> ltag = link) P = p-> lchild; if (p-> rtag = link) P = p-> rchild ;}} else P = p-> rchild; // P points to the subsequent printf ("% C ", p-> data );}}

(7) The writing is in a hurry, the expression is simple, and there must be omissions. Thank you for your criticism.






6.3 clue binary tree (clue-Based 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.