forked from vJechsmayr/JavaScriptAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtreeSort.js
More file actions
64 lines (59 loc) · 1.09 KB
/
treeSort.js
File metadata and controls
64 lines (59 loc) · 1.09 KB
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
class Node {
constructor(value) {
this.value = value;
this.left = null;
this.right = null;
}
}
class BST {
constructor() {
this.root = null;
}
insert(value) {
var inNode = new Node(value);
if(this.root === null)
this.root = inNode;
else
this._insertNode(this.root, inNode);
}
_insertNode(node, inNode) {
if (inNode.value < node.value) {
if (node.left === null)
node.left = inNode;
else
this._insertNode(node.left, inNode);
} else {
if(node.right === null)
node.right = inNode;
else
this._insertNode(node.right, inNode);
}
}
getOrdered() {
return this._order(this.root);
}
_order(node) {
var a = [];
if(node !== null)
{
a = a.concat(this._order(node.left));
a.push(node.value);
a = a.concat(this._order(node.right));
}
return a;
}
}
/*
var bstTest = new BST();
bstTest.insert(5);
bstTest.insert(20);
bstTest.insert(1);
bstTest.insert(15);
bstTest.insert(7);
bstTest.insert(9);
bstTest.insert(12);
bstTest.insert(40);
bstTest.insert(3);
var ordered = bstTest.getOrdered();
console.log(ordered);
*/