@@ -18,6 +18,14 @@ void upd_sz (pnode t) {
18
18
}
19
19
}
20
20
21
+ pnode find (pnode t, int key) {
22
+ if (t == NULL || t->val == key) {
23
+ return t;
24
+ }
25
+
26
+ return find (t->val < key ? t->r : t->l , key);
27
+ }
28
+
21
29
void split (pnode t, pnode &l, pnode &r, int key) {
22
30
if (!t) {
23
31
l = r = NULL ;
@@ -38,7 +46,7 @@ void merge (pnode &t, pnode l, pnode r) {
38
46
if (!l || !r) {
39
47
t = l?l:r;
40
48
}
41
- else if (l->prior > r->prior ) {
49
+ else if (l->prior > r->prior ) {
42
50
merge (l->r , l->r , r);
43
51
t = l;
44
52
}
@@ -50,6 +58,34 @@ void merge (pnode &t, pnode l, pnode r) {
50
58
upd_sz (t);
51
59
}
52
60
61
+ void insert (pnode &t, pnode it) {
62
+ if (!t) {
63
+ t = it;
64
+ }
65
+ else if (it->prior > t->prior ) {
66
+ split (t, it->l , it->r , it->val );
67
+ t = it;
68
+ }
69
+ else {
70
+ insert (t->val <= it->val ? t->r : t->l , it);
71
+ }
72
+
73
+ upd_sz (t);
74
+ }
75
+
76
+ void erase (pnode &t, int key) {
77
+ pnode p = find (t, key);
78
+ if (p == NULL ) {
79
+ return ;
80
+ }
81
+
82
+ pnode temp = t;
83
+ merge (t, p->l , p->r );
84
+ free (temp);
85
+
86
+ upd_sz (t);
87
+ }
88
+
53
89
pnode init (int val) {
54
90
pnode ret = (pnode)malloc (sizeof (node));
55
91
ret->val = val;
@@ -58,3 +94,18 @@ pnode init (int val) {
58
94
ret->l = ret->r = NULL ;
59
95
return ret;
60
96
}
97
+
98
+ int main () {
99
+ pnode t = init (4 );
100
+ insert (t, init (19 ));
101
+ insert (t, init (8 ));
102
+ insert (t, init (27 ));
103
+ insert (t, init (20 ));
104
+ insert (t, init (12 ));
105
+ insert (t, init (15 ));
106
+ insert (t, init (6 ));
107
+ insert (t, init (7 ));
108
+ insert (t, init (8 ));
109
+
110
+ return 0 ;
111
+ }
0 commit comments