-
Notifications
You must be signed in to change notification settings - Fork 0
/
bintree.js
77 lines (71 loc) · 1.78 KB
/
bintree.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
function BinaryTreeNode(value) {
this.value = value;
this.left = null;
this.right = null;
}
BinaryTreeNode.prototype.insert = function (value) {
if (value < this.value) {
if (this.left !== null) {
return this.left.insertLeft(value);
} else {
this.left = new BinaryTreeNode(value);
return this.left;
}
} else {
if (this.right !== null) {
return this.right.insertRight(value);
} else {
this.right = new BinaryTreeNode(value);
return this.right;
}
}
};
BinaryTreeNode.prototype.insertLeft = function (value) {
if (this.left !== null) {
return this.left.insertLeft(value);
} else {
this.left = new BinaryTreeNode(value);
return this.left;
}
};
BinaryTreeNode.prototype.insertRight = function (value) {
if (this.right !== null) {
return this.right.insertRight(value);
} else {
this.right = new BinaryTreeNode(value);
return this.right;
}
};
BinaryTreeNode.prototype.find2ndBig = function (mynode) {
if (mynode) {
if (mynode.right.right !== null) {
return this.find2ndBig(mynode.right);
} else {
return mynode.value;
}
} else {
if (this !== null) {
return this.find2ndBig(this);
} else {
return null;
}
}
};
function myFunction(arg) {
// write the body of your function here
tree = new BinaryTreeNode(5);
tree.insert(3);
tree.insert(6);
tree.insert(8);
tree.insert(10);
console.log(tree.value);
console.log(tree.right.value);
console.log(tree.right.right.value);
console.log(tree.right.right.right.value);
return (
"tree.value = " + tree.value + "; tree.find2ndBig() = " + tree.find2ndBig()
);
}
// run your function through some test cases here
// remember: debugging is half the battle!
console.log(myFunction("test input"));