-
Notifications
You must be signed in to change notification settings - Fork 0
/
binaryTree.js
175 lines (147 loc) · 3.75 KB
/
binaryTree.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
class Node {
constructor(data) {
this.data = data;
this.right = null;
this.left = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
}
insert(data) {
let stack = [];
let current = this.root;
stack.push(current);
if (current === null && current === this.root) {
this.root = new Node(data);
return;
}
while (stack.length > 0) {
current = stack[0];
stack.shift();
if (!current.left) {
current.left = new Node(data);
return;
}
stack.push(current.left);
if (!current.right) {
current.right = new Node(data);
return;
}
stack.push(current.right);
}
}
randomInsert(data) {
let current = this.root;
const DIRECTION = ["left", "right"];
if (!current && current === this.root) {
this.root = new Node(data);
return;
}
while (current) {
let index = Math.floor(Math.random() * 2);
if (!current.left) {
current.left = new Node(data);
return;
}
if (!current.right) {
current.right = new Node(data);
return;
}
if (DIRECTION[index] === "left") {
current = current.left;
} else {
current = current.right;
}
}
}
replaceRootRight(data) {
const temp = this.root;
this.root = new Node(data);
this.root.right = temp;
}
replaceRootLeft(data) {
const temp = this.root;
this.root = new Node(data);
this.root.left = temp;
}
insertRight(data) {
let current = this.root;
while (current) {
if (!current.right) {
break;
}
current = current.right;
}
current.right = new Node(data);
}
insertLeft(data) {
let current = this.root;
while (current) {
if (!current.left) {
break;
}
current = current.left;
}
current.left = new Node(data);
}
// inorderTraversal() {
// //log out data in tree in left node, root, right node sequence
// let current = this.root;
// let pre;
// while (current) {
// //while current is not null
// if (!current.left) {
// // check if current.left is empty
// console.log(current.data); //if current.left is empty display its data
// current = current.right; // assign current to the current.right since we are at the end of the available left nodes
// } else {
// // if current.left is not empty
// pre = current.left; // assign current.left to a new variable
// while (pre.right && pre.right !== current) {
// //loop till pre.right is null or pre.right is the same as current break out of loop otherwise
// pre = pre.right; //while the condition is true assign the right node of the new variable to the variable
// }
// if (!pre.right) {
// //check if pre.right is empty
// pre.right = current; // if it is empty assign current from line 88 to pre.right
// current = current.left; // reassign current to the left node of current from line 88 to pre
// } else {
// //if pre.right is not empty
// pre.right = null; // reassign that node as empty
// console.log(current.data); // log out current from line 88 data
// current = current.right; // reassign current to current.right
// }
// }
// }
// }
inorderTraversal(root, stack = []) {
if (root) {
this.inorderTraversal(root.left, stack);
stack.push(root.data);
this.inorderTraversal(root.right, stack);
}
return stack;
}
preorderTraversal(root, stack = []) {
if (root) {
stack.push(root.data);
this.preorderTraversal(root.left, stack);
this.preorderTraversal(root.right, stack);
}
return stack;
}
postorderTraversal(root, stack = []) {
if (root) {
this.postorderTraversal(root.left, stack);
this.postorderTraversal(root.right, stack);
stack.push(root.data);
}
return stack;
}
search(key) {
const stack = this.inorderTraversal(this.root);
return stack.some((value) => value === key);
}
}