Basic operation of the Clue two fork tree

Source: Internet
Author: User

//define data typestypedef enum{link, Thread} pointertag;      //link = 0 means pointing to the left and right child pointer, thread = 1 indicates a precursor or successor to the typedef struct bitnode{     char data;                               //node Data     struct BitNode * Lchild;                 //Kids hands      struct Bitnode *rchild;    pointertag Ltag;                         //flag     pointertag Rtag;} Bitnode, *bitree; bitree Pre;                                  //global variable, always points to the node you just visited   //Pre-order Create two-fork treevoid Createtree (Bitree T) {char ch;    scanf ("%c", &ch);    if (ch = = ' # ') {t= NULL; } else {if (!        t= (bitree) malloc (sizeof (Bitnode))) return 0;        T->data = ch;        Createtree (T->lchild));    Createtree (T->rchild)); } return 0;} ---------------------------------------------------------------------------------------- Middle sequence traverse, get middle sequence clue two fork treeThe THR points to the head node, the head node left chain lchild points to the root node, the head node right chain rchild points to the last node in the middle sequence traversal.    int Inorderthraverse_thr (Bitree T) {Bitree Thr;    thr->ltag=link;    Thr-rtag=thread;    thr->rchild=thr; if (!    T) Thr-lchild=thr;        else{Pre=thr;        Inthreading (T);        pre->rtag=thread;        pre->rchild=thr;    thr->rchild=pre; } return OK; //Middle sequence cluevoid Inthreading (Bitree p) {    if (P)     {         inthreading (P->lchild);              //recursive left sub-tree thread           if (!p->lchild)                        //No left child          {            p->ltag = Thread;                //Precursor Clues              p->lchild = pre;             //left child pointer pointing to the precursor, this is the 3rd step          }        if (!pre->rchild)  &Nbsp;              //No right child          {             Pre->rtag = Thread;              //successor              pre->rchild = p;             //precursor right child pointer pointing to successor (current node P)          }        pre=p;          inthreading (P->rchild);              //Recursive right sub-tree thread      }}---------------------------------------------------------------------------------------- //Find precursor nodes and successor nodes  Finding the middle sequence precursor node of any node in the sequence Clue two fork tree for any node on the middle sequence clue two fork tree, in order to find the precursor node, there are two cases: (1) If the left sign of the node is 1, then the node to which the left pointer field points is its precursor node, (2) if the left flag of the node is 0,     Indicates that the node has a left child, according to the definition of the middle sequence traversal, its precursor node is the most right node of the subtree which is the root node of the left child of the node, that is, along the right-hand chain of its left subtree to look down. When the right sign of a node is 1 o'clock, it is the precursor node to be found.    Bitree Inprenode (Bitree &p) {Bitree pre;    pre=p->lchild;        if (!p->ltag) while (!pre->rtag) {pre=pre->rchild; }} return pre; find the middle sequence successor node of any node in the middle sequence clue two fork treeFor any of the nodes on the middle sequence clue two fork tree, the following two conditions are found for the successive nodes of the sequence: (1) If the right sign of the node is 1, then the node to which the right pointer field points is its successor, (2) If the right sign of the node is 0, indicating that the node has a right child, according to the definition of the middle sequence traversal,     Its precursor node is the leftmost node of the subtree that is the root node of the right child of the node, which is looked down along the left pointer chain of its right subtree. When the left sign of a node is 1 o'clock, it is the successor to be found.    Bitree Inpostnode (Bitree &p) {Bitree post;    post=p->rchild;        if (!p->rtag) while (!post->ltag) {pre=pre->lchild; }} return post;} -------------------------------------------------------------------------------------- //Find a node with a value of x on the middle sequence Clue two fork tree (traverse the Clue two fork tree)      we can traverse all the nodes of the two-fork tree using the algorithm of finding the successor and precursor nodes on the middle sequence clue two fork tree. For example, we first find the first node that is traversed by a sequence, and then query the following sequentially, or find the last node that is traversed by a sequence, and then query its predecessor in turn. This way, you can access all nodes of a two-fork tree without a stack or recursion.      looking for a node with a value of X on the median two tree, essentially traversing the thread two fork tree, writes the operation of the Access node specifically to the statement that the node's value compares to X.  bitree Insearch (Bitree head,chare) {    bitree p;    p=head-> Lchild;    while (P->ltag==0&&p!=head)      p= Head for Traverse end flag         p=p->lchild;                 find the first node in the middle sequence traversal (leftmost child)     while (P->data!=e&&p!=head)      p=head for traverse end flag          inpostnode (P);     if (p->data==e)          return p;    else{         printf ("not Found");  &nbsP;      return 0; } --------------------------------------------------- --------------------------------------  //Insert and delete on the middle sequence thread two tree           Clue Two the update of the fork tree refers to inserting a node in the thread two tree or deleting a node. In general, these operations are likely to destroy the original clues, so when you modify the pointer, you also need to modify the thread accordingly. In general, the cost of this process is almost identical to the re-lead. Here is a simple case where a node p is inserted in the middle sequence cue two tree to make it the right child of node S.   The following two scenarios to analyze:  (1) If the right subtree of S is empty, 6.13 (a) is shown, then inserting the node p becomes the case shown in Figure 6.13 (b). In this case, the successor of      s will be the middle order successor of P, S becomes the middle sequence precursor of P, and P becomes the right child of S. The pointers       and clues in the other parts of the binary tree are not changed.   (2) If the right subtree of S is non-empty, 6.14 (a) is shown, insert node p is shown after 6.14 (b). S original right sub-tree becomes P's right       subtree, because P has no left subtree, so s becomes the middle sequence precursor of P, p becomes the right child of S, and because S original successor becomes P's after       , so we have to point to the successor of S to the left clue, instead pointing to P.    the algorithm for the above operation is given below.  void insertthrright (Bithrtree s,bithrtree p) {       bithrtree w;     p->rchild=s->rchild;    p->rtag=s->rtag;     p->lchild=s;    p->ltag=1;    s->rchild=p;     s->rtag=0;     if (p->rtag==0)     {        w= Inpostnode (P);         w->lchild=p;    }}

Basic operation of the thread two fork 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.