Skip to content

Commit ca46ad4

Browse files
authored
Merge pull request #4 from tejas-2232/master
Updates from original repo
2 parents 4cda468 + 44f2dff commit ca46ad4

File tree

11 files changed

+237
-0
lines changed

11 files changed

+237
-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+
*/

README.md

+34
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,40 @@ __Code Implementation:__
321321
1. Using .forEach Method:
322322
The .forEach method in javascript runs a provided function on each element within array
323323

324+
```js
325+
function sentenceCap(text) {
326+
let wordArray = text.toLowerCase().split(' ')
327+
328+
let capsarray = []
329+
330+
wordArray.forEach(word => {
331+
capsarray.push(word[0].toUpperCase()+ word.slice(1) )
332+
});
333+
334+
return capsarray.join(' ')
335+
336+
}
337+
console.log(sentenceCap("ARTIFICIAL"))
338+
//will return Artificial
339+
```
340+
341+
<p>
342+
343+
* We call the .toLowerCase() method on the string of text received to convert the entire sentence to lowercase. We also chain the .split() method in sequence to divide the lowercase sentence into an array of words as shown below. This array is stored as wordsArray
344+
</p>
345+
<p>
346+
347+
* Next, using the .foreach() method, we iterate through every word(element) in the array and execute a function on it.
348+
* The function takes the first letter of the word and turns it to uppercase using the .toUpperCase() method. To retrieve the remaining part of the word in lowercase, we use the .slice() method to slice the string starting from position 1 till the end.
349+
350+
* We combine the transformed first letter and the sliced section together to form the capitalized word which we push into our array of capitalized words capsArray.
351+
352+
* After this process has being carried out for every word, capsArray now holds the capitalized version of every word in the sentence
353+
354+
* Finally .join() method is used. Then We pass in an empty space as the separator. This gives us the capitalized sentence which we return in conclusion.
355+
</p>
356+
357+
324358
2. Using .map and .slice method:
325359
The .map method is used to create a new array with the results gotten from calling a provided function on every element in the array on which it is called.
326360

0 commit comments

Comments
 (0)