-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplay.c
More file actions
153 lines (144 loc) · 3.83 KB
/
splay.c
File metadata and controls
153 lines (144 loc) · 3.83 KB
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
#include <stdio.h>
#include <stdlib.h>
int size; /* number of nodes in the tree */
/* Not actually needed for any of the operations */
typedef struct tree_node Tree;
struct tree_node {
Tree * left, * right;
int item;
};
Tree * splay (int i, Tree * t) {
/* Simple top down splay, not requiring i to be in the tree t. */
/* What it does is described above. */
Tree N, *l, *r, *y;
if (t == NULL) return t;
N.left = N.right = NULL;
l = r = &N;
for (;;) {
if (i < t->item) {
if (t->left == NULL) break;
if (i < t->left->item) {
y = t->left; /* rotate right */
t->left = y->right;
y->right = t;
t = y;
if (t->left == NULL) break;
}
r->left = t; /* link right */
r = t;
t = t->left;
} else if (i > t->item) {
if (t->right == NULL) break;
if (i > t->right->item) {
y = t->right; /* rotate left */
t->right = y->left;
y->left = t;
t = y;
if (t->right == NULL) break;
}
l->right = t; /* link left */
l = t;
t = t->right;
} else {
break;
}
}
l->right = t->left; /* assemble */
r->left = t->right;
t->left = N.right;
t->right = N.left;
return t;
}
/* Here is how sedgewick would have written this. */
/* It does the same thing. */
Tree * sedgewickized_splay (int i, Tree * t) {
Tree N, *l, *r, *y;
if (t == NULL) return t;
N.left = N.right = NULL;
l = r = &N;
for (;;) {
if (i < t->item) {
if (t->left != NULL && i < t->left->item) {
y = t->left; t->left = y->right; y->right = t; t = y;
}
if (t->left == NULL) break;
r->left = t; r = t; t = t->left;
} else if (i > t->item) {
if (t->right != NULL && i > t->right->item) {
y = t->right; t->right = y->left; y->left = t; t = y;
}
if (t->right == NULL) break;
l->right = t; l = t; t = t->right;
} else break;
}
l->right=t->left; r->left=t->right; t->left=N.right; t->right=N.left;
return t;
}
Tree * insert(int i, Tree * t) {
/* Insert i into the tree t, unless it's already there. */
/* Return a pointer to the resulting tree. */
Tree * new;
new = (Tree *) malloc (sizeof (Tree));
if (new == NULL) {
printf("Ran out of space\n");
exit(1);
}
new->item = i;
if (t == NULL) {
new->left = new->right = NULL;
size = 1;
return new;
}
t = splay(i,t);
if (i < t->item) {
new->left = t->left;
new->right = t;
t->left = NULL;
size ++;
return new;
} else if (i > t->item) {
new->right = t->right;
new->left = t;
t->right = NULL;
size++;
return new;
} else { /* We get here if it's already in the tree */
/* Don't add it again */
free(new);
return t;
}
}
Tree * delete(int i, Tree * t) {
/* Deletes i from the tree if it's there. */
/* Return a pointer to the resulting tree. */
Tree * x;
if (t==NULL) return NULL;
t = splay(i,t);
if (i == t->item) { /* found it */
if (t->left == NULL) {
x = t->right;
} else {
x = splay(i, t->left);
x->right = t->right;
}
size--;
free(t);
return x;
}
return t; /* It wasn't there */
}
void main() {
/* A sample use of these functions. Start with the empty tree, */
/* insert some stuff into it, and then delete it */
Tree * root;
int i;
root = NULL; /* the empty tree */
size = 0;
for (i = 0; i < 1024; i++) {
root = insert((541*i) & (1023), root);
}
for (i = 0; i < 1024; i++) {
root = delete((541*i) & (1023), root);
}
printf("size = %d\n", size);
}