# include & ltstring.h & gt
# include & ltstdlib.h & gt
Struct treenode {/* binary tree structure */
struct treenode * left
struct treenode * right
Character value;
};
The struct nodelist {/* linked list structure is used to traverse the binary tree in non-recursive order */
Struct treenode * node;
struct nodelist * next
};
char * lrd 1 = " deb FCA "; /* Test data followed by */
char * ldr 1 = " DBEAFC "; /* Test data, intermediate order */
Void DLR tree (structtreenode * root)/* Non-recursive preorder traversal of binary tree */
{
struct treenode * node = root
struct nodelist *last = NULL,*tmp = NULL,* ptr
/*
* the idea of non-recursive preorder traversal method, construct a queue (linked list)
* Keep the left and right subtrees of the current node at the end of the queue.
* Traversal pointer moves from the first one of the linked list to the end of the linked list, which is the result of preorder traversal.
*/
Ptr = (structural node list *)malloc(sizeof);
if (ptr == NULL)
Return;
ptr-& gt; Node = root;
ptr-& gt; next = NULL
last = ptr
printf(" DLR tree:");
while(ptr! = empty)
{
node = ptr-& gt; Node;
printf("%c ",node-& gt; Value);
If (node->; Left! = NULL)/* If there is a left subtree, it will be added to the end of the linked list */
{
Tmp = (structural node list *)malloc(sizeof);
if (tmp == NULL)
Return;
Finally-> next = tmp
last = tmp
tmp-& gt; next = NULL
Finally-> node = node-& gt; Left;
}
If (node->; Yes! = NULL)/* If there is a right subtree, it will be added to the end of the linked list */
{
Tmp = (structural node list *)malloc(sizeof);
if (tmp == NULL)
Return;
Finally-> next = tmp
last = tmp
tmp-& gt; next = NULL
Finally-> node = node-& gt; Right;
}
tmp = ptr
ptr = ptr-& gt; Next; /* ptr points to the currently traversed node, and after processing the current node.
After moving, release the memory of the previous node */
Free of charge (TMP);
}
printf(" \ n ");
}
Void replacechar(char *str)/* is used to show binary tree usage */
{
int I;
for(I = 0; I & ltstrlen(str);); i++)
{
if(str[I]= = '+')str[I]= ' | ';
if(str[I]= = ' \ \ ')str[I]= ' ';
if(str[I]= = '-')str[I]= ' ';
}
}
Void display tree (struct treenode * root, char * deepstr)/* graphic display binary tree */
{
int l;
char * ptr
printf("%s%c\n ",deepstr,root-& gt; Value);
l = strlen(deepstr)+4;
ptr =(char *)malloc(l);
strcpy(ptr,deepstr);
replace char(ptr);
if(root-& gt; Left! = NULL & amp& amproot-& gt; Yes! = empty)
{
printf("%s |\n ",ptr);
strcat(ptr,"+-");
Display tree (root->; Left, ptr);
strcpy(ptr,deepstr);
replace char(ptr);
printf("%s |\n ",ptr);
strcat(ptr," \ \-");
Display tree (root->; Right, ptr);
}
else if(root-& gt; Left! = empty)
{
printf("%s |\n ",ptr);
strcat(ptr," \ \-");
Display tree (root->; Left, ptr);
}
else if(root-& gt; Yes! = empty)
{
printf("%s |\n ",ptr);
strcat(ptr," \ \-");
Display tree (root->; Left, ptr);
}
Return;
}
/* Generate binary tree according to post-order and middle-order */
/* lrdlst is the subsequent string */
/* ldrlst is an intermediate sequence string */
struct treenode * create tree(struct treenode * root,char *lrdlst,char *ldrlst)
{
char *llrd,* lldr
char *rlrd,* rldr
int len
int i,k;
len = strlen(lrd lst);
/* Basic idea: get the root node corresponding to the current string, and then generate the subsequent and intermediate order of the left and right subtrees respectively.
String and recursively call */
/* Apply for the string space of the left and right subtrees */
llrd =(char *)malloc(len+ 1);
lldr =(char *)malloc(len+ 1);
rlrd =(char *)malloc(len+ 1);
rldr =(char *)malloc(len+ 1);
/* Exception handling */
if(llrd = = NULL | | lldr = = NULL | | rlrd = = NULL | | rldr = = NULL)
{
If (llrd! = empty)
Free of charge (LLRD);
If (lldr! = empty)
Free of charge (LLDR);
If (rlrd! = empty)
Free (RLRD);
If (rldr! = empty)
Free (rldr);
Returns NULL
}
/* Apply for the root node */
if(root = = NULL & amp; & ampstrlen(ldrlst)>0)
{
root =(struct treenode *)malloc(sizeof(struct treenode));
if (root == NULL)
{
Free of charge (LLRD);
Free of charge (LLDR);
Free (RLRD);
Free (rldr);
Returns NULL
}
}/* This is an error condition. You should just quit */
else if(root = = NULL & amp; & ampstrlen(ldrlst) == 0)
{
Returns NULL
}
/* Root node found */
root-& gt; value = lrd lst[len- 1];
strcpy(lldr,ldr lst);
/* Determine whether the root node has a left subtree */
*strchr(lldr,root->; Value) = 0;
if (strlen(lldr) == 0)
root-& gt; left = NULL
other
{
/* Apply for the space of the left subtree, recursively */
root-& gt; left =(struct treenode *)malloc(sizeof(struct treenode))。
k = 0;
for(I = 0; I & ltleni++)
{
if (strchr(lldr,lrdlst[i])! = empty)
{
llrd[k++]= lrd lst[I];
}
}
llrd[k]= 0;
Create a tree (root->; Left, llrd, lldr);
}
strcpy(rldr,ldr lst);
strcpy(rldr,strchr(rldr,root-& gt; Value)+1);
/* Judge the right subtree */
if (strlen(rldr) == 0)
root-& gt; Right = empty;
other
{
/* Recursive as above */
root-& gt; right =(struct treenode *)malloc(sizeof(struct treenode));
k = 0;
for(I = 0; I & ltleni++)
{
if (strchr(rldr,lrdlst[i])! = empty)
{
rlrd[k++]= lrd lst[I];
}
}
rlrd[k]= 0;
Create a tree (root->; Right, rlrd, rldr);
}
/* Release temporary space */
Free of charge (LLRD);
Free of charge (LLDR);
Free (RLRD);
Free (rldr);
Return to the root directory;
}
int main()
{
struct treenode * r;
r = createtree(NULL,LRD 1,ldr 1); /* Create a tree */
Printf ("tree view \ n"); /* Display tree */
Display tree (r, "");
DLR tree(r); /* Preorder traversal */
Returns 0; ;
}