Skip to content

Commit 68650e9

Browse files
committed
Created binary search tree classes
1 parent c72f6bf commit 68650e9

File tree

2 files changed

+272
-0
lines changed

2 files changed

+272
-0
lines changed

BSTree/BSTree.js

Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
/**
2+
* Created by Stefano on 05/04/2014.
3+
*/
4+
5+
/**
6+
* The single node of the tree.
7+
* @param key {number} The key of the node.
8+
* @param item {*} The item to store in the node.
9+
* @constructor
10+
*/
11+
function BSNode(key, item) {
12+
/**
13+
* The item stored.
14+
* @type {*}
15+
*/
16+
this.item = item;
17+
/**
18+
* The key of the node.
19+
* @type {number}
20+
*/
21+
this.key = key;
22+
/**
23+
* The parent node. It's null if there's no a parent node.
24+
* @type {BSNode|null}
25+
*/
26+
this.parent = null;
27+
/**
28+
* The left node. It's null if there's no a left node.
29+
* @type {BSNode|null}
30+
*/
31+
this.left = null;
32+
/**
33+
* The right node. It's null if there's no a right node.
34+
* @type {BSNode|null}
35+
*/
36+
this.right = null;
37+
}
38+
39+
BSTree.prototype = new Aggregate();
40+
BSTree.prototype.constructor = BSTree;
41+
42+
function BSTree() {
43+
/**
44+
* The root of the tree.
45+
* @type {BSNode|null}
46+
*/
47+
this.root = null;
48+
}
49+
50+
/**
51+
* @inheritDoc
52+
*/
53+
BSTree.prototype.getIterator = function () {
54+
return new BSTreeIterator(this);
55+
};
56+
57+
/**
58+
* Insert the item relatives to the key value in the tree.
59+
* @param key {number} The key to store.
60+
* @param item {*} The item to store.
61+
* @return {void}
62+
*/
63+
BSTree.prototype.insert = function (key, item) {
64+
var node = new BSNode(key, item);
65+
var p = this.root;
66+
for (var n = this.root; n;) {
67+
p = n;
68+
if (key < n.key)
69+
n = n.left;
70+
else
71+
n = n.right;
72+
}
73+
node.parent = p;
74+
if (!p)
75+
this.root = node;
76+
else if (key < p.key)
77+
p.left = node;
78+
else
79+
p.right = node;
80+
};
81+
82+
/**
83+
* Search the item relatives to the key.
84+
* @param key {Number} The key to find.
85+
* @param [node = root] {BSNode} The node from which start the search.
86+
* @return {*} The item found or undefined if there isn't the key in the tree.
87+
*/
88+
BSTree.prototype.search = function (key, node) {
89+
node = node || this.root;
90+
while (node && key !== node.key)
91+
if (key < node.key)
92+
node = node.left;
93+
else
94+
node = node.right;
95+
if (node)
96+
return node.item;
97+
return undefined;
98+
};
99+
100+
/**
101+
* Get the item relatives to the minimum key stored in the tree.
102+
* @param [node = root] {Node} The node from which start the search.
103+
* @return {BSNode} The node found.
104+
*/
105+
BSTree.prototype.minimum = function (node) {
106+
node = node || this.root;
107+
while (node && node.left)
108+
node = node.left;
109+
return node;
110+
};
111+
112+
/**
113+
* Get the item relatives to the maximum key stored in the tree.
114+
* @param [node = root] {Node} The node from which start the search.
115+
* @return {BSNode} The node found.
116+
*/
117+
BSTree.prototype.maximum = function (node) {
118+
node = node || this.root;
119+
while (node && node.right)
120+
node = node.right;
121+
return node;
122+
};
123+
124+
/**
125+
* Get the node with the key next to the param node key.
126+
* @param node {BSNode} The node of which search the successor.
127+
* @return {BSNode} The node found.
128+
*/
129+
BSTree.prototype.successor = function (node) {
130+
if (node.right)
131+
return this.minimum(node.right);
132+
var parent = node.parent;
133+
while (parent && node === parent.right) {
134+
node = parent;
135+
parent = parent.parent;
136+
}
137+
return parent;
138+
};
139+
140+
/**
141+
* Get the node with the key previous to the param node key.
142+
* @param node {BSNode} The node of which search the predecessor.
143+
* @return {BSNode} The node found.
144+
*/
145+
BSTree.prototype.predecessor = function (node) {
146+
if (node.left)
147+
return this.maximum(node.left);
148+
var parent = node.parent;
149+
while (parent && node === parent.left) {
150+
node = parent;
151+
parent = parent.parent;
152+
}
153+
return parent;
154+
};
155+
156+
/**
157+
* Delete the node from the tree.
158+
* @param node {BSNode} The node to delete.
159+
* @return {void}
160+
*/
161+
BSTree.prototype.deleteNode = function (node) {
162+
if (!node.left && !node.right) {
163+
if (node === this.root)
164+
this.root = null;
165+
else if (node.parent.left === node)
166+
node.parent.left = null;
167+
else
168+
node.parent.right = null;
169+
} else if (node.left && node.right) {
170+
var next = this.successor(node);
171+
node.key = next.key;
172+
node.item = next.item;
173+
if (next.parent.left === next)
174+
next.parent.left = null;
175+
else
176+
next.parent.right = null;
177+
} else {
178+
if (node.right) {
179+
if (node === this.root) {
180+
this.root = node.right;
181+
node.right.parent = null;
182+
} else {
183+
node.parent.right = node.right;
184+
node.right.parent = node.parent;
185+
}
186+
} else {
187+
if (node === this.root) {
188+
this.root = node.left;
189+
node.left.parent = null;
190+
} else {
191+
node.parent.left = node.left;
192+
node.left.parent = node.parent;
193+
}
194+
}
195+
}
196+
};

