-
Notifications
You must be signed in to change notification settings - Fork 0
/
Trees.js
165 lines (154 loc) · 4.61 KB
/
Trees.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
class Node {
constructor(value) {
this.leftdown = null;
this.rightdown = null;
this.value = value;
}
}
class BinarySearchTree {
constructor() {
this.root = null;
this.container = {};
}
reorder() {
const arrayArray = Object.entries(this.container);
let arrayClean = [];
for(let i = 0 ; arrayArray.length > i; i++) {
arrayClean[i] = arrayArray[i][1];
}
let middle = arrayArray.length
middle = parseInt(middle/2)
this.root = new Node(arrayClean[middle])
for(let i = 0; arrayClean.length > i; i++) {
this.insert(arrayClean[i]);
}
return arrayClean;
}
insert(value) {
if(value === Number) {
const newNode = new Node(value);
let currentNode = this.root;
if(!this.root) {
this.root = newNode;
this.container[this.root.value] = this.root.value
return this;
} else {
while(true) {
if (value == currentNode.value) {
return "If you want to update the node, please use 'update'"
}
if(value < currentNode.value) {
if (!currentNode.leftdown) {
currentNode.leftdown = newNode;
this.container[currentNode.leftdown.value] = currentNode.leftdown.value
return this;
} else {
currentNode = currentNode.leftdown;
}
} else {
if (!currentNode.rightdown) {
currentNode.rightdown = newNode;
this.container[currentNode.rightdown.value] = currentNode.rightdown.value
return this;
} else {
currentNode = currentNode.rightdown;
}
}
}
}
} else {
return `You need to enter data type Number, ${value} it's not valid`
}
}
update(value) {
if(value === Number) {
const newNode = new Node(value);
let currentNode = this.root;
if(!this.root) {
this.root = newNode;
return this;
} else {
while(true) {
if (value == currentNode.value) {
const beforeitem = currentNode;
currentNode = newNode;
return `Was update the before node ${beforeitem.value} to ${currentNode.value}`
} else if(value < currentNode.value) {
if (currentNode.leftdown) {
currentNode = currentNode.leftdown;
}
} else {
if (currentNode.rightdown) {
currentNode = currentNode.rightdown;
}
}
}
}
} else {
return `You need to enter data type Number, ${value} it's not valid`
}
}
search(value) {
if(value === Number) {
} else {
return `You need to enter data type Number, ${value} it's not valid`
}
try {
let currentNode = this.root;
if(value === Number) {
if(this.root) {
while(true) {
if(value == currentNode.value) {
return currentNode
} else if(value < currentNode.value) {
if (currentNode.leftdown) {
currentNode = currentNode.leftdown;
} else {
return `Node with value ${value} not was found`;
}
} else {
if (currentNode.rightdown) {
currentNode = currentNode.rightdown;
} else {
return `Node with value ${value} not was found`;
}
}
}
} else {
return "There're not data, use instance-->> .insert(value) to enter data"
}
} else {
return `You need to enter data type Number, ${value} it's not valid`
}
} catch(err) {
logMyErrors(err.prototype.message)
}
}
}
const tree = new BinarySearchTree();
tree.insert(2);
tree.insert(3);
tree.insert(12);
tree.insert(87);
tree.insert(7);
tree.insert(42);
tree.insert(54);
tree.insert(67);
tree.insert(2);
tree.insert(1);
tree.insert(6);
tree.insert(10);
tree.insert(3);
tree.search(b);
//CÓDIGO A PROBAR
tree.update(3);
tree.search(2);
tree.container
tree.reorder()
const binarySearchTree = new BinarySearchTree(1);
binarySearchTree.insert(10);
binarySearchTree.insert(2);
binarySearchTree.insert(4);
binarySearchTree.insert(20);
binarySearchTree.insert(10);
binarySearchTree.insert(170);