-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinary-trees.js
159 lines (146 loc) · 4.71 KB
/
binary-trees.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const { inspect } = require('util')
class TreeNode {
constructor(val = null){
this.val = val
this.left = null
this.right = null
}
}
class BinaryTree {
constructor(root = null){
this.root = root
}
insert(value){
const newNode = new TreeNode(value)
// if there is no root node, assign the newNode to be the root
if(!this.root){
this.root = newNode
} else {
this.recursiveInsert(this.root, newNode)
}
}
recursiveInsert(node, newNode){
// compare node's value to newNode's value
if(newNode.val < node.val){
// if there is no node.left, assign newNode to be node.left
if(!node.left){
node.left = newNode
} else {
// Keep traversing to find a home for the newNode
this.recursiveInsert(node.left, newNode)
}
} else {
// if there is no node.right, assign newNode to be node.right
if(!node.right){
node.right = newNode
} else {
// Keep traversing to find a home for the newNode
this.recursiveInsert(node.right, newNode)
}
}
}
// Return the node w the given value
search(value){
return this.recursiveSearch(this.root, value)
}
// Writing a separate method so we can pass ANY node to it, not just the root
recursiveSearch(node, value){
// if we don't have a root node..?
if(!node) return null
else if(node.val === value){
// node's val is equal to the value
// return that node
return node
} else if(value < node.val){
// if value is less than the node.val
return this.recursiveSearch(node.left, value)
} else {
return this.recursiveSearch(node.right, value)
}
}
// addValues(){} -- breadth first, depth first, recursively
// Add up all the vals for each node and return the total
addVals1(){
// Breadth First
// Initialize a total var at 0
let total = 0
const queue = []
queue.push(this.root)
// Iterate over the queue while the queue has nodes in it
while(queue.length){
// take the first element in the queue out of the queue and look at it
const node = queue.shift()
// if the element is a node, then add its val to the total
if(node !== null){
total += node.val
// then check if it has a left point and a right pointer and add them to the queue
if(node.left !== null){
queue.push(node.left)
}
if(node.right !== null){
queue.push(node.right)
}
}
// console.log("Total at this iteration:", total)
// console.log("Queue at this iteration:" , queue)
}
return total
}
addVals2(){
// Initialize a total var to 0
let total = 0
const stack = []
// numStack is for visual purposes(comment in lines 104, 110 && 123 to see)
// const numStack = []
stack.push(this.root)
while(stack.length){
// Take off the LAST element in the stack and save it to a var
// []
const node = stack.pop()
const num = node.val
// If the element is a node, add its val to the total
if(node !== null){
total += node.val
// numStack.push(num)
if(node.right !== null){
stack.push(node.right)
}
if(node.left !== null){
stack.push(node.left)
}
// console.log("Total at this iteration:", total)
// console.log("Queue at this iteration:" , stack)
// console.log(numStack)
}
}
return total
}
// Adding all the vals recursively
addVals3(node = this.root){
if(node === null) return 0
let total = node.val
// console.log(total, node.left, node.right)
return total + this.addVals3(node.left) + this.addVals3(node.right)
}
}
// 5
// / \
// 3 7
// / \ \
// 1 4 9
// \
// 2
const tree = new BinaryTree()
tree.insert(5)
tree.insert(3)
// tree.insert(7)
// tree.insert(1)
// tree.insert(4)
// tree.insert(9)
// tree.insert(2)
// tree.insert(11)
// console.log(inspect(tree, true, 10))
// console.log(tree.search(1))
// console.log(tree.addVals1())
// console.log(tree.addVals2())
console.log(tree.addVals3())