-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
Copy pathdos_tree.h
210 lines (187 loc) · 5.02 KB
/
dos_tree.h
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
/*******************************************************************************
* DANIEL'S ALGORITHM IMPLEMENTAIONS
*
* /\ | _ _ ._ o _|_ |_ ._ _ _
* /--\ | (_| (_) | | |_ | | | | | _>
* _|
*
* DYNAMIC ORDER STATISTIC
*
* Features:
* 1. based on red-black tree
* 2. O(logn) lookup time
* 3. Select the i-th largest element
*
* http://en.wikipedia.org/wiki/Order_statistic
*
******************************************************************************/
#ifndef ALGO_DOS_TREE_H__
#define ALGO_DOS_TREE_H__
#include <stdlib.h>
#include <assert.h>
#include <limits.h>
#include "generic.h"
#include "stack.h"
#include "double_linked_list.h"
#include "rbtree.h"
namespace alg {
class DosTree:public RBTreeAbstract {
public:
/**
* dynamic order stat node structure definition
*/
typedef struct dostree_node_t : public rbtree_node_t {
int key; // the key
int size; // the size of this subtree
} *dostree_node;
#define DOSNODE(rbnode) static_cast<dostree_node>(rbnode)
#define DOSNODE_SIZE(rbnode) (rbnode?DOSNODE(rbnode)->size:0)
public:
DosTree() { }
~DosTree() {
destruct(DOSNODE(get_root()));
}
private:
DosTree(const DosTree&);
DosTree& operator=(const DosTree&);
void destruct(dostree_node n) {
if (n==NULL) return;
destruct(DOSNODE(n->left));
destruct(DOSNODE(n->right));
delete n;
}
public:
dostree_node index(int index) {
return lookup_node(get_root(),index);
}
/**
* dostree_insert
* insert a new key into the dos tree
*/
void insert(int key) {
dostree_node inserted_node = new_node(key, RED, NULL, NULL);
if (get_root() == NULL) {
set_root(inserted_node);
}
else {
rbtree_node n = DOSNODE(get_root());
while (1) {
// incr 1 for each node on the path traversed from the root
DOSNODE(n)->size+=1;
if (key < DOSNODE(n)->key) {
if (n->left == NULL) {
n->left = inserted_node;
break;
} else {
n = n->left;
}
} else {
if (n->right == NULL) {
n->right = inserted_node;
break;
} else {
n = n->right;
}
}
}
inserted_node->parent = n;
}
// fix red-black properties
insert_case1(inserted_node);
}
/**
* delete the key in the red-black tree
*/
void delete_key(dostree_node n) {
rbtree_node child;
/* Copy key/value from predecessor and then delete it instead */
if (n->left != NULL && n->right != NULL) {
dostree_node pred = DOSNODE(maximum_node(n->left));
DOSNODE(n)->key = DOSNODE(pred)->key;
DOSNODE(n)->size = DOSNODE(pred)->size;
n = pred;
}
// fix up size from pred
fixup_size(n);
assert(n->left == NULL || n->right == NULL);
child = n->right == NULL ? n->left : n->right;
if (node_color(n) == BLACK) {
n->color = node_color(child);
delete_case1(n);
}
replace_node(n, child);
if (n->parent == NULL && child != NULL) // root
child->color = BLACK;
delete (DOSNODE(n));
}
void print() {
print_helper(get_root(), 0);
puts("");
}
void print_helper(rbtree_node n, int indent) {
int i;
if (n == NULL) {
fputs("<empty tree>", stdout);
return;
}
if (n->right != NULL) {
print_helper(n->right, indent + INDENT_STEP);
}
for(i=0; i<indent; i++)
fputs(" ", stdout);
if (n->color == BLACK)
printf("[key:%d size:%d]\n", DOSNODE(n)->key,DOSNODE(n)->size);
else
printf("*[key:%d size:%d]\n", DOSNODE(n)->key, DOSNODE(n)->size);
if (n->left != NULL) {
print_helper(n->left, indent + INDENT_STEP);
}
}
private:
/**
* dostree_lookup
*
* select the i-th largest element
*/
dostree_node lookup_node(rbtree_node n, int i) {
if (n == NULL) return NULL; // beware of NULL pointer
int size = DOSNODE_SIZE(n->left) + 1;
if(i == size) return DOSNODE(n);
if(i < size ) return lookup_node(n->left, i);
else return lookup_node(n->right, i-size);
}
/**
* left/right rotation call back function
*/
void rotate_left_callback(rbtree_node n, rbtree_node parent) {
DOSNODE(parent)->size = DOSNODE_SIZE(n);
DOSNODE(n)->size = DOSNODE_SIZE(n->left) + DOSNODE_SIZE(n->right) + 1;
}
void rotate_right_callback(rbtree_node n, rbtree_node parent) {
rotate_left_callback(n,parent);
}
/**
* fix procedure caused by deletion
*/
void fixup_size(rbtree_node n) {
// fix up to the root
do {
DOSNODE(n)->size -= 1;
n = n->parent;
} while(n != NULL);
}
static inline dostree_node new_node(int key, color rbtree_node_color, rbtree_node left, rbtree_node right) {
dostree_node result =new dostree_node_t;
result->key = key;
result->size = 1;
result->color = rbtree_node_color;
result->left = NULL;
result->right = NULL;
if(left !=NULL) left->parent = result;
if(right!=NULL) right->parent = result;
result->parent = NULL;
return result;
}
};
}
#endif