-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlot.c
200 lines (178 loc) · 3.63 KB
/
lot.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
// level order traversal of a tree
//also, give the level of a tree for a given node
#include <stdio.h>
#include<stdlib.h>
struct treenode {
int d;
struct treenode *l;
struct treenode *r;
};
struct queue {
struct treenode *data;
struct queue *next;
};
struct queue *front = NULL;
struct queue *rear = NULL;
struct treenode *newTreeNode(int d) {
struct treenode *new = malloc(sizeof(struct treenode));
new->d = d;
new->l = NULL;
new->r = NULL;
return new;
}
struct queue *newQueueNode() {
struct queue *new = malloc(sizeof(struct queue));
new->data = NULL;
new->next = NULL;
return new;
}
struct treenode *AddToTree(struct treenode **root, int d) {
struct treenode *temp = *root;
struct treenode *new = newTreeNode(d);
if(temp == NULL) {
*root = new;
return *root;
}
else {
if(d <= temp->d) {
temp->l = AddToTree(&(temp->l), d);
}
else if(d > temp->d) {
temp->r = AddToTree(&(temp->r), d);
}
return temp;
}
}
void inOrderTree(struct treenode *root) {
if(root == NULL) {
return;
}
else {
inOrderTree(root->l);
printf("%d ", root->d);
inOrderTree(root->r);
}
}
//returns 1 if queue is empty else return 0
int isEmptyQueue() {
return (front == NULL && rear == NULL);
}
void PrintQueue() {
struct queue *front1 = front;
struct queue *rear1 = rear;
while(front1 != rear1->next) {
printf("%d\n", front1->data->d);
front1 = front1->next;
}
}
void Enqueue(struct treenode *new) {
//struct queue *front = *f;
//struct queue *rear = *r;
//struct treenode *root = *new;
struct queue *newNode = newQueueNode();
if(isEmptyQueue() != 0) { //empty case
rear = newNode;
rear->data = new;
rear->next = NULL;
front = rear;
//*r = rear;
//*f = *r;
}
else { //queue has some elements
rear->next = newNode;
rear->next->data = new;
rear->next->next = NULL;
rear = rear->next;
//*r = rear->next;
}
}
struct treenode *Dequeue() {
struct queue *temp = front;
struct queue *temp2 = front;
struct treenode *t = temp2->data;
//struct queue *rear = rear;
if(isEmptyQueue() != 0) {
printf("Queue underflow\n");
return NULL;
}
else {
temp = temp->next;
if(temp == NULL) {
front = temp;
rear = front;
}
else {
front = temp;
}
free(temp2);
}
return t;
}
void LevelOrderTraversal(struct treenode *root) {
//struct queue *front = *f;
//struct queue *rear = *r;
struct treenode *temp = NULL;
Enqueue(root);
while(!isEmptyQueue()) {
printf("In while loop\n");
temp = Dequeue();
printf("%d\n", temp->d);
if(temp->l != NULL) {
printf("Enqueing left child\n");
Enqueue(temp->l);
}
if(temp->r != NULL) {
printf("Enqueing right child\n");
Enqueue(temp->r);
}
}
}
int GetHeight(struct treenode *root) {
struct treenode *temp = NULL;
int level = 0;
Enqueue(root);
Enqueue(NULL);
while(!isEmptyQueue()) {
temp = Dequeue();
if(temp == NULL) {
if(!isEmptyQueue()) {
Enqueue(NULL);
}
level++;
}
else {
if(temp->l != NULL) {
Enqueue(temp->l);
}
if(temp->r != NULL) {
Enqueue(temp->r);
}
}
}
return level;
}
int main(void) {
struct treenode *root = NULL;
AddToTree(&root, 6);
AddToTree(&root, 4);
AddToTree(&root, 1);
AddToTree(&root, 2);
AddToTree(&root, 3);
AddToTree(&root, 5);
//prints the inorder traversal of the tree
//inOrderTree(root);
//printf("\n");
//printf("Queue is : %d", isEmptyQueue(front, rear));
//LevelOrderTraversal(root);
int h = GetHeight(root);
printf("Height of tree is : %d\n",h);
/*Enqueue(root);
//printf("\n%d\n", (Dequeue())->d);
Dequeue();
Enqueue((root->l));
Enqueue((root->l->l));
Enqueue((root->l->r));
Dequeue();
PrintQueue(); */
return 0;
}