-
Notifications
You must be signed in to change notification settings - Fork 3
/
binarytrees.js
121 lines (105 loc) · 3.62 KB
/
binarytrees.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
// Binary Search Tree
// a BINARY tree is a tree that has ONLY AT MOST TWO children, so we have a left pointer and right pointer
class BinaryTreeNode {
constructor(data){
this.data = data
this.left = null
this.right = null
}
}
class BinarySearchTree {
constructor(root=null){
this.root = root
}
insert(data){
// Create a new instance of the BinaryTreeNode class using the data from the param
const newTreeNode = new BinaryTreeNode(data)
if(!this.root){
this.root = newTreeNode
} else {
let currentNode = this.root
while(currentNode){
// check the newTreeNode against the currentNode
if(newTreeNode.data < currentNode.data){
// if currentNode.left is null then assign the newTreeNode to be current.left
if(!currentNode.left){
currentNode.left = newTreeNode
break
} else {
// reassign currentNode to currentNode.left so we can keep iterating
currentNode = currentNode.left
}
} else {
// newTreeNode.data is larger than currentNode.data
if(!currentNode.right){
currentNode.right = newTreeNode
break
} else {
currentNode = currentNode.right
}
}
}
}
}
search(data){
let currentNode = this.root
while(currentNode){
// if the currentNode's data is equal to data from the param
if(currentNode.data === data){
// return that node
return currentNode
} else if(data < currentNode.data){
// else if data is less currentNode's data
// reassign currentNode to be currentNode.left
currentNode = currentNode.left
} else {
// reassign currentNode to be currentNode.right
currentNode = currentNode.right
}
}
return null
}
insert2(value){
const newNode = new BinaryTreeNode(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 data to newNode's data
if(newNode.data < node.data){
// 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)
}
}
}
}
const firstTreeNode = new BinaryTreeNode(10)
// console.log(firstTreeNode)
const bst = new BinarySearchTree(firstTreeNode)
bst.insert(5)
bst.insert(12)
// bst.insert(7)
// bst.insert(18)
// console.log(bst)
const { inspect } = require('util')
// console.log( bst.search(7) )
// console.log( bst.search(6) )
bst.insert2(7)
// console.log(bst)
console.log( inspect(bst, { "depth": null, "colors": true }) )