-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary_search_tree.js
executable file
·139 lines (114 loc) · 3.18 KB
/
binary_search_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
#!/usr/bin/env node --harmony
var BinarySearchTree = function() {
this.root = null;
this._size = 0;
this.add = function(value) {
var newNode = { value: value, left: null, right: null, duplicates: 0 }
// easy
if (this.root === null) {
this.root = newNode;
// requires traversal
} else {
var currentNode = this.root;
while(true) {
// go to right, loop or assign
if (currentNode.value < newNode.value) {
if (currentNode.right) {
currentNode = currentNode.right;
} else {
currentNode.right = newNode;
this._size++;
break
}
// go to left, loop or assign
} else if (currentNode.value > newNode.value) {
if (currentNode.left) {
currentNode = currentNode.left;
} else {
currentNode.left = newNode;
this._size++;
break
}
// duplicate
} else {
// don't increment size though
currentNode.duplicates++;
break
}
}
}
return this;
}
this.contains = function(value) {
var found = false,
currentNode = this.root;
while(!found && currentNode) {
if (currentNode.value > value) {
currentNode = currentNode.left;
}
else if (currentNode.value < value) {
currentNode = currentNode.right;
}
else {
found = true;
}
}
return found;
}
this.remove = function(value) {
var found = false,
currentParent = null,
currentDirection = null;
currentNode = this.root;
// same setup, except keep track of a parent and direction as well.
while(!found && currentNode) {
if (currentNode.value > value) {
currentParent = currentNode;
currentDirection = 'left';
currentNode = currentNode.left;
}
else if (currentNode.value < value) {
currentParent = currentNode;
currentDirection = 'right';
currentNode = currentNode.right;
}
else {
found = true;
}
}
if (found) {
// algorithm for removal requires disconnecting and reconnecting nodes
// no children
if (!currentNode.left && !currentNode.right) {
// just delete the child, we know which side it was
// on because we kept track of direction
currentParent[currentDirection] = null
return true;
// has children
// 12
// 10 15
// 4 6
// killing 10 means:
// 12
// 6 15
// 4
//
} else {
// two children
if (currentNode.left && currentNode.right) {
// this is clearly more complicated
// TODO want food
// one child
} else {
// don't see why this wouldn't work just fine...
currentParent[currentDirection] = (currentNode.left||currentNode.right);
return true;
}
}
} else {
return false;
}
}
}
var mytree = new BinarySearchTree();
mytree.add(7).add(2).add(4).add(9).add(12).add(6).add(5).add(14).add(11);