forked from rolling-scopes-school/binary-tree
-
Notifications
You must be signed in to change notification settings - Fork 0
/
binary-tree.js
155 lines (134 loc) · 3.13 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
'use strict';
class BinaryTree {
constructor() {
this.root = null;
}
insert(data) {
var node = new Node(data, null, null);
if (this.root == null) {
return this.root = node;
}
var currentNode = this.root;
var parentNode = null;
while (currentNode) {
parentNode = currentNode;
if (data < currentNode.data) {
currentNode = currentNode.left;
if (currentNode == null) {
return parentNode.left = node;
}
} else {
currentNode = currentNode.right;
if (currentNode == null) {
parentNode.right = node;
}
}
}
}
contains(data) {
var found = false,
current = this.root;
while (!found && current) {
if (data < current.data) {
current = current.left;
}
else if (data > current.data) {
current = current.right;
}
else { found = true; }
}
return found;
}
remove(data) {
var contains = this.contains(data);
var found = !contains || this.root == null || data == null;
if (found) {
return false;
} else {
if (this.size() == 1) {
return (this.root = null);
}
var parentNode = function (node) {
var parent = node;
while (data != node.data) {
if (data < node.data) { parent = node; node = node.left;
}
else {
parent = node; node = node.right;
}
}
return parent;
}(this.root);
var removed = data < parentNode.data ? parentNode.left : parentNode.right;
// node has no children
if ((removed.left == null) && (removed.right == null)) {
if (data < parentNode.data) {parentNode.left = null;}
else { parentNode.right = null;}
return null;
}
// node has one child
if ((removed.left != null) || (removed.right != null)) {
// node has no right child
if (removed.left != null) {
if (data < parentNode.data) {parentNode.left = removed.left; }
else { parentNode.right = removed.left;}
return this.root;
}else {
// node has no left child
parentNode.right = removed.right;
if (data < parentNode.data) {
parentNode.left = removed.right;
}
else {
parentNode.right = removed.right;
}
return this.root;
}
}
// node has two children
if ((removed.left != null) && (removed.right != null)) {
if (data < this.root.data) {
var tempMin = this.getMin();
if (tempMin.right != null) {
tempMin = tempMin.right;
}
parentNode.left = tempMin;
} else if (data > this.root) {
var tempMax = this.getMax();
if (tempMax.left != null) {
tempMax = tempMax.left;
}
removed.data = tempMax.data;
}
return parentNode;
}
}
}
size() {
var treeSize = 0;
if (this.root == null) return 0;
treeSize = this.inOrder(this.root);
return treeSize;
}
inOrder(node) {
if (node == null) return 0;
return this.inOrder(node.left) + this.inOrder(node.right) + 1;
}
isEmpty() {
return this.root === null||this.size() == 0;
}
getMin(){
var current = this.root;
while (current.left != null) {
current = current.left;
}
return current;
}
getMax(){
var current = this.root;
while (!(current.right == null)) {
current = current.right;
}
return current.data;
}
}