Data structure---C-language implementation clue two fork tree

Source: Internet
Author: User

Clue two fork tree, here on the basis of the binary tree added a clue//Xin Yang # include <stdio.h> #include <stdlib.h>typedef char elemtype; typedef enum {Link,thread} Childtag;  Link represents the node, thread represents the clue typedef struct bitnode{elemtype data;  struct Bitnode *lchild, *rchild; int Ltag, Rtag;} Bitnode, *bittree;bittree Pre; Create a global variable that represents the node you just visited/* Create a two-fork tree whose input must follow the order of the pre-order traversal. T: Two fork root node arr: The values of each node in the order of the preceding sequence traversal.  No child nodes with a space instead of */void create_tree (Bittree *t, char **arr) {char C; SSCANF (*arr, "%c", &c);  Read in a node value (*arr) + +;    if ("= = = c)//If it is a space, indicates an empty node {*t=null;      } else {*t= (bittree) malloc (sizeof (Bitnode));//Construct new node (*t)->data=c;      (*t)->ltag=link;      (*t)->rtag=link; Create_tree (& (*T)->lchild,arr)///construct a new node for the left Child Create_tree (& (*t)->rchild,arr);//Construct new node right child}}/* Access node information */ void visit (Bittree T) {printf ("| %d | %c | %d |\n ", T->ltag,t->data,t->rtag);}      /* Pre-order traversal accesses the binary tree */void pre_order_traverse (bittree t,int level) {if (t) {visit (T);     Pre_order_traverse (t->lchild,level+1); Pre_order_traverse (t->rchild,level+1);      }}/* traversing a binary tree, */void in_order_threading (Bittree t) {if (t) {in_order_threading (t->lchild);//left child threaded if (!      T->lchild)//If the left child is empty, point it to the direct precursor {t->lchild=pre;    t->ltag=thread; } if (!pre->rchild)//If the right child of the previous node is empty, it points to the direct successor.      (Note: Only access to the next node will be known to the successor of this node.) {pre->rchild=t;    pre->rtag=thread;      } pre=t; In_order_threading (T->rchild); Right child clue}}/* joins a head node to make a two-fork clue tree into a closed ring P: a two-fork tree with a head node. The left child of the head node points to a binary tree T; the right child points to the last leaf node in the T-tree: Two-tree without a head node.  */void in_thread (bittree *p,bittree T) {(*p) = (bittree) malloc (sizeof (Bitnode));//Construct a newly added head node (*p)->ltag=link;  (*p)->rtag=thread;  (*p)->rchild=*p; if (!    T)//If the binary tree is empty, then the child of P points to itself.    {(*p)->lchild=*p;      } else {(*p)->lchild=t;      Pre=*p; In_order_threading (T); The two-fork tree is threaded (*p)->rchild=pre; Point the head node right child to the last leaf knot pre->rtag=thread; Point the last leaf node's right child to the head node.      In this way, the ring is formed.    pre->rchild=*p; }}/* non-recursive mode: Middle sequence traversal twoA fork tree (the tree must have a head node and is already threaded) P: Two-tree with head node */void in_order_traverse (Bittree P) {Bittree T;  t=p->lchild;      while (T!=P)//Determine if the empty tree {while (T->ltag==link)//starts from the left child until the leaf node {t=t->lchild;}      Visit (T); while (T->rtag==thread && t->rchild!=p)//According to the clue, access the successor node.  And the successor node is not the {t=t->rchild; that points to the head node.      Visit (T);}    t=t->rchild;  }}int Main () {Bittree p,t; int level = 1; Represents the depth of the node char *arr= "AB D CE"; Create_tree (&t,&arr) for constructing a two-fork tree with the required nodes (entered as a pre-order traversal method);  Constructs a two-fork tree printf ("Pre_order_traverse: First order traversal: \ n"); Pre_order_traverse (T,level);  Pre-sequence traversal output binary tree printf ("In_order_traverse: Middle sequence traversal: \ n"); In_thread (&p,t); Binary Tree-threaded in_order_traverse (P); Outputs a threaded two-tree return of 0;}




Data structure---C-language implementation 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.