-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarySearchTree.js
More file actions
154 lines (134 loc) · 4 KB
/
Copy pathbinarySearchTree.js
File metadata and controls
154 lines (134 loc) · 4 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
class BST{
constructor(){
this.root = null;
}
// insert in tree
insert(val){
let newNode = new Node(val);
if(!this.root){
this.root = newNode;
}else{
let temp = this.root;
let loop = true;
while(temp && loop){
if(val > temp.val){
if(temp.right){
temp = temp.right;
}else{
temp.right = newNode;
loop = false;
}
}else if(val < temp.val){
if(temp.left){
temp = temp.left;
}else{
temp.left = newNode;
loop = false;
}
}else{
return undefined;
}
}
}
}
// find if the tree has a node
contains(val){
if(!this.root){
return false;
}else{
let temp = this.root;
while(temp){
if(val > temp.val){
if(temp.right){
temp = temp.right;
}else{
return false;
}
}else if(val < temp.val){
if(temp.left){
temp = temp.left;
}else{
return false;
}
}else if(val === temp.val){
return true;
}
}
}
}
// traverse
traverse(){
if(!this.root){
return [];
}else{
let q = [];
q.push(this.root);
let list = [];
while(q.length > 0){
let temp = q.shift();
list.push(temp.val);
if(temp.left){
q.push(temp.left);
}
if(temp.right){
q.push(temp.right);
}
}
return list;
}
}
// remove node from the tree
remove(val) {
let currentNode, parentNode, nextBiggestParentNode=null, found=false, base=[this.root];
while(base.length > 0 && !found) {
currentNode = base.pop();
if(currentNode.val === val) {
found=true;
if(!currentNode.left && !currentNode.right) {
parentNode.right === currentNode ? parentNode.right = null : parentNode.left = null;
}
else if(!currentNode.right && currentNode.left) {
parentNode.right === currentNode ? parentNode.right = currentNode.left : parentNode.left = currentNode.left;
}
else if(!currentNode.left && currentNode.right) {
parentNode.right === currentNode ? parentNode.right = currentNode.right : parentNode.left = currentNode.right;
}
else {
let _traverse = node => {
if (node.right) {
nextBiggestParentNode = node;
_traverse(node.right);
}
else {
currentNode.val = node.val;
nextBiggestParentNode ? nextBiggestParentNode.right = null : currentNode.left = null;
}
}
_traverse(currentNode.left);
}
}
else {
parentNode = currentNode;
val > currentNode.val && currentNode.right ? base.unshift(currentNode.right) : base.unshift(currentNode.left);
}
}
return this;
}
}
///////
class Node{
constructor(val){
this.val = val;
this.left = null;
this.right = null;
}
}
////////
let bst = new BST();
bst.insert(10);
bst.insert(15);
bst.insert(7);
bst.insert(99);
bst.insert(9);
bst.insert(7.2);
console.log(bst.traverse());