-
Notifications
You must be signed in to change notification settings - Fork 1
/
DataStructure_TreeByLeftmostchild-RightSibling.cpp
170 lines (157 loc) · 3.12 KB
/
DataStructure_TreeByLeftmostchild-RightSibling.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef char NodeType;
typedef struct Node{
NodeType Data;
Node *Leftmostchild;
Node *Rightsibling;
};
Node *Root;
Node *fTmp;
int count=0;
int h=0;
int ld=0;
int rd=0;
Node *MakeNode(){
Node *Tmp;
Tmp=(Node*)malloc(sizeof(Node));
if(Tmp==NULL) exit(1);
else{
Tmp->Leftmostchild=NULL;
Tmp->Rightsibling=NULL;
}
return Tmp;
}
void InOrderTraversal(Node *Root){
if(Root->Leftmostchild==NULL){
printf("%5c",Root->Data);
count++;
}
else{
Node *Tmp;
Tmp=Root->Leftmostchild;
InOrderTraversal(Tmp);
printf("%5c",Root->Data);
while(Tmp->Rightsibling!=NULL){
InOrderTraversal(Tmp->Rightsibling);
Tmp=Tmp->Rightsibling;
}
}
}
void PreOrderTraversal(Node *fRoot){
printf("%5c",fRoot->Data);
if(fRoot->Leftmostchild==NULL) count++;
if(fRoot->Leftmostchild!=NULL){
Node *Temp;
Temp=fRoot->Leftmostchild;
while(Temp!=NULL){
PreOrderTraversal(Temp);
Temp=Temp->Rightsibling;
}
}
}
void PostOrderTraversal(Node *fRoot){
if(fRoot->Leftmostchild==NULL) count++;
if(fRoot->Leftmostchild!=NULL){
Node *Temp;
Temp=fRoot->Leftmostchild;
while(Temp!=NULL){
PostOrderTraversal(Temp);
Temp=Temp->Rightsibling;
}
}
printf("%5c",fRoot->Data);
}
int Height(Node *fRoot){
if(fRoot==NULL) return 0;
if(fRoot->Leftmostchild==NULL) return 1;
else{
Node *Tmp1;
Tmp1=fRoot->Leftmostchild;
ld=Height(Tmp1);
if(Tmp1->Rightsibling!=NULL){
Node *Tmp2;
Tmp2=Tmp1->Rightsibling;
rd=Height(Tmp2);
}
}
if(ld>=rd) return 1+ld;
else return 1+rd;
}
Node *InOrderFind(NodeType ch,Node *fRoot){
if(fRoot->Leftmostchild==NULL){
if(fRoot->Data==ch) fTmp=fRoot;
}
else{
Node *Tmp;
Tmp=fRoot->Leftmostchild;
fTmp=InOrderFind(ch,Tmp);
if(fRoot->Data==ch) fTmp=fRoot;
while(Tmp->Rightsibling!=NULL){
fTmp=InOrderFind(ch,Tmp->Rightsibling);
Tmp=Tmp->Rightsibling;
}
}
return fTmp;
}
void LeftMostchild(NodeType ch1,NodeType ch2){
//ch1 la cha, ch2 la con
Node *Tmp;
Tmp=InOrderFind(ch1,Root);
Node *Newnode;
Newnode=MakeNode();
Newnode->Data=ch2;
Newnode->Rightsibling=NULL;
Newnode->Leftmostchild=NULL;
Tmp->Leftmostchild=Newnode;
}
void RightSibling(NodeType ch1,NodeType ch2){
//ch1 ben trai ch2
Node *Tmp;
Tmp=InOrderFind(ch1,Root);
Node *NewNode;
NewNode=MakeNode();
NewNode->Data=ch2;
NewNode->Leftmostchild=NULL;
NewNode->Rightsibling=NULL;
Tmp->Rightsibling=NewNode;
}
Node *MakeRoot(NodeType ch){
Root=MakeNode();
Root->Data=ch;
return Root;
}
int main(){
MakeRoot('a');
LeftMostchild('a','b');
fTmp=NULL;
LeftMostchild('b','c');
fTmp=NULL;
LeftMostchild('c','d');
fTmp=NULL;
RightSibling('c','k');
fTmp=NULL;
RightSibling('b','e');
fTmp=NULL;
LeftMostchild('e','h');
fTmp=NULL;
RightSibling('h','g');
fTmp=NULL;
LeftMostchild('g','l');
fTmp=NULL;
LeftMostchild('l','m');
fTmp=NULL;
RightSibling('l','n');
printf("\nINORDER TRAVERSAL: ");
InOrderTraversal(Root);
count=0;
printf("\nPOSTORDER TRAVERSAL:");
PostOrderTraversal(Root);
count=0;
printf("\nPREORDER TRAVERSAL: ");
PreOrderTraversal(Root);
printf("\nNumber Leaves: %d",count);
h=Height(Root);
printf("\nHeight of Tree: %d",h);
}