-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathchallenge-prompt.js
More file actions
executable file
·115 lines (91 loc) · 2.75 KB
/
challenge-prompt.js
File metadata and controls
executable file
·115 lines (91 loc) · 2.75 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
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
// Checking the balance of a binary search tree
// BST Node Constructor
function Node(value = null){
this.value = value;
this.left = null;
this.right = null;
}
// BST Constructor
function BST(){
this.root = null;
this.insert = function(node) {
if (this.root === null) {
this.root = node;
return this;
}
var currNode = this.root;
while (true) {
if (node.value > currNode.value) {
if (currNode.right === null) {
currNode.right = node;
return this;
} else
currNode = currNode.right;
} else if (node.value < currNode.value) {
if (currNode.left === null) {
currNode.left = node;
return this;
} else
currNode = currNode.left;
} else
return this;
}
}
}
//---------------------------------------------------------
// Helper Functions
//
// -------------------- Your Code Here --------------------
// -------------------- End Code Area ---------------------
// This function checks whether the current binary search tree is balanced.
BST.prototype.checkBalance = function() {
// -------------------- Your Code Here --------------------
// -------------------- End Code Area ---------------------
}
// Tests
// All tests should be true
console.log("========== Test 1 ==========")
var test = new BST();
console.log(test.checkBalance());
console.log("========== Test 2 ==========")
test.insert(new Node(10))
console.log(test.checkBalance());
console.log("========== Test 3 ==========")
test.insert(new Node(5))
test.insert(new Node(2))
console.log(test.checkBalance() === false);
console.log("========== Test 4 ==========")
test.insert(new Node(15))
test.insert(new Node(7))
console.log(test.checkBalance());
console.log("========== Test 5 ==========")
test.insert(new Node(18))
test.insert(new Node(20))
console.log(test.checkBalance() === false);
console.log("========== Test 6 ==========")
test = new BST();
test.insert(new Node(20))
test.insert(new Node(5))
test.insert(new Node(3))
test.insert(new Node(4))
test.insert(new Node(1))
test.insert(new Node(2))
test.insert(new Node(12))
test.insert(new Node(9))
test.insert(new Node(17))
test.insert(new Node(7))
test.insert(new Node(10))
test.insert(new Node(13))
test.insert(new Node(18))
// These two nodes extend 2 past the shorted branch which terminates at `4`
test.insert(new Node(6))
test.insert(new Node(8))
//
test.insert(new Node(40))
test.insert(new Node(30))
test.insert(new Node(50))
test.insert(new Node(25))
test.insert(new Node(35))
test.insert(new Node(45))
test.insert(new Node(55))
console.log(test.checkBalance() === false)