Traverse application of Clue two fork tree

Source: Internet
Author: User

The Traverse of the Clue two fork tree is in the established Clue two fork tree, which is based on clues to find the precursor and successor of the node. Using the precursor and subsequent ideas of finding nodes in the Clue two fork tree, traverse the clue two fork tree.
#include <stdio.h> #include <stdlib.h> #include <malloc.h> #define MAXSIZE typedef char ELEMTYPE  ;  typedef enum {link,/* points to the child node */thread/* points to the precursor or successor */} Pointertag;      typedef struct NODE {elemtype data;      struct Node *lchild;      struct Node *rchild;  Pointertag Ltag,rtag;    }*bitthrtree,bitthrnode;  Bitthrtree Pre; void CreateBitTree2 (Bitthrtree *t,char str[]);//Create Search binary-tree void inthreading (Bitthrtree p);//sequence-threaded binary-tree int inorderthreading (Bitthrtree *thrt,bitthrtree t);//by traversing the binary tree T in the middle order, the sequence of T is threaded. Thrt is a pointer to the head node int inordertraverse (Bitthrtree t,int (*visit) (Bitthrtree e));//sequence traverse thread two cross-tree int Print (bitthrtree T);// Print binary tree nodes and clues bitthrnode* findpoint (Bitthrtree t,elemtype e);//Find the pointer to E in the Clue two fork Tree bitthrnode* inorderpre (bitthrnode *p) bitthrnode* inorderpost (Bitthrnode *p);//middle order successor void Destroybittree (Bitthrtree *t);//Destroy binary tree #include "linkbitree.      H "void CreateBitTree2 (Bitthrtree *t,char str[])//Create search binary tree {char ch; Bitthrtree Stack[maxsize];      int top =-1;      int flag,k;      Bitthrnode *p;      *t = null,k = 0;      ch = str[k];              while (ch! = ') ' {switch (ch) {case ' (': stack[++top] = p;              flag = 1;          Break              Case ') ': top--;          Break              Case ', ': flag = 2;          Break              Default:p = (bitthrtree) malloc (sizeof (Bitthrnode));              P->data = ch;              P->lchild = NULL;              P->rchild = NULL;              if (*t = = NULL) {*t = P;                      } else {switch (flag) {Case 1:                      Stack[top]->lchild = p;                  Break                      Case 2:stack[top]->rchild = p;                  Break        } if (Stack[top]->lchild) {              Stack[top]->ltag = Link;                  } if (stack[top]->rchild) {stack[top]->rtag = Link;      }}} ch = str[++k];          }} void Inthreading (Bitthrtree p)//sequence-threaded binary tree {if (p)//left dial hand tree-threaded {inthreading (p->lchild);              if (!p->lchild)//precursor thread {p->ltag = thread;          P->lchild = pre;              } if (!pre->rchild)//subsequent thread {Pre->rtag = thread;          Pre->rchild = p;          } pre = P; Inthreading (p->rchild);//Right Sub-tree thread}} int inorderthreading (Bitthrtree *thrt,bitthrtree t)//through the middle sequence to traverse the binary tree T, so that the T-sequence clue. Thrt is the pointer to the head node {if (!) ( *thrt = (bitthrtree) malloc (sizeof (Bitthrnode))) {exit (-1);//For Head node allocation unit} (*thrt)->ltag = link;/ /modify precursor cue flag (*thrt)->rtag = thread;//Modify successor Cue flag (*thrt)->rchild = *thrt;//The rchild of the head nodePin point yourself if (!      T)//If the binary tree is empty, then point the lchild pointer to itself {(*thrt)->lchild = *thrt; } else {(*thrt)->lchild = t;//points The left pointer of the head node to the root node pre = *thrt;//points The pre to the already-threaded node Inthread ING (T);//middle sequence traversal for middle order thread/* The last node is threaded */pre->rchild = *thrt;//The right pointer of the last node points to the head node Pre->rtag = T  hread;//modifies the last node's Rtag flag field (*thrt)->rchild = pre;//points The rchild pointer of the head node to the last node} return 1;      } int Inordertraverse (Bitthrtree t,int (*visit) (Bitthrtree e))//middle sequence traverse thread two fork tree {Bitthrtree p;              p = T->lchild;//p points to the root node while (P! = t)//empty tree or traversal ends, p = = T {while (P->ltag = = Link) {          p = p->lchild;          if (!visit (p))//print {return 0;              } while (P->rtag = = Thread && p->rchild! = T)//Access subsequent node {p = p->rchild;          Visit (p);      } p = P->rchild;  } return 1; } int Print (bitthrtreeT)//print binary tree nodes and clue flags {static int k = 1; printf ("%2d\t%s\t%2c\t%s\t\n", K++,t->ltag = = 0? "Link": "Thread", t->data, T->rtag = = 1?      "Thread": "Link");  return 1;      } bitthrnode* findpoint (Bitthrtree t,elemtype E)//finds a pointer to E in the clue two fork tree {Bitthrtree p;      p = t->lchild;          while (P! = T) {while (P->ltag = = Link) {p = p->lchild;          } if (P->data = = e) {return p;              } while (P->rtag = = Thread && p->rchild! = T) {p = p->rchild;              if (P->data = = e) {return p;      }} p = P->rchild;  } return NULL;          } bitthrnode* Inorderpre (Bitthrnode *p)//the sequence precursor {if (P->ltag = = thread)//If the flag field of P is ltag as a clue, then P's left subtree node is the precursor {      return p->lchild; } else {pre = P->lchild;//Find the right lower node of the left child of p while (Pre->rtag = = Link)//Right sub-tree non-empty, along the right chain to look down {pre = Pre->rchild; } return Pre;//pre is the right Bottom node}} bitthrnode* Inorderpost (Bitthrnode *p)//middle order successor {if (P->rtag = = Threa      d)//If the Sign field of P is rtag as a clue, then the right subtree node of p is the rear drive {return p->rchild;          } else {pre = P->rchild;//Find P's right child's leftmost node while (Pre->ltag = = link)//Left dial hand tree is not empty, look down along the left chain          {pre = Pre->lchild; The return pre;//pre is the leftmost lower node}} void Destroybittree (Bitthrtree *t)//destroys the binary tree {if (*t) {if (!          (*t)->lchild) {Destroybittree (& (*t)->lchild); } if (! (          *T)->rchild) {Destroybittree (& (*t)->rchild);          } free (*t);      *t = NULL;      }} #include "LinkBiTree.h" int main (void) {Bitthrtree t,thrt;      Bitthrnode *p,*pre,*post;      CreateBitTree2 (&t, "(A (B (, D (G)), C (E (, H), F))"); printf ("The output sequence of the Clue two fork tree: \ n");      Inorderthreading (&thrt,t);      printf ("Sequence precursor flag node successor sign \ n");      Inordertraverse (Thrt,print);      p = findpoint (Thrt, ' D ');      Pre = Inorderpre (p);      printf ("The direct precursor element of element D is:%c\n", pre->data);      Post = Inorderpost (p);      printf ("The direct post-flooding element of element D is:%c\n", post->data);      p = findpoint (Thrt, ' E ');      Pre = Inorderpre (p);      printf ("The direct precursor element of element E in the preface is:%c\n", pre->data);      Post = Inorderpost (p);      printf ("element e in the direct post-flooding element is:%c\n", post->data);      Destroybittree (&AMP;THRT);  return 0;   }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Traverse application of Clue 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.