Data structure of the Clue two fork tree
enum PointerTag//枚举{ Link,Thread//Link(0):指针;thread(1):线索};struct BiThrNode{ TElemType data;//结点的值 BiThrNode * lchild, *rchild;//左右孩子结点 2;//左标志,占2bit 2;//右标志,占2bit};typedef BiThrNode *BiThrTree;
Basic operation of the Clue two fork tree
voidCreatebithrtree (Bithrtree&T) {//Enter the value of the node in the first order input thread two fork tree, construct the clue two fork tree T. 0 (int)/space (character type) denotes empty nodeTelemtype ch; scanf"%d",&CH);//The value of the input node is empty if(Ch==Nil) T= NULL;Else//Node value is not empty{T=(bithrtree) malloc (sizeof (bithrtree));//Generate root node (first order) if(!T) exit (OVERFLOW); T -Data =Ch//assigns a value to the node of T PointCreatebithrtree (T -Lchild);//Recursive construction of left sub-tree if(T -Lchild)//have left childT -Ltag= Link;//Assign value to left child (pointer)Createbithrtree (T -Rchild);//Recursive construction of right sub-tree if(T -Rchild)//have right childT -Rtag= Link;//Assign value to right child (pointer)}}bithrtree Pre;//global variable that always points to the node you just visitedvoidInthreading (Bithrtree p) {//through the middle sequence traversal to carry on the middle sequence clue, after the clue, the pre points to the last node. if(p)//Clue two tree is not empty{inthreading (P -Lchild);//Recursive left sub-tree thread if(!P -Lchild)//No left child{p -Ltag= Thread;//Left sign as lead (precursor)P -Lchild=Pre//left child pointer pointing to precursor}if(!Or: -Rchild)//Precursor no right child{Pre -Rtag= Thread;//precursor to the right sign for clues (successor)Or: -Lchild=P//Precursor right child pointer pointing to its successor (current node P)} Pre=P//Keep the pre-pointing precursor to PInthreading (P -Rchild);//Recursive right sub-tree thread}}voidInorderthreading (Bithrtree&Thrt, Bithrtree T) {//sequence traversal of the binary tree T, and the sequence of the clue, Thrt point to the head node if(!(Thrt=(bithrtree) malloc (sizeof (Bithrnode))))//Generate head node not successfulExit (OVERFLOW); Thrt -Ltag= Link;//Build head node, left flag as pointerThrt -Rtag= Thread;//Right sign for cluesThrt -Rchild=Thrt;//Right child pointer back finger if(!T//Jou fork Tree T is empty, the left child pointer refers back toThrt -Lchild=Thrt;Else//Two fork tree non-empty{Thrt -Lchild=Tthe left child pointer of the head node points to the root node .Or:=Thrt;the initial value of the//pre (precursor) points to the head nodeInthreading (T);//Middle sequence traversal for middle order thread, pre pointing to the last node in the middle sequence traversalOr: -Rchild=Thrt;///The right child pointer to the last node points to the head nodeOr: -Rtag= Thread;//The right sign of the last node is a clueThrt -Rchild=PreThe right child pointer of the head node points to the last node in the middle sequence traversal}}voidInordertraverse_thr (Bithrtree T,void(*Visit) (Telemtype)) {//The non-recursive algorithm of the middle sequence traverse clue two fork Tree T (head node)Bithrtree p; P=T -Lchild;//p point to root node while(p!=T) {//empty tree or traversal at the end, P==t while(p -Ltag== Link)///Yugen node always finds the leftmost node of a two-fork treeP=P -Lchild;//p pointing to his left childVisit (P -Data);//Access this node while(p -Rtag== Thread &&P -Rchild!=T) {//p->rchild is the Clue (successor) and is not the last node of the TraverseP=P -Rchild;//p pointed to its successorVisit (P -Data);//access to subsequent nodes}//If P->rchild is not a clue (right child), p refers to the right child, return to the loop, find the 1th node in the subtrees tree sequence traversalP=P -Rchild; }}voidPrethreading (Bithrtree p) {recursive function called by//preorderthreading () if(!Pre -Rchild)//p's precursor, no right child.{Pre -Rtag= Thread;//pre's right child pointer for cluesPre -Rchild=Pthe successor of the//p precursor P}if(!P -Lchild)//p no left child{p -Ltag= Thread;//p's left child pointer for cluesP -Lchild=Pre//p left child pointer pointing to precursor} Pre=P//Mobile precursor if(p -Ltag== Link)//p has left childPrethreading (P -Lchild);//To P's left child recursive call prethreading () if(p -Rtag== Link)//p has a right child .Prethreading (P -Rchild);//To P's right child recursive call prethreading ()}voidPreorderthreading (Bithrtree&Thrt, Bithrtree T) {//first-order threaded binary tree T, right child pointer of head node pointing to the last node of the first order traversal if(!(Thrt=(bithrtree) malloc (sizeof (Bithrnode))))//Generate head nodeExit (OVERFLOW); Thrt -Ltag= Link;//Head node left child pointer for childThrt -Rtag= Thread;//Head node right child pointer for clueThrt -Rchild=Thrt;//The right child pointer to the head node points to itself if(!T//Empty treeThrt -Lchild=Thrt;the left child pointer of the head node points to the root node . Else//Non-empty tree{Thrt -Lchild=Tthe left child pointer of the head node points to the root node .Or:=Thrt;//Precursor for head nodePrethreading (T);//Starting from the beginning of the first order recursive leadOr: -Rtag= Thread;///The right child pointer of the last node is a clueOr: -Rchild=Thrt;//The successor of the last node points to the head nodeThrt -Rchild=Pre///The successor of the head node points to the last node .}}voidPreordertraverse_thr (Bithrtree T,void(*Visit) (Telemtype)) {non-recursive algorithm for two-tree T (head node) with sequential traverse clueBithrtree P=T -Lchild;//p point to root node while(p!=T//p does not point to the head node (the successor of the last node of the Traverse points to the head node){Visit (P -Data);//Access root node if(p -Ltag== Link)//p has left childP=P -Lchild;//p Pointing left child (successor) Else//p no left childP=P -Rchild;//p refers to the right child or successor}}voidPostthreading (Bithrtree p) {recursive function called by//postorderthreading () if(p)//p not empty{postthreading (P -Lchild);//To P's left child recursive call postthreading ()Postthreading (P -Rchild);//To P's right child recursive call postthreading () if(!P -Lchild)//p no left child{p -Ltag= Thread;//p's left child pointer for cluesP -Lchild=Pre//p left child pointer pointing to precursor}if(!Or: -Rchild)//p's precursor, no right child.{Pre -Rtag= Thread;//p precursor to right child pointer for cluesOr: -Rchild=Pthe successor of the//p precursor P} Pre=P//Mobile precursor}}voidPostorderthreading (Bithrtree&Thrt, Bithrtree T) {//post-secondary recursive clue binary tree if(!(Thrt=(bithrtree) malloc (sizeof (Bithrnode))))//Generate head nodeExit (OVERFLOW); Thrt -Ltag= Link;//Head node left child pointer for childThrt -Rtag= Thread;//Head node right child pointer for clue if(!T//Empty treeThrt -Lchild=Thrt -Rchild=Thrt;///head node left and right child pointer pointing to itself Else//Non-empty tree{Thrt -Lchild=Thrt -Rchild=Tthe left and right child pointer of the head node points to the root node (the last node) .Or:=Thrt;//Precursor for head nodePostthreading (T);//start of post-order recursive cues if(Pre -Rtag!= Link)//Last node no right child{Pre -Rtag= Thread;///The right child pointer of the last node is a clueOr: -Rchild=Thrt;//The successor of the last node points to the head node} }}voidDestroybitree (Bithrtree&T) {recursive function called by//destroybithrtree, T points to root node ifT//Non-empty tree{if(T -Ltag== 0)//have left childDestroybitree (T -Lchild);//Destroy left sub-tree if(T -Rtag== 0)//have right childDestroybitree (T -Rchild);//Destroy right sub-treeFree (T);//Release root nodeT= NULL;//Null pointer assignment 0}}voidDestroybithrtree (Bithrtree&Thrt) {//Clue two fork Tree Thrt present, destroy Clue two fork tree Thrt if(THRT)//Head node presence{if(Thrt -Lchild)//root node presenceDestroybithrtree (Thrt -Lchild);//Recursive destruction of the head node Lchild two-fork treeFree (THRT);//Release head nodeThrt= NULL;//Clue two fork tree thrt pointer 0}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The representation and implementation of the Clue two fork tree