Skip to content

Commit 44f2dff

Browse files
authored
Merge pull request #12 from shreyanshchordia/master
Added code for Binary Search Trees in JS and renamed the folders for better structure of Repo
2 parents 342f45b + 5a434f2 commit 44f2dff

File tree

10 files changed

+203
-0
lines changed

10 files changed

+203
-0
lines changed
File renamed without changes.
File renamed without changes.

8. Binary Search Tree/BST.js

+203
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,203 @@
1+
/*
2+
This code explains, how can Javascript be used to implement Binary Search Trees.
3+
In this program I have written code to add elements to a binary search tree, run in-order,
4+
pre-order, post-order traversals and find elements in a BST.
5+
*/
6+
class Node{
7+
constructor(val){
8+
this.val = val
9+
this.left = null
10+
this.right = null
11+
this.count = 1
12+
}
13+
}
14+
15+
class BinarySearchTree{
16+
constructor(){
17+
this.root = null
18+
}
19+
20+
create(val){
21+
var newnode = new Node(val)
22+
return newnode
23+
}
24+
25+
add(val){
26+
var newnode = this.create(val)
27+
if (!this.root){
28+
this.root = newnode
29+
return
30+
}
31+
32+
else{
33+
var prev = null
34+
var temp = this.root
35+
while(temp){
36+
prev = temp
37+
38+
if (val == temp.val){
39+
temp.count += 1
40+
return
41+
}
42+
else if (val < temp.val){
43+
temp = temp.left
44+
}
45+
else{
46+
temp = temp.right
47+
}
48+
}
49+
50+
if (val > prev.val){
51+
prev.right = newnode
52+
}
53+
54+
else {
55+
prev.left = newnode
56+
}
57+
58+
return
59+
}
60+
}
61+
62+
inorder(temp){
63+
if(!temp){
64+
return
65+
}
66+
67+
else{
68+
this.inorder(temp.left)
69+
70+
for (let index = 0; index < temp.count; index++) {
71+
console.log(temp.val + " ")
72+
}
73+
74+
this.inorder(temp.right)
75+
return
76+
}
77+
}
78+
79+
inorder_traversal(){
80+
this.inorder(this.root)
81+
}
82+
83+
postorder(temp){
84+
if(!temp){
85+
return
86+
}
87+
88+
else{
89+
this.postorder(temp.left)
90+
91+
this.postorder(temp.right)
92+
93+
for (let index = 0; index < temp.count; index++) {
94+
console.log(temp.val + " ")
95+
}
96+
97+
return
98+
}
99+
}
100+
101+
postorder_traversal(){
102+
this.postorder(this.root)
103+
}
104+
105+
preorder(temp){
106+
if(!temp){
107+
return
108+
}
109+
110+
else{
111+
112+
for (let index = 0; index < temp.count; index++) {
113+
console.log(temp.val + " ")
114+
}
115+
116+
this.preorder(temp.left)
117+
118+
this.preorder(temp.right)
119+
return
120+
}
121+
}
122+
123+
preorder_traversal(){
124+
this.preorder(this.root)
125+
}
126+
127+
find_val(val, temp){
128+
if(!temp){
129+
return -1
130+
}
131+
132+
else{
133+
if(val == temp.val){
134+
return temp.count
135+
}
136+
else if (val < temp.val){
137+
return this.find_val(val, temp.left)
138+
}
139+
else{
140+
return this.find_val(val, temp.right)
141+
}
142+
}
143+
}
144+
145+
find(val){
146+
return this.find_val(val, this.root)
147+
}
148+
149+
}
150+
151+
152+
var tree = new BinarySearchTree()
153+
154+
tree.add(1)
155+
tree.add(5)
156+
tree.add(2)
157+
tree.add(4)
158+
tree.add(2)
159+
tree.add(3)
160+
console.log("# Inorder Traversal")
161+
tree.inorder_traversal()
162+
console.log("# Pre-order Traversal")
163+
tree.preorder_traversal()
164+
console.log("# Post-order Traversal")
165+
tree.postorder_traversal()
166+
console.log("# Finding 10")
167+
console.log(tree.find(10))
168+
console.log("# Finding 2")
169+
console.log(tree.find(2))
170+
console.log("# Finding 5")
171+
console.log(tree.find(5))
172+
173+
/*
174+
OUTPUT
175+
176+
# Inorder Traversal
177+
1
178+
2
179+
2
180+
3
181+
4
182+
5
183+
# Pre-order Traversal
184+
1
185+
5
186+
2
187+
2
188+
4
189+
3
190+
# Post-order Traversal
191+
3
192+
4
193+
2
194+
2
195+
5
196+
1
197+
# Finding 10
198+
-1
199+
# Finding 2
200+
2
201+
# Finding 5
202+
1
203+
*/

0 commit comments

Comments
 (0)