"Data structure" clue two fork Tree

Source: Internet
Author: User

Clue two fork tree: pointing to the precursor and subsequent pointers called Clues, plus a clue to the two-linked list called the Clue list, the corresponding two-fork tree is called the Clue Two fork tree (threaded binary trees).

Clue: The two-fork tree is traversed in some order to turn it into a clue two the fork tree is called a clue. The process of the clue is the process of modifying the null pointer during the traversal.

Code:

#include "string.h" #include "stdio.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h" #defin E OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 100/* Storage space initial allocation */typedef int status;/* Status is the type of the function, its value is the function result status code, such as OK etc. */typedef char telemtype;typedef enum {link,thread} pointertag;/* link==0 means pointing to the left and right child pointer, *//* thread== 1 indicates a precursor or successor */typedef struct bithrnode/* two fork clue storage node Structure */{telemtype data;/* node data */struct bithrnode *lchild, *rchild;/* around Child pointer */pointertag Ltag; Pointertag rtag;/* flag */} Bithrnode, *bithrtree; Telemtype nil= ' # '; /* Character type is empty with space */status visit (Telemtype e) {printf ("%c", e); return OK;} /* In order to enter the value of the node in the binary clue tree, construct a two-fork clue Tree T *//* 0 (int)/space (character type) to indicate empty junction */status Createbithrtree (Bithrtree *t) {telemtype h;scanf ("%c", &H), if (H==nil) *t=null;else{*t= (bithrtree) malloc (sizeof (Bithrnode)), if (!*t) exit (OVERFLOW);(*t)->data=h; /* Generate root node (pre-order) */createbithrtree (& (*t)->lchild); /* Recursive construction left subtree */if ((*t)->lchild)/* with Left child */(*T)->ltag=link; Createbithrtree (&(*t)->rchild); /* Recursive construction Right subtree */if ((*t)->rchild)/* have right child */(*T)->rtag=link;} return OK;} Bithrtree Pre; /* global variable, always point to the node you have just visited *//* the sequence traversal for the middle order thread */void inthreading (Bithrtree p) {if (p) {inthreading (p->lchild);/* Recursive left subtree thread */if  (!p->lchild)/* No left child */{p->ltag=thread;//precursor thread */p->lchild=pre;//left child pointer pointing precursor */}if (!pre->rchild)/* Precursor no right child */{pre->rtag=thread;/* Follow-up clue */pre->rchild=p;/* Precursor right child pointer pointing to successor (current node p) */}pre=p; /* Keep pre */inthreading (p->rchild) pointing to P; /* Recursive right subtree */}}/* sequence traversal of the binary tree T, and the sequence is threaded, thrt points to the head node */status inorderthreading (Bithrtree *thrt,bithrtree T) {*thrt= ( Bithrtree) malloc (sizeof (Bithrnode)), if (!*thrt) exit (OVERFLOW);(*thrt)->ltag=link; /* Build head Node */(*thrt)->rtag=thread; (*thrt)->rchild= (*THRT); /* Right pointer back to */IF (! T)/* If the binary tree is empty, then the left pointer *thrt */()->lchild=*thrt;else{(*thrt)->lchild=t;pre= (*THRT); inthreading (t);/* Middle sequence traversal for middle order thread * /pre->rchild=*thrt;pre->rtag=thread; /* Last node Clue */(*THRT)->rchild=pre;} return OK;} /* Non-recursive algorithm for sequential traversal of binary clue Tree T (head node) */status InordertRaverse_thr (Bithrtree T) {bithrtree p;p=t->lchild;/* p points to root node */while (p!=t) {/* empty tree or traversal end, p==t */while (p->ltag== Link) p=p->lchild;if (!visit (P->data))/* Access the node whose left subtree is empty */return error;while (p->rtag==thread&&p-> rchild!=t) {p=p->rchild;visit (p->data);/* Access subsequent node */}p=p->rchild;} return OK;} int main () {Bithrtree h,t;printf ("Please enter the binary tree in the preamble (e.g. ' abdh# #I # #EJ # # #CF # #G # # ') \ n"); Createbithrtree (&t); /* Generate two Fork Tree */inorderthreading (&h,t) according to the preamble; /* Middle sequence traversal, and middle sequence threaded binary tree */printf ("Middle sequence traversal (output) binary clue tree: \ n"); Inordertraverse_thr (H); /* Middle sequence traversal (output) binary clue tree */printf ("\ n"); return 0;}


Results:

Scope of application:

If the two-fork tree used to traverse or find nodes often requires a precursor and successor in some kind of traversal sequence, then the storage structure with the clue two-linked list will be a good choice.

"Data structure" 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.