-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.js
More file actions
140 lines (123 loc) · 3.49 KB
/
Copy pathbinary_tree.js
File metadata and controls
140 lines (123 loc) · 3.49 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
`use strict`;
function Node(data) {
this.data = data;
this.left = null;
this.right = null;
}
function BinarySearchTree() {
this.root = null;
}
BinarySearchTree.prototype.add = function(data) {
const node = new Node(data);
if (!this.root) {
this.root = node;
return this;
}
let current = this.root;
while (current) {
if (node.data < current.data) {
if (!current.left) {
current.left = node;
break;
}
current = current.left;
} else if (node.data > current.data) {
if (!current.right) {
current.right = node;
break;
}
current = current.right;
} else {
break;
}
}
return this;
};
function nextMinNode(node) {
let minNode;
let current = node.right;
if (!current.left) {
minNode = current;
current = removeNode(current);
} else {
while (current.left.left) {
current = current.left;
}
minNode = current.left;
current.left = removeNode(current.left);
}
return minNode;
}
function removeNode(node) {
if (!node.left && !node.right) {
return null;
} else if (node.left && !node.right) {
return node.left;
} else if (!node.left && node.right) {
return node.right;
} else if (node.left && node.right) {
const newNode = nextMinNode(node);
newNode.left = node.left;
newNode.right = node.right;
return newNode;
}
}
BinarySearchTree.prototype.remove = function(data) {
let current = this.root;
if (current.data === data) {
this.root = removeNode(current);
return this;
}
while (current) {
if (data < current.data && data !== current.left.data) {
current = current.left;
} else if (data > current.data && data !== current.right.data) {
current = current.right;
} else if (data < current.data && data == current.left.data) {
current.left = removeNode(current.left);
return this;
} else if (data > current.data && data == current.right.data) {
current.right = removeNode(current.right);
return this;
}
}
console.log("Value wasn't founded.");
};
BinarySearchTree.prototype.search = function(data) {
let current = this.root;
if (current.data === data) {
return true;
}
while (current) {
if (data < current.data) {
current = current.left;
} else if (data > current.data) {
current = current.right;
} else {
return true;
}
}
return false;
};
BinarySearchTree.prototype.goRound = function() {
let current;
const searched = [],
stack = [this.root];
while (stack[0] !== undefined) {
current = stack.pop();
searched.push(current.data);
if (current.right) {
stack.push(current.right);
}
if (current.left) {
stack.push(current.left);
}
}
return searched;
};
const bst = new BinarySearchTree();
bst.add(8).add(3).add(10).add(1).add(6).add(4).add(7).add(10).add(14).add(13);
// bst.search(7);
// bst.remove(3);
// bst.goRound();
console.log(bst);