Skip to content

Commit 27ac958

Browse files
committed
Adding Tree Sort using unbalenced BST.
1 parent 5614404 commit 27ac958

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

‎SortingAlgorithms/treeSort.js‎

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
2+
class Node {
3+
constructor(value) {
4+
this.value = value;
5+
this.left = null;
6+
this.right = null;
7+
}
8+
}
9+
10+
class BST {
11+
constructor() {
12+
this.root = null;
13+
}
14+
insert(value) {
15+
var inNode = new Node(value);
16+
17+
if(this.root === null)
18+
this.root = inNode;
19+
else
20+
this._insertNode(this.root, inNode);
21+
}
22+
_insertNode(node, inNode) {
23+
if (inNode.value < node.value) {
24+
if (node.left === null)
25+
node.left = inNode;
26+
else
27+
this._insertNode(node.left, inNode);
28+
} else {
29+
if(node.right === null)
30+
node.right = inNode;
31+
else
32+
this._insertNode(node.right, inNode);
33+
}
34+
}
35+
getOrdered() {
36+
return this._order(this.root);
37+
}
38+
_order(node) {
39+
var a = [];
40+
if(node !== null)
41+
{
42+
a = a.concat(this._order(node.left));
43+
a.push(node.value);
44+
a = a.concat(this._order(node.right));
45+
}
46+
return a;
47+
}
48+
49+
}
50+
51+
/*
52+
var bstTest = new BST();
53+
bstTest.insert(5);
54+
bstTest.insert(20);
55+
bstTest.insert(1);
56+
bstTest.insert(15);
57+
bstTest.insert(7);
58+
bstTest.insert(9);
59+
bstTest.insert(12);
60+
bstTest.insert(40);
61+
bstTest.insert(3);
62+
var ordered = bstTest.getOrdered();
63+
console.log(ordered);
64+
*/

0 commit comments

Comments
 (0)