-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDataStructure_BinaryTree.cpp
110 lines (98 loc) · 2.43 KB
/
DataStructure_BinaryTree.cpp
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef int DataType;
typedef struct NodeType{
DataType Data;
NodeType *Leftchild;
NodeType *Rightchild;
};
NodeType *Root;
NodeType *fTmp;
int ld = 0;
int rd = 0;
NodeType *MakeNode(){
NodeType *Temp;
Temp=(NodeType*) malloc(sizeof(NodeType));
if(Temp==NULL) exit(1);
else{
Temp->Leftchild=NULL;
Temp->Rightchild=NULL;
}
return Temp;
}
NodeType *PreOrderFind(NodeType *fRoot,DataType ch){
if(fRoot->Data==ch) fTmp=fRoot;
else{
if(fRoot->Leftchild!=NULL) fTmp=PreOrderFind(fRoot->Leftchild,ch);
if(fRoot->Rightchild!=NULL) fTmp=PreOrderFind(fRoot->Rightchild,ch);
}
return fTmp;
}
void PreOrderTraversal(NodeType *fRoot){
printf(" %c ",fRoot->Data);
if(fRoot->Leftchild!=NULL) PreOrderTraversal(fRoot->Leftchild);
if(fRoot->Rightchild!=NULL) PreOrderTraversal(fRoot->Rightchild);
}
void InOrderTraversal(NodeType *fRoot){
if(fRoot->Leftchild!=NULL) InOrderTraversal(fRoot->Leftchild);
printf(" %c ",fRoot->Data);
if(fRoot->Rightchild!=NULL) InOrderTraversal(fRoot->Rightchild);
}
void PostOrderTraversal(NodeType *fRoot){
if(fRoot->Leftchild!=NULL) PostOrderTraversal(fRoot->Leftchild);
if(fRoot->Rightchild!=NULL) PostOrderTraversal(fRoot->Rightchild);
printf(" %c ",fRoot->Data);
}
void LeftChild(DataType ch1,DataType ch2){
NodeType *Tmp1;
NodeType *Tmp2;
Tmp2=MakeNode();
Tmp1=PreOrderFind(Root,ch1);
if(Tmp1==NULL) exit(1);
else{
Tmp1->Leftchild=Tmp2;
Tmp2->Data=ch2;
}
}
void MakeRoot(DataType ch){
Root=MakeNode();
Root->Data=ch;
}
void RightChild(DataType ch1,DataType ch2){
NodeType *Tmp1;
NodeType *Tmp2;
Tmp2=MakeNode();
Tmp1=PreOrderFind(Root,ch1);
if(Tmp1==NULL) exit(1);
else{
Tmp1->Rightchild=Tmp2;
Tmp2->Data=ch2;
}
}
int Height(NodeType *fRoot){
if(fRoot == NULL) return 0;
int ld = Height(fRoot->Leftchild);
int rd = Height(fRoot->Rightchild);
return ld > rd ? (ld+1):(rd+1);
}
int main(){
MakeRoot('a');
LeftChild('a','b');fTmp=NULL;
RightChild('a','c');fTmp=NULL;
LeftChild('b','d');fTmp=NULL;
RightChild('b','e');fTmp=NULL;
RightChild('d','j');fTmp=NULL;
LeftChild('d','h');fTmp=NULL;
LeftChild('e','k');fTmp=NULL;
RightChild('e','l');fTmp=NULL;
LeftChild('c','f');fTmp=NULL;
RightChild('c','g');fTmp=NULL;
printf("PreOrderTraversal: ");
PreOrderTraversal(Root);
printf("\nInOrderTraversal: ");
InOrderTraversal(Root);
printf("\nPostOrderTraversal: ");
PostOrderTraversal(Root);
printf("\nHeight: %d",Height(Root));
}