-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBSTree.js
More file actions
177 lines (149 loc) · 4.56 KB
/
BSTree.js
File metadata and controls
177 lines (149 loc) · 4.56 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
class BSNode {
// each node in a binary search tree contains a value, a pointer to the node to the left, and a pointer to the node to the right
constructor(value){
this.value = value;
this.left = null;
this.right = null;
}
}
class BSTree {
constructor(){
this.root = null;
}
//<--------------------------------------------------------------------------<<<<<
printTree() {
if(this.root == null) {
console.log("Tree is empty.");
return this;
}
this.printHelper();
}
printHelper(toPrint = "", runner = this.root) {
if(runner == null) {
return this;
}
toPrint += "\t";
this.printHelper(toPrint, runner.right);
console.log(`${toPrint}${runner.value}`);
this.printHelper(toPrint, runner.left);
}
//<-------------------------------------------------------------------------<<<<<
// write an algorithm that can determine whether or not a binary search tree is empty.
isEmpty() {
if(this.root == null) {
return true;
}
return false;
}
// write an algorithm that will find the smallest number in a binary search tree
min(){
if(this.isEmpty()) {
return false;
}
let runner = this.root;
while(runner.left != null) {
runner = runner.left;
}
return runner.value;
}
// write an algorithm that will find the largest number in a binary search tree
max() {
if(this.isEmpty()) {
return false;
}
let runner = this.root;
while(runner.right != null) {
runner = runner.right;
}
return runner.value;
}
// 4/28 BONUS: Write an algorithm that can determine whether or not the binary search tree
// contains a node with a given value
contains(value, runner = this.root) {
if(runner == null)
return false;
else if(value == runner.value)
return true;
else if(value < runner.value)
return this.contains(value, runner.left);
else
return this.contains(value, runner.right);
}
// Write an algorithm that will add a new node to a Binary Search Tree
add(value) {
if(this.root == null) {
this.root = new BSNode(value);
return this;
}
return this.addHelper(value, this.root);
}
addHelper(value, runner) {
if(value >= runner.value) {
if(runner.right == null) {
runner.right = new BSNode(value);
return this;
}
return this.addHelper(value, runner.right);
}
else {
if(runner.left == null) {
runner.left = new BSNode(value);
return this;
}
return this.addHelper(value, runner.left);
}
}
// Write an algorithm that will find the minimum value of the right subtree
minRight(node = this.root) {
if(node == null || node.right == null) {
return false;
}
return this.min(node.right);
}
// Write an algorithm that will find the maximum value of the left subtree
maxLeft(node = this.root) {
if(node == null || node.left == null) {
return false;
}
return this.max(node.left);
}
//Write an algorithm that finds the height of the Binary Search Tree
height(runner = this.root, height = 0) {
if(runner == null) {
return height;
}
let rightHeight = this.height(runner.right, height+1);
let leftHeight = this.height(runner.left, height+1);
if(leftHeight >= rightHeight)
return leftHeight;
return rightHeight;
}
//Write an algorithm that finds whether or not the Binary Search Tree is balanced
isBalanced() {
if(this.root == null || (this.root.right == null && this.root.left == null)) {
return true;
}
else if(Math.abs(this.height(this.root.right) - this.height(this.root.left)) <= 1) {
return true;
}
else {
return false;
}
}
// Write an algorithm that determines if a BST is full
isFull(runner = this.root) {
}
}
let testBST = new BSTree();
testBST.add(25);
testBST.add(6);
testBST.add(5);
testBST.add(15);
testBST.add(100);
testBST.add(21);
testBST.printTree();
// console.log(testBST.contains(15));
// console.log(testBST.max());
// console.log(testBST.maxLeft());
// console.log(testBST.min());
// console.log(testBST.minRight());