How to use Recursion to realize clue binary Tree in C language
Most people do not understand the knowledge points of this article "how to use recursion to achieve clue binary tree in C language", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article, let's take a look at this "C language how to use recursion to achieve clue binary tree" article.
Description: point the empty left child pointer domain of the node in the binary tree to the precursor node and the empty right child pointer domain to the successor node.
Code:
# pragma warning (disable:4996) # include#includetypedef struct TreeNode {char data; struct TreeNode * lchild, * rchild; int ltag, rtag;} Tree,*BTree;BTree Build_Tree (void) {BTree T; char ch; scanf ("% c", & ch); if (ch ='#') {T = NULL;} else {T = (BTree) malloc (sizeof (Tree)); T-> data = ch; T-> ltag = 0; T-> rtag = 0; T-> lchild = Build_Tree () T-> rchild = Build_Tree ();} return T;} / / pre-threaded void Pre_Thread (BTree cur, BTree * pre) {if (cur & & cur- > ltag==0) {printf ("% c", cur- > data); if (cur- > lchild = = NULL) {cur- > lchild = * pre; (* pre)-> ltag= 1; cur- > ltag= 1;} if (cur- > rchild = = NULL) {cur- > rtag = 1 } if (* pre & & (* pre)-> rtag = = 1) {(* pre)-> rchild = cur;} * pre = cur; Pre_Thread (cur- > lchild, pre); Pre_Thread (cur- > rchild, pre);}} / / Central sequence void In_Thread (BTree cur, BTree * pre) {if (cur) {In_Thread (cur- > lchild, pre); printf ("% c", cur- > data); if (cur- > lchild==NULL) {cur- > lchild= * pre Cur- > ltag = 1;} if (cur- > rtag = = NULL) {cur- > rtag = 1;} if (* pre & & (* pre)-> rtag = = 1) {(* pre)-> rchild = cur;} * pre = cur; In_Thread (cur- > rchild, pre);}} / / sequenced void Post_Thread (BTree cur, BTree * pre) {if (cur) {Post_Thread (cur- > lchild, pre); Post_Thread (cur- > rchild, pre) Printf ("% c", cur- > data); if (cur- > lchild = = NULL) {cur- > lchild = * pre; cur- > ltag = 1;} if (cur- > rchild = = NULL) {cur- > rtag = 1;} if (* pre & & (* pre)-> rtag = = 1) {(* pre)-> rchild = cur;} * pre = cur;} int main (void) {BTree Tmeans pendant null; T = Build_Tree (); Pre_Thread (T, & p) / / In_Thread (T, & p); / / Post_Thread (T, & p); return 0;} the above is about "how to use recursion to realize clue binary tree in C language". I believe we all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please pay attention to the industry information channel.