-
Notifications
You must be signed in to change notification settings - Fork 0
/
AugBST.cpp
123 lines (121 loc) · 3.8 KB
/
AugBST.cpp
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
#include<iostream>
using namespace std;
template <typename T>
class AugBST{
public:
class node{
public:
T key;
int nleft;
node * left;
node * right;
node * parent;
node(T k, node * p){
parent = p;
nleft = 0;
key = k;
left = NULL;
right = NULL;
}
};
node * root = NULL;
int n;
void insert(T x){
root=insertUtil(root, x, NULL);
}
void remove(T x){
root=removeUtil(root, x, NULL);
}
node * search(T x){
return searchUtil(root,x);
}
void inorder(){
inorderUtil(root);
cout<<endl;
}
T ksmallest(int k){
return ksmallestUtil(root, k);
}
int rank(T x){
return rankUtil(root, x, 0);
}
private:
void inorderUtil(node * head){
if(head==NULL) return ;
inorderUtil(head->left);
cout<<head->key<<" ";
inorderUtil(head->right);
}
node * insertUtil(node * head, T x, node * p){
if(head == NULL){
n++;
node * temp = new node(x,p);
while(p!=NULL){
if(x<p->key) p->nleft += 1;
p=p->parent;
}
return temp;
}
if(x > head->key) head->right = insertUtil(head->right, x, head);
else if(x < head->key) head->left = insertUtil(head->left, x, head);
return head;
}
node * searchUtil(node * head, T x){
if(head == NULL) return NULL;
T k = head->key;
if(k == x) return head;
if(k > x) return searchUtil(head->left, x);
if(k < x) return searchUtil(head->right, x);
}
int rankUtil(node * head, T x, int r){
if(head == NULL) return 0;
T k = head->key;
if(k == x) return r+1+head->nleft;
if(k > x) return rankUtil(head->left, x, r);
if(k < x) return rankUtil(head->right, x, r+head->nleft+1);
}
node * removeUtil(node * head, T x, node * p){
if(head == NULL) return NULL;
if(x == head->key){
node * l = head->left;
node * r = head->right;
while(p!=NULL){
if(x<p->key) p->nleft -= 1;
p=p->parent;
}
if(l == NULL){
delete(head);
return r;
}
if(r == NULL){
delete(head);
return l;
}
while(r->left != NULL) r = r->left;
head->key = r->key;
head->right = removeUtil(head->right, r->key, NULL);
return head;
}
if(x < head->key) head->left = removeUtil(head->left, x, head);
else head->right = removeUtil(head->right, x, head);
return head;
}
T ksmallestUtil(node * head, int k){
if(k<1 || k>n) return NULL;
if(head->nleft == k-1) return head->key;
if(head->nleft > k-1) return ksmallestUtil(head->left, k);
return ksmallestUtil(head->right, k-1-head->nleft);
}
};
int main(){
AugBST<float> t;
t.insert(4.1);
t.insert(2.3);
t.insert(1.5);
t.insert(3.7);
t.insert(6.5);
t.insert(5.4);
t.insert(7.2);
t.inorder();
cout<<t.rank(t.ksmallest(1));
}