Skip to content

Commit 73b3db5

Browse files
authored
Merge pull request loiane#193 from Jeffzholy/fix/chapter-10-AVLTree
fix: Chapter 10 AVL tree insertNode and removeNode functions
2 parents 8a39b5c + f0fa8e5 commit 73b3db5

File tree

2 files changed

+53
-100
lines changed

2 files changed

+53
-100
lines changed

src/js/data-structures/avl-tree.js

+19-26
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,13 @@ export default class AVLTree extends BinarySearchTree {
101101
insertNode(node, key) {
102102
if (node == null) {
103103
return new Node(key);
104-
} if (this.compareFn(key, node.key) === Compare.LESS_THAN) {
104+
}
105+
if (this.compareFn(key, node.key) === Compare.LESS_THAN) {
105106
node.left = this.insertNode(node.left, key);
106-
} else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {
107-
node.right = this.insertNode(node.right, key);
108107
} else {
109-
return node; // duplicated key
108+
node.right = this.insertNode(node.right, key);
110109
}
110+
111111
// verify if tree is balanced
112112
const balanceFactor = this.getBalanceFactor(node);
113113
if (balanceFactor === BalanceFactor.UNBALANCED_LEFT) {
@@ -116,7 +116,7 @@ export default class AVLTree extends BinarySearchTree {
116116
node = this.rotationLL(node);
117117
} else {
118118
// Left right case
119-
return this.rotationLR(node);
119+
node = this.rotationLR(node);
120120
}
121121
}
122122
if (balanceFactor === BalanceFactor.UNBALANCED_RIGHT) {
@@ -125,7 +125,7 @@ export default class AVLTree extends BinarySearchTree {
125125
node = this.rotationRR(node);
126126
} else {
127127
// Right left case
128-
return this.rotationRL(node);
128+
node = this.rotationRL(node);
129129
}
130130
}
131131
return node;
@@ -136,32 +136,25 @@ export default class AVLTree extends BinarySearchTree {
136136
if (node == null) {
137137
return node;
138138
}
139+
139140
// verify if tree is balanced
140141
const balanceFactor = this.getBalanceFactor(node);
141142
if (balanceFactor === BalanceFactor.UNBALANCED_LEFT) {
142-
// Left left case
143-
if (
144-
this.getBalanceFactor(node.left) === BalanceFactor.BALANCED
145-
|| this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT
146-
) {
147-
return this.rotationLL(node);
148-
}
149-
// Left right case
150-
if (this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT) {
151-
return this.rotationLR(node.left);
143+
if (this.compareFn(key, node.left.key) === Compare.LESS_THAN) {
144+
// Left left case
145+
node = this.rotationLL(node);
146+
} else {
147+
// Left right case
148+
node = this.rotationLR(node);
152149
}
153150
}
154151
if (balanceFactor === BalanceFactor.UNBALANCED_RIGHT) {
155-
// Right right case
156-
if (
157-
this.getBalanceFactor(node.right) === BalanceFactor.BALANCED
158-
|| this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT
159-
) {
160-
return this.rotationRR(node);
161-
}
162-
// Right left case
163-
if (this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT) {
164-
return this.rotationRL(node.right);
152+
if (this.compareFn(key, node.right.key) === Compare.BIGGER_THAN) {
153+
// Right right case
154+
node = this.rotationRR(node);
155+
} else {
156+
// Right left case
157+
node = this.rotationRL(node);
165158
}
166159
}
167160
return node;

src/ts/data-structures/avl-tree.ts

+34-74
Original file line numberDiff line numberDiff line change
@@ -100,102 +100,62 @@ export default class AVLTree<T> extends BinarySearchTree<T> {
100100
protected insertNode(node: Node<T>, key: T) {
101101
if (node == null) {
102102
return new Node(key);
103-
} else if (this.compareFn(key, node.key) === Compare.LESS_THAN) {
103+
}
104+
if (this.compareFn(key, node.key) === Compare.LESS_THAN) {
104105
node.left = this.insertNode(node.left, key);
105-
} else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {
106-
node.right = this.insertNode(node.right, key);
107106
} else {
108-
return node; // duplicated key
107+
node.right = this.insertNode(node.right, key);
109108
}
110109

111110
// verify if tree is balanced
112-
const balanceState = this.getBalanceFactor(node);
113-
114-
if (balanceState === BalanceFactor.UNBALANCED_LEFT) {
111+
const balanceFactor = this.getBalanceFactor(node);
112+
if (balanceFactor === BalanceFactor.UNBALANCED_LEFT) {
115113
if (this.compareFn(key, node.left.key) === Compare.LESS_THAN) {
116114
// Left left case
117115
node = this.rotationLL(node);
118116
} else {
119117
// Left right case
120-
return this.rotationLR(node);
118+
node = this.rotationLR(node);
121119
}
122120
}
123-
124-
if (balanceState === BalanceFactor.UNBALANCED_RIGHT) {
121+
if (balanceFactor === BalanceFactor.UNBALANCED_RIGHT) {
125122
if (this.compareFn(key, node.right.key) === Compare.BIGGER_THAN) {
126123
// Right right case
127124
node = this.rotationRR(node);
128125
} else {
129126
// Right left case
130-
return this.rotationRL(node);
127+
node = this.rotationRL(node);
131128
}
132129
}
133-
134130
return node;
135131
}
136132

137133
protected removeNode(node: Node<T>, key: T) {
138-
if (node == null) {
139-
return null;
140-
}
141-
142-
if (this.compareFn(key, node.key) === Compare.LESS_THAN) {
143-
// The key to be deleted is in the left sub-tree
144-
node.left = this.removeNode(node.left, key);
145-
} else if (this.compareFn(key, node.key) === Compare.BIGGER_THAN) {
146-
// The key to be deleted is in the right sub-tree
147-
node.right = this.removeNode(node.right, key);
148-
} else {
149-
// node is the node to be deleted
150-
if (node.left == null && node.right == null) {
151-
node = null;
152-
} else if (node.left == null && node.right != null) {
153-
node = node.right;
154-
} else if (node.left != null && node.right == null) {
155-
node = node.left;
156-
} else {
157-
// node has 2 children, get the in-order successor
158-
const inOrderSuccessor = this.minNode(node.right);
159-
node.key = inOrderSuccessor.key;
160-
node.right = this.removeNode(node.right, inOrderSuccessor.key);
161-
}
162-
}
163-
164-
if (node == null) {
165-
return node;
166-
}
167-
168-
// verify if tree is balanced
169-
const balanceState = this.getBalanceFactor(node);
170-
171-
if (balanceState === BalanceFactor.UNBALANCED_LEFT) {
172-
// Left left case
173-
if (
174-
this.getBalanceFactor(node.left) === BalanceFactor.BALANCED ||
175-
this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT
176-
) {
177-
return this.rotationLL(node);
178-
}
179-
// Left right case
180-
if (this.getBalanceFactor(node.left) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT) {
181-
return this.rotationLR(node.left);
182-
}
183-
}
184-
185-
if (balanceState === BalanceFactor.UNBALANCED_RIGHT) {
186-
// Right right case
187-
if (
188-
this.getBalanceFactor(node.right) === BalanceFactor.BALANCED ||
189-
this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_RIGHT
190-
) {
191-
return this.rotationRR(node);
192-
}
193-
// Right left case
194-
if (this.getBalanceFactor(node.right) === BalanceFactor.SLIGHTLY_UNBALANCED_LEFT) {
195-
return this.rotationRL(node.right);
196-
}
197-
}
198-
199-
return node;
134+
node = super.removeNode(node, key); // {1}
135+
if (node == null) {
136+
return node;
137+
}
138+
139+
// verify if tree is balanced
140+
const balanceFactor = this.getBalanceFactor(node);
141+
if (balanceFactor === BalanceFactor.UNBALANCED_LEFT) {
142+
if (this.compareFn(key, node.left.key) === Compare.LESS_THAN) {
143+
// Left left case
144+
node = this.rotationLL(node);
145+
} else {
146+
// Left right case
147+
node = this.rotationLR(node);
148+
}
149+
}
150+
if (balanceFactor === BalanceFactor.UNBALANCED_RIGHT) {
151+
if (this.compareFn(key, node.right.key) === Compare.BIGGER_THAN) {
152+
// Right right case
153+
node = this.rotationRR(node);
154+
} else {
155+
// Right left case
156+
node = this.rotationRL(node);
157+
}
158+
}
159+
return node;
200160
}
201161
}

0 commit comments

Comments
 (0)