Skip to content

Commit 53e6c19

Browse files
committed
left rotate binary tree
1 parent 0054c91 commit 53e6c19

11 files changed

+50
-5799
lines changed

Data Structures/Binary Tree/index.js

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,25 @@ class BinaryTree {
1717
this.root = new Node(info, null, null);
1818
return;
1919
} else {
20-
const searchTree = function (node) {
20+
const createTree = function (node) {
2121
if (info < node.info) {
2222
if (node.left === null) {
2323
node.left = new Node(info, null, null);
2424
return;
2525
} else if (node.left !== null) {
26-
return searchTree(node.left);
26+
return createTree(node.left);
2727
}
2828
} else if (info > node.info) {
2929
if (node.right === null) {
3030
node.right = new Node(info, null, null);
3131
} else if (node.right !== null) {
32-
return searchTree(node.right);
32+
return createTree(node.right);
3333
}
3434
} else {
3535
return null;
3636
}
3737
};
38-
return searchTree(node);
38+
return createTree(node);
3939
}
4040
}
4141

@@ -57,15 +57,18 @@ class BinaryTree {
5757

5858
find(info) {
5959
let currentNode = this.root;
60+
let parentNode = this.root;
6061
while (currentNode.info !== info) {
6162
if (info > currentNode.info) {
63+
parentNode = currentNode;
6264
currentNode = currentNode.right;
6365
} else {
66+
parentNode = currentNode;
6467
currentNode = currentNode.left;
6568
}
6669
if (currentNode === null) return null;
6770
}
68-
return currentNode;
71+
return { currentNode, parentNode };
6972
}
7073

7174
isPresent(info) {
@@ -88,15 +91,19 @@ class BinaryTree {
8891
return null;
8992
}
9093
if (info === node.info) {
94+
// if the deleted node has no left child and right child
9195
if (node.left === null && node.right === null) {
9296
return null;
9397
}
98+
// if the deleted node has no left child
9499
if (node.left === null) {
95100
return node.right;
96101
}
102+
// if the deleted node has no right child
97103
if (node.right === null) {
98104
return node.left;
99105
}
106+
// if the deleted node has both left and right child
100107
let tempNode = node.right;
101108
while (tempNode.left !== null) {
102109
template = tempNode.left;
@@ -165,6 +172,26 @@ class BinaryTree {
165172
return visit(node);
166173
}
167174
}
175+
176+
leftRotate(info) {
177+
let node = this.find(info) ? this.find(info).currentNode : null;
178+
let rightNode = node ? node.right : null;
179+
if (rightNode !== null) {
180+
// Copy the info
181+
let info = node.info;
182+
node.info = rightNode.info;
183+
rightNode.info = info;
184+
185+
// left rotation
186+
node.right = rightNode.right;
187+
rightNode.right = rightNode.left;
188+
rightNode.left = node.left;
189+
node.left = rightNode;
190+
return this.leftRotate(rightNode.info);
191+
}
192+
}
193+
194+
rightRotate(info) {}
168195
}
169196

170197
const bst1 = new BinaryTree();
@@ -175,9 +202,7 @@ bst1.add(11);
175202
bst1.add(-2);
176203
bst1.add(8);
177204
bst1.add(21);
178-
bst1.remove(21);
179-
console.log(bst1);
180-
console.log("Root node: ", bst1.root);
181-
console.log("pre order traversal: ", bst1.preOrderTraversal());
182-
console.log("in order traversal: ", bst1.inOrderTraversal());
183-
console.log("post order traversal: ", bst1.postOrderTraversal());
205+
console.log(bst1.root);
206+
console.log(bst1.leftRotate(20));
207+
console.log(bst1.root);
208+
console.log(bst1.find(21));

0 commit comments

Comments
 (0)