BSTree/BSTreeIterator.js

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/**
2+
* Created by Stefano on 06/04/2014.
3+
*/
4+
5+
BSTreeIterator.prototype = new Iterator();
6+
BSTreeIterator.prototype.constructor = BSTreeIterator;
7+
8+
/**
9+
* Class that implements the iterator for a binary search tree.
10+
* @param aggregate {BSTree} The aggregate to scan.
11+
* @constructor
12+
*/
13+
function BSTreeIterator(aggregate) {
14+
/**
15+
* The aggregate relates to this iterator.
16+
* @type {BSTree}
17+
*/
18+
this.aggregate = aggregate;
19+
20+
/**
21+
* The pointer to the position.
22+
* @type {BSNode|null}
23+
*/
24+
this.pointer = null;
25+
}
26+
27+
/**
28+
* @inheritDoc
29+
*/
30+
BSTreeIterator.prototype.first = function () {
31+
this.pointer = this.aggregate.minimum();
32+
};
33+
34+
/**
35+
* @inheritDoc
36+
*/
37+
BSTreeIterator.prototype.next = function () {
38+
this.pointer = this.aggregate.successor(this.pointer);
39+
};
40+
41+
/**
42+
* @inheritDoc
43+
*/
44+
BSTreeIterator.prototype.last = function () {
45+
this.pointer = this.aggregate.maximum();
46+
};
47+
48+
/**
49+
* @inheritDoc
50+
*/
51+
BSTreeIterator.prototype.previous = function () {
52+
this.pointer = this.aggregate.predecessor(this.pointer);
53+
};
54+
55+
/**
56+
* @inheritDoc
57+
*/
58+
BSTreeIterator.prototype.isDone = function () {
59+
return !this.pointer;
60+
};
61+
62+
/**
63+
* @inheritDoc
64+
*/
65+
BSTreeIterator.prototype.getItem = function () {
66+
return this.pointer.item;
67+
};
68+
69+
/**
70+
* Return the node stored at the position pointed by the iterator.
71+
* @abstract
72+
* @return {BSNode|null} The node stored or null if it's out of the bounds.
73+
*/
74+
BSTreeIterator.prototype.getNode = function () {
75+
return this.pointer;
76+
};

0 commit comments

Comments
 (0)