Skip to content

Commit 528a810

Browse files
committed
feat: add rebalance function
1 parent 1dd93ad commit 528a810

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

binary_search_tree/Tree.js

+9
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ class Tree {
178178
);
179179
}
180180

181+
rebalance() {
182+
if (this.checkIsBalanced()) return;
183+
184+
const arrayOfNodes = this.levelOrder((node) => node.data);
185+
this.root = this.buildTree(arrayOfNodes);
186+
187+
return this.root;
188+
}
189+
181190
print(node = this.root, prefix = "", isLeft = true) {
182191
if (node === null) {
183192
return;

binary_search_tree/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ console.log(tree.checkIsBalanced());
3232

3333
tree.insert(2.5);
3434
tree.print();
35-
36-
console.log(tree.checkIsBalanced());
35+
tree.rebalance();
36+
tree.print();

0 commit comments

Comments
 (0)