-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.ts
More file actions
130 lines (107 loc) · 3.52 KB
/
Copy pathBinarySearchTree.ts
File metadata and controls
130 lines (107 loc) · 3.52 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
/**
* Binary Search Tree.
*
* Binary Search Tree (BST) is a tree that has TWO child nodes at the most.
* The two nodes is identified as: left and right node.
*
* The left of the two notes always has a smaller value than the right.
* This applies to nodes and the entire sub-tree (increases speed of search).
*
*/
class TreeNode {
value: number;
leftNode: TreeNode;
rightNode: TreeNode;
constructor(value: number) {
this.value = value;
this.leftNode = null;
this.rightNode = null;
}
}
class BinarySearchTree {
root: TreeNode;
constructor() {
this.root = null;
}
addNode(node: TreeNode): boolean {
if (this.root == null) {
this.root = node;
return true;
}
let currentNode = this.root;
let traveringTree = true;
while (traveringTree) {
if (currentNode.value == node.value) {
// No duplicates.
traveringTree = false;
return false;
} else if (node.value < currentNode.value) {
if (currentNode.leftNode == null) {
// I had no child. Please give me.
currentNode.leftNode = node;
traveringTree = false;
return true;
} else {
// I am smaller. Look to the left.
currentNode = currentNode.leftNode;
}
} else if (node.value > currentNode.value) {
// Look to the right.
if (currentNode.value == null) {
// I had no right child. Plox provide.
currentNode.rightNode = node;
traveringTree = false;
return true;
} else {
currentNode = currentNode.rightNode;
}
}
}
}
/**
* Breadth-First search.
*
* Looks in the width of the tree.
* That means at all child nodes at one level before moving on.
*/
breadthFirstSearch(): number[] {
let nodesToVisit: TreeNode[] = [];
let visited: number[] = [];
nodesToVisit.push(this.root);
while(nodesToVisit.length >= 1) {
let node = nodesToVisit.shift();
visited.push(node.value);
if (node.leftNode != null) {
nodesToVisit.push(node.leftNode);
}
if (node.rightNode != null) {
nodesToVisit.push(node.rightNode);
}
}
return visited;
}
/**
* Depth First Search
*
* Starting at the root node,
* keeps going left until it hits the last leaf.
*/
depthFirstSearch(): number[] {
let nodesToVisit: TreeNode[] = [];
let visited: number[] = [];
nodesToVisit.push(this.root);
while (nodesToVisit.length >= 1) {
let node = nodesToVisit.pop();
visited.push(node.value);
if (node.rightNode != null) {
nodesToVisit.push(node.rightNode);
}
// Add the left node to the stack. We add left child after the rigth child node into the stack.
// This is because we have to traverse left subtree before traversing right sub tree.
if (node.leftNode != null) {
nodesToVisit.push(node.leftNode);
}
}
return visited;
}
}