-
Notifications
You must be signed in to change notification settings - Fork 0
/
splayHeap.c
147 lines (127 loc) · 2.74 KB
/
splayHeap.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
#include<stdio.h>
#include<stdlib.h>
struct SplayNode
{
int value;
struct SplayNode* left;
struct SplayNode* right;
};
struct SplayNode* createNode(int value)
{
struct SplayNode* newNode = (struct SplayNode*)malloc(sizeof(struct SplayNode));
newNode->value = value;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
struct SplayNode* rightRotate(struct SplayNode* node)
{
struct SplayNode* newRoot = node->left;
node->left = newRoot->right;
newRoot->right = node;
return newRoot;
}
struct SplayNode* leftRotate(struct SplayNode* node)
{
struct SplayNode* newRoot = node->right;
node->right = newRoot->left;
newRoot->left = node;
return newRoot;
}
struct SplayNode* splay(struct SplayNode* root,int value)
{
if(root == NULL || root->value == value)
{
return root;
}
if(value < root->value)
{
if(root->left == NULL)
return root;
//root->left = splay(root->left,value);
//root = rightRotate(root);
if(value < root->left->value)
{
root->left->left = splay(root->left->left,value);
root = rightRotate(root);
}
else if(value > root->left->value)
{
root->left->right = splay(root->left->right,value);
if(root->left->right != NULL){
root->left = leftRotate(root->left);
}
}
return (root->left == NULL) ? root : rightRotate(root);
}
else
{
if(root->right == NULL)
{
return root;
}
if(value > root->right->value)
{
root->right->right = splay(root->right->right,value);
root = leftRotate(root);
}
else if(value < root->right->value)
{
root->right->left = splay(root->right->left,value);
if(root->right->left != NULL)
{
root->right = rightRotate(root->right);
}
}
return (root->right == NULL) ? root : leftRotate(root);
}
}
struct SplayNode* insert(struct SplayNode* root,int value)
{
if(root == NULL)
{
return createNode(value);
}
root = splay(root,value);
if(root->value == value)
{
return root;
}
struct SplayNode* newNode = createNode(value);
if(value < root->value)
{
newNode->right = root;
newNode->left = root->left;
root->left = NULL;
}
else
{
newNode->left = root;
newNode->right = root->right;
root->right = NULL;
}
return newNode;
}
void inOrderTraversal(struct SplayNode* root)
{
if(root != NULL)
{
inOrderTraversal(root->left);
printf("%d ",root->value);
inOrderTraversal(root->right);
}
}
int main()
{
struct SplayNode* root = NULL;
root = insert(root,5);
root = insert(root,15);
root = insert(root,32);
root = insert(root,24);
root = insert(root,8);
root = insert(root,1);
printf("In-Order traversal: \n");
inOrderTraversal(root);
printf("\n");
return 0;
}