-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinaryTreeSearch.js
More file actions
125 lines (118 loc) · 3.59 KB
/
binaryTreeSearch.js
File metadata and controls
125 lines (118 loc) · 3.59 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
class Node{
constructor(value , row, col){
this.value = value;
this.left = null;
this.right = null;
}
}
//export default
class BinarySearchTree {
constructor(){
this.root = null;
};
add( value ){
this.root = addWithin( this.root,value);
function addWithin(node,value){
if( !node) return new Node(value);
if ( value < node.value ){
node.left = addWithin(node.left,value);
}
else if ( value > node.value) {
node.right = addWithin(node.right,value);
}
return node;
}
};
root() { return this.root};
has(data){
let current = this.root;
return hasValue(current,data);
function hasValue(node, value) {
if (!node) return false;
if ( node.value> value ){
return hasValue(node.left,value );
}
else if (node.value<value ){
return hasValue(node.right,value);
}
else return true;
}
}
find(data){
return findElement(this.root,data);
function findElement(node, value){
if (!node) return null;
if (value<node.value){
return findElement(node.left,value);
}
else if (value>node.value){
return findElement(node.right,value)
}
else return node;
}
}
get toString(){
let level = 0;
print(this.root,'heder');
function print(node, type = 'left') {
if ( !node) return;
console.log( 'level '+type+' is:---',level++,'-----value-----',node.value);
print(node.left); print(node.right,'right'); level--;
}
}
remove(data){
let deleteNode = null;
this.root = removeData(this.root,data);
function removeData(node,value){
if (!node) return null;
if (value<node.value ){
node.left= removeData(node.left,value);
return node;
}
else if (value>node.value){
node.right = removeData(node.right,value);
return node;
}
else{
deleteNode = node;
//equal
if (!node.left && !node.right ) {deleteNode = node; return null;}
if ( !node.left ) return node.right;
else if (!node.right) return node.left;
else {
let minFromRoot = node.right;
while (minFromRoot.left){
minFromRoot = minFromRoot.left;
}
minFromRoot.left = deleteNode.left;
minFromRoot.right = deleteNode.right;
return minFromRoot;
}
}
}
return deleteNode;
}
isEmpty(){ return (!this.root?true:false)}
min(){
if (this.isEmpty()) return null;
let minResult = this.root.value;
findMinimum( this.root);
function findMinimum( node){
if (!node) return;
if( minResult>node.value) minResult = node.value;
findMinimum(node.left); findMinimum(node.right);
}
return minResult;
}
max(){
if (this.isEmpty()) return null;
let maxResult = this.root.value;
findMinimum( this.root);
function findMinimum( node){
if (!node) return;
if( maxResult<node.value) maxResult = node.value;
findMinimum(node.left); findMinimum(node.right);
}
return maxResult;
}
}