Skip to content

Commit 5c31c71

Browse files
committed
Fix issue 'bug in tree rebalancing' and 'Segment Fault Bug'
1 parent c6cc67f commit 5c31c71

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

AVL.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,14 +114,14 @@ class AVL{
114114
head->height = 1 + max(height(head->left), height(head->right));
115115
int bal = height(head->left) - height(head->right);
116116
if(bal>1){
117-
if(x > head->left->key){
117+
if(height(head->left) >= height(head->right)){
118118
return rightRotation(head);
119119
}else{
120120
head->left = leftRotation(head->left);
121121
return rightRotation(head);
122122
}
123123
}else if(bal < -1){
124-
if(x < head->right->key){
124+
if(height(head->right) >= height(head->left)){
125125
return leftRotation(head);
126126
}else{
127127
head->right = rightRotation(head->right);
@@ -138,6 +138,7 @@ class AVL{
138138
if(k < x) return searchUtil(head->right, x);
139139
}
140140
};
141+
/*
141142
int main(){
142143
AVL<float> t;
143144
t.insert(1.3);
@@ -153,3 +154,4 @@ int main(){
153154
t.remove(7.9);
154155
t.inorder();
155156
}
157+
*/

0 commit comments

Comments
 (0)