Skip to content

Commit 0054c91

Browse files
committed
binary search tree
1 parent 829c352 commit 0054c91

10 files changed

+5971
-0
lines changed

Data Structures/Binary Tree/index.js

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
class Node {
2+
constructor(info, left, right) {
3+
this.info = info;
4+
this.left = left;
5+
this.right = right;
6+
}
7+
}
8+
9+
class BinaryTree {
10+
constructor() {
11+
this.root = null;
12+
}
13+
14+
add(info) {
15+
const node = this.root; // Start from the root node
16+
if (node === null) {
17+
this.root = new Node(info, null, null);
18+
return;
19+
} else {
20+
const searchTree = function (node) {
21+
if (info < node.info) {
22+
if (node.left === null) {
23+
node.left = new Node(info, null, null);
24+
return;
25+
} else if (node.left !== null) {
26+
return searchTree(node.left);
27+
}
28+
} else if (info > node.info) {
29+
if (node.right === null) {
30+
node.right = new Node(info, null, null);
31+
} else if (node.right !== null) {
32+
return searchTree(node.right);
33+
}
34+
} else {
35+
return null;
36+
}
37+
};
38+
return searchTree(node);
39+
}
40+
}
41+
42+
findMin() {
43+
let currentNode = this.root;
44+
while (currentNode.left !== null) {
45+
currentNode = currentNode.left;
46+
}
47+
return currentNode.info;
48+
}
49+
50+
findMax() {
51+
let currentNode = this.root;
52+
while (currentNode.right !== null) {
53+
currentNode = currentNode.right;
54+
}
55+
return currentNode.info;
56+
}
57+
58+
find(info) {
59+
let currentNode = this.root;
60+
while (currentNode.info !== info) {
61+
if (info > currentNode.info) {
62+
currentNode = currentNode.right;
63+
} else {
64+
currentNode = currentNode.left;
65+
}
66+
if (currentNode === null) return null;
67+
}
68+
return currentNode;
69+
}
70+
71+
isPresent(info) {
72+
let currentNode = this.root;
73+
while (currentNode) {
74+
if (info > currentNode.info) {
75+
currentNode = currentNode.right;
76+
} else if (info < currentNode.info) {
77+
currentNode = currentNode.left;
78+
} else {
79+
return true;
80+
}
81+
}
82+
return false;
83+
}
84+
85+
remove(info) {
86+
const removeNode = (node, info) => {
87+
if (node === null) {
88+
return null;
89+
}
90+
if (info === node.info) {
91+
if (node.left === null && node.right === null) {
92+
return null;
93+
}
94+
if (node.left === null) {
95+
return node.right;
96+
}
97+
if (node.right === null) {
98+
return node.left;
99+
}
100+
let tempNode = node.right;
101+
while (tempNode.left !== null) {
102+
template = tempNode.left;
103+
}
104+
node.info = tempNode.info;
105+
node.right = removeNode(node.right, tempNode.info);
106+
return node;
107+
} else if (info < node.info) {
108+
node.left = removeNode(node.left, info);
109+
return node;
110+
} else {
111+
node.right = removeNode(node.right, info);
112+
return node;
113+
}
114+
};
115+
this.root = removeNode(this.root, info);
116+
}
117+
118+
preOrderTraversal() {
119+
const node = this.root;
120+
if (node === null) {
121+
return [];
122+
} else {
123+
const visit = (node, acc = []) => {
124+
if (node) {
125+
acc.push(node.info);
126+
if (node.left) visit(node.left, acc);
127+
if (node.right) visit(node.right, acc);
128+
}
129+
return acc;
130+
};
131+
return visit(node);
132+
}
133+
}
134+
135+
inOrderTraversal() {
136+
const node = this.root;
137+
if (node === null) {
138+
return [];
139+
} else {
140+
const visit = (node, acc = []) => {
141+
if (node) {
142+
if (node.left) visit(node.left, acc);
143+
acc.push(node.info);
144+
if (node.right) visit(node.right, acc);
145+
}
146+
return acc;
147+
};
148+
return visit(node);
149+
}
150+
}
151+
152+
postOrderTraversal() {
153+
const node = this.root;
154+
if (node === null) {
155+
return [];
156+
} else {
157+
const visit = (node, acc = []) => {
158+
if (node) {
159+
if (node.left) visit(node.left, acc);
160+
if (node.right) visit(node.right, acc);
161+
acc.push(node.info);
162+
}
163+
return acc;
164+
};
165+
return visit(node);
166+
}
167+
}
168+
}
169+
170+
const bst1 = new BinaryTree();
171+
bst1.add(10);
172+
bst1.add(20);
173+
bst1.add(5);
174+
bst1.add(11);
175+
bst1.add(-2);
176+
bst1.add(8);
177+
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());

0 commit comments

Comments
 (0)