-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinarySearchTree.js
More file actions
91 lines (75 loc) · 1.88 KB
/
BinarySearchTree.js
File metadata and controls
91 lines (75 loc) · 1.88 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
BinarySearchTree = function() {
// Methods //
this.insert = function(key, node) {
if (node == null)
node = this.root;
// landed on one of the null nodes, so promote the null node
// to a full-blown bst node and give it null node children
if (node.key == null) {
node.key = key;
node.left = this.createNullNode();
node.right = this.createNullNode();
}
else if (key < node.key) {
this.insert(key, node.left);
}
else if (key > node.key) {
this.insert(key, node.right);
}
};
this.findMin = function (node) {
if (node.left == null || this.getKey(node.left) == null) {
return node.key;
}
else {
return this.findMin(node.left);
}
};
this.findMax = function (node) {
if (node.right == null || this.getKey(node.right) == null) {
return node.key;
}
else {
return this.findMax(node.right);
}
};
this.remove = function(key,node) {
if (node == null)
node = this.root;
if (node.key == null)
return node;
if (key < node.key)
node.left = this.remove(key, node.left);
else if (key > node.key)
node.right = this.remove(key, node.right);
else if (node.left.key != null && node.right.key != null) {
node.key = this.findMin(node.right);
node.right = this.remove(node.key, node.right);
}
else
node = ( node.left.key != null) ? node.left : node.right;
return node;
};
this.getKey = function(node) {
return node.key;
};
// all nodes are born as null nodes
this.createNullNode = function() {
var node = {
"key" : null,
"left" : null,
"right" : null,
"id" : this.nodeCounter
};
this.nodeCounter++;
return node;
};
this.getNodes = function () {
return this.root;
};
// Member Variables //
// create an empty tree with one null node
this.root = this.createNullNode();
// identifier for nodes -- increment each time a new node is created
this.nodeCounter = 0;
};