Clue Two fork Tree algorithm

Source: Internet
Author: User
Tags thread

#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
typedef char datatype;/* Definition DataType Type * *
typedef enum {Link,thread}pointertag;
typedef struct node{
DataType data;
struct node *lchild, *rchild;/* left and right child subtree *
Pointertag Ltag,rtag;
}bithrnode; /* Node Type * *
typedef bithrnode *BITHRTREE///two fork tree type * *
void Creatbintree (Bithrtree *t)
{/* Constructs two forks list, note: The input sequence is the first order sequence * *
Char ch;
if ((Ch=getchar ()) = = ")
*t=null;
else{/* Read non-space * *
*t= (Bithrnode *) malloc (sizeof (Bithrnode));/* Generate node * *
(*t)->data=ch; (*t)->ltag=link; (*t)->rtag=link;
Creatbintree (& (*t)->lchild); /* Constructed left subtree * *
Creatbintree (& (*t)->rchild); /* Construct right subtree * *
}
}
Bithrtree pre;/* global Variable * *
void Inthreading (Bithrtree p)
{
if (p)
{inthreading (P->lchild);//* Zoozi tree Cue * *
if (!p->lchild) {p->ltag=thread;p->lchild=pre;} * Precursor leads/*
if (!pre->rchild) {pre->rtag=thread;pre->rchild=p;} * Follow-up leads/*
pre=p;/* keep the pre pointing to p*/
Inthreading (p->rchild)/* Right subtree cue/
}
}
int inorderthreading (Bithrtree *thrt,bithrtree T)
/* In the sequence Shing fork tree T, and will be the sequence of clues, Thrt point to the head node * *
{if (!) ( *thrt= (bithrtree) malloc (sizeof (Bithrnode))) exit (0);
(*thrt)->ltag=link; (*thrt)->rtag=thread;/* Building head Node * *
(*thrt)->rchild=*thrt;/* right pointer anaphora/
if (! T) (*thrt)->lchild=*thrt;
Else
{(*THRT)->lchild=t;pre=*thrt;
Inthreading (T);//* Middle sequence traversal in order to lead
pre->rchild=*thrt;pre->rtag=thread;/* the last knot of the clue.
(*THRT)->rchild=pre;
}
return 1;
}
int print (Bithrtree e)
{printf ("%d%c%d\n", E->ltag,e->data,e->rtag); return 1;}

int Inordertraverse (Bithrtree t,int (* visit) (Bithrtree e))
/*t point to the head node, the left chain of the head node Lchild point to the root node, in the sequence of Shing fork Tree * *
{Bithrtree p;
P=t->lchild;/*p point to root node * *
while (p!=t)/* Empty tree or at the end of the p==t*/
{while (P->ltag==link) p=p->lchild;
if (!visit (p)) return 0;/* Print * *
while (p->rtag==thread&&p->rchild!=t)
{p=p->rchild;visit (P);} * * Access to subsequent nodes * *
p=p->rchild;
}
return 1;
}
void Main ()
{/* Test procedure * *
Bithrtree T,thrt;
Creatbintree (&t);
Inorderthreading (&thrt,t);
Inordertraverse (Thrt,print);
}
/* Can be entered "-+a *b-c d/e F" To test (note the space) * *

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.