-
Notifications
You must be signed in to change notification settings - Fork 0
/
22 Binary Search Trees.js
404 lines (343 loc) · 8.64 KB
/
22 Binary Search 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/* BINARY SEARCH TREES */
/* Introduction to Trees */
/* TREES
OBJECTIVES
* Define what a tree is
* Compare and contrast trees and lists
* Explain the differences between trees, binary trees, and binary search trees
* Implement operations on binary search trees
WHAT IS A TREE?
A data structure that consists of nodes in a parent / child relationship
* ______2______
* / / / \ \
* 9 12 8 99 10
* / /|\ / \ / \
* 2 1 7 2 44 11 55 87
(!) Lists - linear
(!) Trees - nonlinear
A Singly Linked List
(sort of a special case of a tree)
* Tree
* 2
* \ Singly Linked List
* 12 2 -> 12 -> 11
* \
* 11
* Not a tree
* ______ 2 ______
* / / \ \
* 9 -> 8 -> 99 -> 10
* Not a tree
* 2 ____ 8 ______
* / / \ \
* 9 -> 7 -> 99 -> 10
TREE TERMINOLOGY
Root - The top node in a tree.
Child -A node directly connected to another node when moving away from the Root.
Parent - The converse notion of a child.
Siblings -A group of nodes with the same parent.
Leaf - A node with no children.
Edge - The connection between one node and another.
*/
/* Uses For Trees */
/*
Trees have lots of different applications!
HTML DOM
Network Routing
Abstract Syntax Tree
Artificial Intelligence
Folders in Operating Systems
Computer File Systems
*/
/* Intro to Binary Trees */
/*
KINDS OF TREES
(>) Trees
(>) Binary Trees
(>) Binary Search Trees
BINARY TREES
* 1
* / \
* 5 12
* / \ \
* 6 3 11
NOT A BINARY TREE
* 1
* / | \
* 5 9 12
* / \ \
* 6 3 11
BINARY TREES
Lots of different applications as well!
(+) Decision Trees (true / false)
(+) Database Indicies
(+) Sorting Algorithms
BINARY SEARCH TREES
* 10
* / \
* 6 15
* / \ \
* 3 8 20
HOW BSTS WORK
(>) Every parent node has at most two children
(>) Every node to the left of a parent node is always less than the parent
(>) Every node to the right of a parent node is always greater than the parent
* Non-valid BST
* 10
* / \
* 8 15
* / \ \
* 3 6 20
*
* Non-valid BST
* 10
* / | \
* 8 4 15
* / \
* 3 20
* \
* 6
* Non-valid BST
* 10
* / \
* 8 15
* / \
* 3 20
* / \
* 4 6
/* Our Tree Classes */
/*
* The BinarySearchTree Class
* class BinarySearchTree {
* constructor(){
* this.root = null;
* }
* }
* class Node {
* constructor(value){
* this.value = value;
* this.left = null;
* this.right = null;
* }
* }
*
* var tree = new BinarySearchTree();
* tree.root = new Node(10);
* tree.root.right = new Node(15);
* tree.root.left = new Node(7);
* tree.root.left.right = new Node(9);
*
/* BTS Insert */
/* INSERTING
*
* 10 <- 13
* / \
* 6 15
* / \ \
* 3 8 20
* 10
* / \
* 6 15
* / \ / \
* 3 8 13 20
INSERTING A NODE
( Steps - Iteratively or Recursively )
* (1) Create a new node
* (2) Starting at the root
* (2.1) Check if there is a root, if not -
* the root now becomes that new node!
* (2.2) If there is a root, check if the value
* of the new node is greater than or less
* than the value of the root
* (2.3) If it is greater:
* (2.3a) Check to see if there is a node to the right
* (2.3aa) If there is, move to that node and repeat
* these steps
* (2.3ab) If there is not, add that node as the
* right property
* (2.4) If it is less:
* (2.4a) Check to see if there is a node to the left
* (2.4aa) If there is, move to that node and repeat
* these steps
* (2.4ab) If there is not, add that node as the
* left property
*/
(()=>{
// class Node {
// constructor(value){
// this.value = value;
// this.left = null;
// this.right = null;
// }
// }
// class BinarySearchTree {
// constructor(){
// this.root = null;
// }
// insert(value){
// var newNode = new Node(value);
// if(this.root === null){
// this.root = newNode;
// return this;
// }
// var current = this.root;
// while(true){
// if(value === current.value) return undefined;
// if(value < current.value){
// if(current.left === null){
// current.left = newNode;
// return this;
// }
// current = current.left;
// } else {
// if(current.right === null){
// current.right = newNode;
// return this;
// }
// current = current.right;
// }
// }
// }
// }
// // 10
// // 5 13
// // 2 7 11 16
// var tree = new BinarySearchTree();
// tree.insert(10)
// tree.insert(5)
// tree.insert(13)
// tree.insert(11)
// tree.insert(2)
// tree.insert(16)
// tree.insert(7)
})();
/*
Finding a Node in a BST
( Steps - Iteratively or Recursively )
Starting at the root:
(1) Check if there is a root, if not - we're done
searching!
(2) If there is a root, check if the value of the
new node is the value we are looking for.
If we found it, we're done!
(3) If not, check to see if the value is greater
than or less than the value of the root
(4) If it is greater:
(4.1) Check to see if there is a node to the right
(4.1a) If there is, move to that node and repeat
these steps
(4.1b) If there is not, we're done searching!
(5) If it is less:
(5.1) Check to see if there is a node to the left
(5.1a) If there is, move to that node and repeat
these steps
(5.1b) If there is not, we're done searching!
*/
(()=>{
// class Node {
// constructor(value){
// this.value = value;
// this.left = null;
// this.right = null;
// }
// }
// class BinarySearchTree {
// constructor(){
// this.root = null;
// }
// insert(value){
// var newNode = new Node(value);
// if(this.root === null){
// this.root = newNode;
// return this;
// }
// var current = this.root;
// while(true){
// if(value === current.value) return undefined;
// if(value < current.value){
// if(current.left === null){
// current.left = newNode;
// return this;
// }
// current = current.left;
// } else {
// if(current.right === null){
// current.right = newNode;
// return this;
// }
// current = current.right;
// }
// }
// }
// find(value){
// if(this.root === null) return false;
// var current = this.root,
// found = false;
// while(current && !found){
// if(value < current.value){
// current = current.left;
// } else if(value > current.value){
// current = current.right;
// } else {
// found = true;
// }
// }
// if(!found) return undefined;
// return current;
// }
// contains(value){
// if(this.root === null) return false;
// var current = this.root,
// found = false;
// while(current && !found){
// if(value < current.value){
// current = current.left;
// } else if(value > current.value){
// current = current.right;
// } else {
// return true;
// }
// }
// return false;
// }
// }
// // 10
// // 5 13
// // 2 7 11 16
// var tree = new BinarySearchTree();
// tree.insert(10)
// tree.insert(5)
// tree.insert(13)
// tree.insert(11)
// tree.insert(2)
// tree.insert(16)
// tree.insert(7)
})()
/* Big O of Binary Search Tree */
/*
Big O of BST
Insertion - O(log n)
Searching - O(log n)
NOT guaranteed!
Double the number of nodes...
- You only increase the number of steps to
insert/find by 1
2x number of nodes: 1 extra step
4x number of nodes: 2 extra steps
8x number of nodes: 3 extra steps
THIS IS A VALID BINARY SEARCH TREE
* 3
* \
* 17
* \
* 19
* \
* 32
* \
* 34
* \
* 63
* \
* 86
* \
* 91