/
BiTree.c
executable file
·52 lines (52 loc) · 1.27 KB
/
BiTree.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#include<string.h>
#define MaxSize 20
typedef char DataType;
#include"Bitree.h"
int main()
{
int i;
BiTreeNode *q[MaxSize+1];
BiTreeNode *root;
int left,right,temp;
int Num[MaxSize]={0};
char strA[MaxSize]="befcgdh";//pre_order
char strB[MaxSize]="febgchd";//in_order
char point;int n;
n=strlen(strA);
Initiate(&root);
for(i=0;i<n;i++)
{
point=strA[i];
temp=Search(strB,point);
left=SearchLeft(Num,temp);
right=SearchRight(Num,temp);
if(left==-1&&right==-1)
{
q[temp]=InsertLeftNode(root,point);
Num[temp]=1;
}
else if(left!=-1&&q[left]->rightChild==NULL)
{
q[temp]=InsertRightNode(q[left],point);
Num[temp]=1;
}
else if(right!=-1&&q[right]->leftChild==NULL)
{
q[temp]=InsertLeftNode(q[right],point);
Num[temp]=1;
}
}
PrintBiTree(root,0);
printf("****************************************\n\n");
printf("PreOrder: \t");
PreOrder(root->leftChild,Visit);
printf("\nInOrder:\t");
InOrder(root->leftChild,Visit);
printf("\nPostOrder: \t");
PostOrder(root->leftChild,Visit);
printf("\n*****************************************************************************\n\t\t\t\bCopyright @ 2009 Gary All Right Reserved\n");
return 0;
}