Skip to content

Commit 7d790c2

Browse files
authored
Add files via upload
1 parent 9bed294 commit 7d790c2

File tree

1 file changed

+267
-0
lines changed

1 file changed

+267
-0
lines changed

Tree/AVL/03 Delete From AVL.cpp

Lines changed: 267 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,267 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Node {
6+
public:
7+
Node* lchild;
8+
int data;
9+
Node* rchild;
10+
int height;
11+
};
12+
13+
class AVL{
14+
public:
15+
Node* root;
16+
17+
AVL(){ root = nullptr; }
18+
19+
// Helper methods for inserting/deleting
20+
int NodeHeight(Node* p);
21+
int BalanceFactor(Node* p);
22+
Node* LLRotation(Node* p);
23+
Node* RRRotation(Node* p);
24+
Node* LRRotation(Node* p);
25+
Node* RLRotation(Node* p);
26+
Node* InPre(Node* p);
27+
Node* InSucc(Node* p);
28+
29+
// Insert
30+
Node* rInsert(Node* p, int key);
31+
32+
// Traversal
33+
void Inorder(Node* p);
34+
void Inorder(){ Inorder(root); }
35+
Node* getRoot(){ return root; }
36+
37+
// Delete
38+
Node* Delete(Node* p, int key);
39+
};
40+
41+
int AVL::NodeHeight(Node *p) {
42+
int hl;
43+
int hr;
44+
45+
hl = (p && p->lchild) ? p->lchild->height : 0;
46+
hr = (p && p->rchild) ? p->rchild->height : 0;
47+
48+
return hl > hr ? hl + 1 : hr + 1;
49+
}
50+
51+
int AVL::BalanceFactor(Node *p) {
52+
int hl;
53+
int hr;
54+
55+
hl = (p && p->lchild) ? p->lchild->height : 0;
56+
hr = (p && p->rchild) ? p->rchild->height : 0;
57+
58+
return hl - hr;
59+
}
60+
61+
Node* AVL::LLRotation(Node *p) {
62+
Node* pl = p->lchild;
63+
Node* plr = pl->rchild;
64+
65+
pl->rchild = p;
66+
p->lchild = plr;
67+
68+
// Update height
69+
p->height = NodeHeight(p);
70+
pl->height = NodeHeight(pl);
71+
72+
// Update root
73+
if (root == p){
74+
root = pl;
75+
}
76+
return pl;
77+
}
78+
79+
Node* AVL::RRRotation(Node *p) {
80+
Node* pr = p->rchild;
81+
Node* prl = pr->lchild;
82+
83+
pr->lchild = p;
84+
p->rchild = prl;
85+
86+
// Update height
87+
p->height = NodeHeight(p);
88+
pr->height = NodeHeight(pr);
89+
90+
// Update root
91+
if (root == p){
92+
root = pr;
93+
}
94+
return pr;
95+
}
96+
97+
Node* AVL::LRRotation(Node *p) {
98+
Node* pl = p->lchild;
99+
Node* plr = pl->rchild;
100+
101+
pl->rchild = plr->lchild;
102+
p->lchild = plr->rchild;
103+
104+
plr->lchild = pl;
105+
plr->rchild = p;
106+
107+
// Update height
108+
pl->height = NodeHeight(pl);
109+
p->height = NodeHeight(p);
110+
plr->height = NodeHeight(plr);
111+
112+
// Update root
113+
if (p == root){
114+
root = plr;
115+
}
116+
return plr;
117+
}
118+
119+
Node* AVL::RLRotation(Node *p) {
120+
Node* pr = p->rchild;
121+
Node* prl = pr->lchild;
122+
123+
pr->lchild = prl->rchild;
124+
p->rchild = prl->lchild;
125+
126+
prl->rchild = pr;
127+
prl->lchild = p;
128+
129+
// Update height
130+
pr->height = NodeHeight(pr);
131+
p->height = NodeHeight(p);
132+
prl->height = NodeHeight(prl);
133+
134+
// Update root
135+
if (root == p){
136+
root = prl;
137+
}
138+
return prl;
139+
}
140+
141+
Node* AVL::InPre(Node *p) {
142+
while (p && p->rchild != nullptr){
143+
p = p->rchild;
144+
}
145+
return p;
146+
}
147+
148+
Node* AVL::InSucc(Node *p) {
149+
while (p && p->lchild != nullptr){
150+
p = p->lchild;
151+
}
152+
return p;
153+
}
154+
155+
Node* AVL::rInsert(Node *p, int key) {
156+
Node* t;
157+
if (p == nullptr){
158+
t = new Node;
159+
t->data = key;
160+
t->lchild = nullptr;
161+
t->rchild = nullptr;
162+
t->height = 1; // Starting height from 1 onwards instead of 0
163+
return t;
164+
}
165+
166+
if (key < p->data){
167+
p->lchild = rInsert(p->lchild, key);
168+
} else if (key > p->data){
169+
p->rchild = rInsert(p->rchild, key);
170+
}
171+
172+
// Update height
173+
p->height = NodeHeight(p);
174+
175+
// Balance Factor and Rotation
176+
if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == 1) {
177+
return LLRotation(p);
178+
} else if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == -1){
179+
return LRRotation(p);
180+
} else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == -1){
181+
return RRRotation(p);
182+
} else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == 1){
183+
return RLRotation(p);
184+
}
185+
186+
return p;
187+
}
188+
189+
void AVL::Inorder(Node *p) {
190+
if (p){
191+
Inorder(p->lchild);
192+
cout << p->data << ", " << flush;
193+
Inorder(p->rchild);
194+
}
195+
}
196+
197+
Node* AVL::Delete(Node *p, int key) {
198+
if (p == nullptr){
199+
return nullptr;
200+
}
201+
202+
if (p->lchild == nullptr && p->rchild == nullptr){
203+
if (p == root){
204+
root = nullptr;
205+
}
206+
delete p;
207+
return nullptr;
208+
}
209+
210+
if (key < p->data){
211+
p->lchild = Delete(p->lchild, key);
212+
} else if (key > p->data){
213+
p->rchild = Delete(p->rchild, key);
214+
} else {
215+
Node* q;
216+
if (NodeHeight(p->lchild) > NodeHeight(p->rchild)){
217+
q = InPre(p->lchild);
218+
p->data = q->data;
219+
p->lchild = Delete(p->lchild, q->data);
220+
} else {
221+
q = InSucc(p->rchild);
222+
p->data = q->data;
223+
p->rchild = Delete(p->rchild, q->data);
224+
}
225+
}
226+
227+
// Update height
228+
p->height = NodeHeight(p);
229+
230+
// Balance Factor and Rotation
231+
if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == 1) { // L1 Rotation
232+
return LLRotation(p);
233+
} else if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == -1){ // L-1 Rotation
234+
return LRRotation(p);
235+
} else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == -1){ // R-1 Rotation
236+
return RRRotation(p);
237+
} else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == 1){ // R1 Rotation
238+
return RLRotation(p);
239+
} else if (BalanceFactor(p) == 2 && BalanceFactor(p->lchild) == 0){ // L0 Rotation
240+
return LLRotation(p);
241+
} else if (BalanceFactor(p) == -2 && BalanceFactor(p->rchild) == 0){ // R0 Rotation
242+
return RRRotation(p);
243+
}
244+
245+
return p;
246+
}
247+
248+
249+
int main() {
250+
251+
AVL tree;
252+
253+
int A[] = {10, 20, 30, 25, 28, 27, 5};
254+
for (int i=0; i<sizeof(A)/sizeof(A[0]); i++){
255+
tree.root = tree.rInsert(tree.root, A[i]);
256+
}
257+
258+
tree.Inorder();
259+
cout << endl;
260+
261+
tree.Delete(tree.root, 28);
262+
263+
tree.Inorder();
264+
cout << endl;
265+
266+
return 0;
267+
}

0 commit comments

Comments
 (0)