-
Notifications
You must be signed in to change notification settings - Fork 6
/
Bottom view of the binary tree
214 lines (211 loc) · 3.93 KB
/
Bottom view of the binary tree
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include<stdio.h>
#include<stdlib.h>
//// Hash map implemention starts here
struct Hash
{
int key;
int value;
struct Hash *left,*right;
int height;
};
struct Hash *map = NULL;
int height(struct Hash *root)
{
if(root)
return root->height;
return -1;
}
int max(int a, int b)
{
return a>b?a:b;
}
void setHeight(struct Hash *root)
{
root->height=max(height(root->left),height(root->right))+1;
}
struct Hash * rotateLeft(struct Hash *root)
{
struct Hash *newroot = root->left;
root->left=newroot->right;
newroot->right=root;
setHeight(newroot);
setHeight(root);
return newroot;
}
struct Hash * rotateRight(struct Hash *root)
{
struct Hash *newroot=root->right;
root->right=newroot->left;
newroot->left=root;
setHeight(newroot);
setHeight(root);
return newroot;
}
struct Hash * doubleRotateLeft(struct Hash *root)
{
root->left = rotateRight(root->left);
return rotateLeft(root);
}
struct Hash * doubleRotateRight(struct Hash *root)
{
root->right =rotateLeft(root->right);
return rotateRight(root);
}
struct Hash *newNode(int value,int key)
{
struct Hash *temp = (struct Hash*)malloc(sizeof(struct Hash));
temp->value=value;
temp->key=key;
temp->left=temp->right=NULL;
temp->height=0;
return temp;
}
struct Hash * insert(struct Hash *root, int value, int key)
{
if(root == NULL)
{
root = newNode(value,key);
}
else if(key<root->key)
{
root->left= insert(root->left,value,key);
if(height(root->left)-height(root->right)==2)
{
if(value<root->left->value)
root = rotateLeft(root);
else
root = doubleRotateLeft(root);
}
}
else if(key > root->key)
{
root->right= insert(root->right,value,key);
if(height(root->right)-height(root->left)==2)
{
if(value > root->right->value)
root = rotateRight(root);
else
root = doubleRotateRight(root);
}
}
else
{
root->value=value;
}
setHeight(root);
return root;
}
void inOrder(struct Hash *root)
{
if(root != NULL)
{
inOrder(root->left);
printf("key = %d : value = %d\n",root->key,root->value);
inOrder(root->right);
}
}
struct Hash * search(struct Hash *root,int key)
{
if(root == NULL)
return root;
else if(root->key == key)
return root;
struct Hash *found = search(root->left,key);
if(found)
return found;
search(root->right,key);
}
void get(int key)
{
struct Hash * temp = search(map,key);
if(temp)
printf("\nKey : %d value : %d",key,temp->value);
else
printf("\nException Occured :P");
}
void put(int key,int value)
{
map = insert(map,value,key);
}
void hasKey(int key)
{
struct Hash * temp = search(map,key);
if(temp)
printf("\nKey:%d Found",key);
else
printf("\nkey:%d Not Found",key);
}
void printMap()
{
inOrder(map);
}
//// Hash map implemention ends here
struct Tree
{
int data;
struct Tree *left,*right;
};
struct Tree *newTreeNode(int data)
{
struct Tree *temp=(struct Tree*)malloc(sizeof(struct Tree));
temp->data=data;
temp->left=temp->right=NULL;
}
void preOrder(struct Tree *root)
{
if(root != NULL)
{
printf("%d\n",root->data);
preOrder(root->left);
preOrder(root->right);
}
}
struct Tree *createTree(struct Tree *root, int data)
{
if(root == NULL)
{
return newTreeNode(data);
}
else if(data < root->data)
{
root->left=createTree(root->left,data);
}
else if(data > root->data)
{
root->right=createTree(root->right,data);
}
return root;
}
void BVT(struct Tree *root,int key)
{
if(root != NULL)
{
//printf("%d\n",root->data);
put(key,root->data);
BVT(root->left,key-1);
BVT(root->right,key+1);
}
}
void bottomViewTree(struct Tree *root)
{
BVT(root,0);
if(map == NULL)
return;
printf("\t***Bottom View of Tree***\n");
printMap(map);
}
void main()
{
struct Tree *root = NULL;
//int input1[] = {10,20,30,40,50,60,70,80,90};
//int input1[] = {20,30,60,90,80,100,70,35,40};
//int input1[] = {90,80,70,60,50,40,30,20,10};
int input1[] = {20,8,22,5,3,4,25,10,14,28};
int *input = &input1[0];
int i,len = 9;
for(i=0;i<len;i++)
{
root = createTree(root,input[i]);
}
bottomViewTree(root);
}