-
Notifications
You must be signed in to change notification settings - Fork 6
/
HashMap in C
164 lines (160 loc) · 2.86 KB
/
HashMap in 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
#include<stdio.h>
#include<stdlib.h>
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);
}
void main()
{
put(60,600);
put(80,800);
put(65,650);
put(90,900);
put(100,1000);
put(80,8);
put(70,700);
put(20,200);
put(30,300);
put(60,6);
printf("\t***HASH MAP***\n");
printMap();
hasKey(70);
hasKey(35);
get(80);
get(85);
printf("\n");
}