-
Notifications
You must be signed in to change notification settings - Fork 26
/
binary-tree.js
182 lines (181 loc) · 4.78 KB
/
binary-tree.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
176
177
178
179
180
181
182
class Node {
constructor(data) {
this.data = data;
this.left = null;
this.right = null;
}
}
class BinaryTree {
constructor() {
this.root = null;
this.size = 0;
}
add(data) {
const newNode = new Node(data);
if (this.root === null) {
this.root = newNode;
this.size++;
}
let current = this.root;
while (true) {
if (data < current.data) {
if (current.left === null) {
current.left = newNode;
this.size++;
break;
} else {
current = current.left;
}
} else if (data > current.data) {
if (current.right === null) {
current.right = newNode;
this.size++;
break;
} else {
current = current.right;
}
} else {
break;
}
}
}
/**
* Максимальный узел в дереве
* @memberof BinaryTree
*/
getMax() {
let current = this.root;
while (current.right !== null) {
current = current.right;
}
return current.data;
}
/**
* Минимальный узел в дереве
* @memberof BinaryTree
*/
getMin() {
let current = this.root;
while (current.left !== null) {
current = current.left;
}
return current.data;
}
/**
* Количество узлов в дереве
* @memberof BinaryTree
*/
size() {
return this.size;
}
find(data) {
let current = this.root;
while (current.data !== data) {
if (data < current.data) {
current = current.left;
} else {
current = current.right;
}
if (current === null) {
return false;
}
}
return true;
}
/**
* Прямой обход дерева
* @param {any} node
* @memberof BinaryTree
*/
preOrder(node) {
if (node === null) {
return;
}
console.log(node.data);
this.preOrder(node.left);
this.preOrder(node.right);
}
/**
* Симметричный обход дерева
* @param {any} node
* @memberof BinaryTree
*/
inOrder(node) {
if (node === null) {
return;
}
this.inOrder(node);
console.log(node.data);
this.inOrder(node);
}
/**
* Обратный обход дерева
* @param {any} node
* @memberof BinaryTree
*/
postOrder(node) {
if (node === null) {
return;
}
this.inOrder(node);
this.inOrder(node);
console.log(node.data);
}
/**
* Обход дерева в ширину
* @param {any} node
* @memberof BinaryTree
*/
bfs(node) {
let queue = [];
let values = [];
queue.push(node);
while (queue.length > 0) {
let current = queue.shift();
values.push(current.data);
if (current.left) {
queue.push(current.left);
}
if (current.right) {
queue.push(current.right);
}
}
return values;
}
remove(data) {
const removeNode = function(node, data) {
if (node == null) {
return null;
}
if (data == node.data) {
// У узла нет детей
if (node.left === null && node.right === null) {
return null;
}
// У узла только правый ребенок
if (node.left === null) {
return node.right;
}
// У узла только левый ребенок
if (node.right === null) {
return node.left;
}
// У узла двое детей
var current = node.right;
while (current.left !== null) {
current = current.left;
}
node.data = current.data;
node.right = removeNode(node.right, current.data);
return node;
} else if (data < node.data) {
node.left = removeNode(node.left, data);
return node;
} else {
node.right = removeNode(node.right, data);
return node;
}
};
this.root = removeNode(this.root, data);
}
}