The construction and traversal of the clue two fork tree------The data structure and algorithm of the small turtle

Source: Internet
Author: User

#include <stdio.h>#include<stdlib.h>typedefCharElemtype;//Cue store flag bit//Link (0): pointer to left and right child//Thread (1): Indicates a clue to the predecessor's successortypedefenum{Link, Thread} pointertag;typedefstructbithrnode{Chardata; structBithrnode *lchild,*Rchild;    Pointertag Ltag; Pointertag Rtag;} Bithrnode,*Bithrtree;//defines a global variable that represents a node that has just passedBithrtree pre;//Create a binary tree and contract the user to enter the data in the same way as the pre-order traversalvoidCreatebithrtree (Bithrtree *T) {CharC; scanf ("%c",&c); if(' '==c) {        *t =NULL; }Else {        *t = (Bithrnode *)malloc(sizeof(Bithrnode)); (*t)->data =C; (*t)->ltag =Link; (*t)->rtag =Link; Createbithrtree (& (*t)lchild); Createbithrtree (& (*t)rchild); }}//sequential traversal of a threadvoidinthreading (Bithrtree T) {if(t) {inthreading (t->lchild);//recursive left child clue        if( ! T->lchild) {T->ltag =Thread; T->lchild =Pre; }        if(!pre->rchild) {Pre->rtag =Thread; Pre->rchild =T; } Pre=T; Inthreading (T->rchild);//recursive right child lead    }}//initialize a head pointervoidInorderthreading (Bithrtree *p, Bithrtree T) {    *p = (Bithrnode *)malloc(sizeof(Bithrnode)); (*P)->ltag =Link; (*P)->rtag =Link; (*P)->rchild = *p; if(!T) {(*P)->lchild = *p; }Else {        (*P)->lchild =T; Pre= *p;        Inthreading (T); Pre->rchild = *p; Pre->rtag =Thread; (*P)->rchild =Pre; }}voidVisit (Charc) {printf ("%c", c);}//Middle sequence Traversal binary tree, iterativevoidInordertraverse (Bithrtree T) {Bithrtree p; P= t->Lchild;  while(p!=T) {         while(P->ltag = =Link) {P= p->Lchild; } visit (P-data);  while(P->rtag = = Thread && P->rchild! =T) {P= p->Rchild; Visit (P-data); } P= p->Rchild; }}intMain () {Bithrtree p,t=NULL; Createbithrtree (&T); Inorderthreading (&P, T); printf ("The results of the middle sequence traversal of the binary tree are:");    Inordertraverse (P); printf ("\ n"); return 0;}

Run

The construction and traversal of the clue two fork tree------The data structure and algorithm of the small turtle

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.