/
binarytree.js
173 lines (151 loc) · 4.2 KB
/
binarytree.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
var BinaryTree = function(identifier)
{
var self = this;
this.identifier = identifier;
var BNode = function(content)
{
var node = this;
node.parent = null
node.left = null;
node.right = null;
node.content = content;
if(node.content !== null && typeof node.content.BNodeMap === 'undefined')
{
Object.defineProperty(node.content, "BNodeMap", {
enumerable:false,
configurable:true,
writable:true,
value:{}
});
}
};
BNode.prototype.isALeaf = function() { return this.left == null && this.right == null; };
BNode.prototype.isAHead = function() { return this.parent == null; };
BNode.prototype.isLeftChild = function() { return this.parent.left == this; };
BNode.prototype.numChildren = function() { var num = 0; if(this.left != null) num++; if(this.right != null) num++; return num; };
self.head = null;
var findGreatestChildNode = function(head)
{
if(!head) return null;
var tmp;
while(tmp = head.right)
head = tmp;
return head;
};
var findLeastChildNode = function(head)
{
if(!head) return null;
var tmp;
while(tmp = head.left)
head = tmp;
return head;
};
var insertNode = function(node)
{
node.content.BNodeMap[self.identifier] = node;
if(!self.head) //I hate doing things like this...
{
self.head = node;
return;
}
var parentNode = null;
var tmpNode = self.head;
var evaluation = node.content.evaluate();
var tmpEvaluation = 0;
var lastCheckWasLeft = false;
//Non-recursive tree traversal
while(tmpNode)
{
if(tmpNode) tmpEvaluation = tmpNode.content.evaluate();
while(tmpNode && evaluation < tmpEvaluation)
{
parentNode = tmpNode;
tmpNode = parentNode.left;
if(tmpNode) tmpEvaluation = tmpNode.content.evaluate();
lastCheckWasLeft = true;
}
if(tmpNode) tmpEvaluation = tmpNode.content.evaluate();
while(tmpNode && evaluation >= tmpEvaluation)
{
parentNode = tmpNode;
tmpNode = parentNode.right;
if(tmpNode) tmpEvaluation = tmpNode.content.evaluate();
lastCheckWasLeft = false;
}
}
node.parent = parentNode;
if(parentNode && lastCheckWasLeft)
parentNode.left = node;
if(parentNode && !lastCheckWasLeft)
parentNode.right = node;
};
var moveContentToNode = function(fromNode, toNode) //overwrites toNode's content
{
toNode.content = fromNode.content;
toNode.content.BNodeMap[self.identifier] = toNode;
fromNode.content = null;
};
var removeNode = function(node) //turned out that a potentially recursive algorithm was just o(1)...? did I do something wrong?
{
delete node.content.BNodeMap[self.identifier];
if(node.numChildren() == 2)
{
var newNodeToDelete = findGreatestChildNode(node.left);
moveContentToNode(newNodeToDelete, node);
node = newNodeToDelete;
}
var child = null;
if(node.left != null)
child = node.left;
else if(node.right != null)
child = node.right;
if(node.parent)
{
if(node.isLeftChild())
node.parent.left = child;
else
node.parent.right = child;
}
if(child)
child.parent = node.parent;
if(!node.parent)
self.head = child;
return node;
};
self.add = function(content)
{
insertNode(new BNode(content));
};
self.remove = function(content)
{
removeNode(content.BNodeMap[self.identifier]);
};
self.popBiggest = function()
{
var n;
if(n = findGreatestChildNode(self.head))
return removeNode(n).content;
return null;
};
self.popSmallest = function()
{
var n;
if(n = findLeastChildNode(self.head))
return removeNode(n).content;
return null;
};
//recursive. sorry.
var appendChildrenAndSelfContentToOrderedList = function(node, list)
{
if(!node) return;
appendChildrenAndSelfContentToOrderedList(node.left, list);
list[list.length] = node.content;
appendChildrenAndSelfContentToOrderedList(node.right, list);
};
self.getOrderedList = function()
{
var list = [];
appendChildrenAndSelfContentToOrderedList(self.head, list);
return list;
};
};