Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3ddfd2c

Browse files
add binary search tree - help needed
1 parent cd56400 commit 3ddfd2c

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

‎data-structures/binary-search-tree.js

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
// https://en.wikipedia.org/wiki/Binary_search_tree
2+
3+
function Node(data) {
4+
this.key = data
5+
this.left = null
6+
this.right = null
7+
}
8+
9+
function BST() {
10+
this.root = null
11+
}
12+
13+
BST.prototype.insert = function (value) {
14+
let node = new Node(value),
15+
current = null,
16+
x = this.root
17+
18+
while (x !== null) {
19+
current = x
20+
if (node.key < x.key) x = x.left
21+
else x = x.right
22+
}
23+
24+
if (current === null) this.root = node
25+
else if (node.key < current.key) current.left = node
26+
else current.right = node
27+
}
28+
29+
BST.prototype.delete = function (value) {
30+
// Need help with this one, as most other languages implementations
31+
// depend on the parent being stored in the node
32+
// Is there another way of traversing the three in O(lg n) without
33+
// that?
34+
let current = this.root,
35+
parent = null,
36+
found = false
37+
38+
while (!found && current) {
39+
parent = current
40+
if (value < current.key) current = current.left
41+
else if (value > current.key) current = current.right
42+
else found = true
43+
}
44+
45+
if (!found) return
46+
47+
// delete a node with no children
48+
// if (!current.left || !current.right) {
49+
// if (current === parent.left) {
50+
// parent.left = current.left || current.right
51+
// if (!parent.left) {
52+
// parent.left.parent = parent
53+
// }
54+
// }
55+
// }
56+
}
57+
58+
BST.prototype.search = function (value) {
59+
let current = this.root
60+
while (current) {
61+
if (value === current.key) return current
62+
else if (value < current.key) current = current.left
63+
else current = current.right
64+
}
65+
return null
66+
l}
67+
68+
BST.prototype.min = function () {
69+
let current = this.root.left
70+
while (current.left) current = current.left
71+
return current
72+
}
73+
74+
BST.prototype.max = function () {
75+
let current = this.root.right
76+
while (current.right) current = current.right
77+
return current
78+
}
79+
80+
BST.prototype.length = function () {
81+
let count = 0
82+
this.inorderTreeWalk(this.root, function() {
83+
count++
84+
})
85+
return count
86+
}
87+
88+
BST.prototype.toArray = function () {
89+
let result = []
90+
this.inorderTreeWalk(this.root, function(key) {
91+
result.push(key)
92+
})
93+
return result
94+
}
95+
96+
BST.prototype.inorderTreeWalk = function (node, callback) {
97+
if (node) {
98+
this.inorderTreeWalk(node.left, callback)
99+
callback(node.key)
100+
this.inorderTreeWalk(node.right, callback)
101+
}
102+
}
103+
104+
const test = require('tape')
105+
106+
test('BST', assert => {
107+
let bst = new BST()
108+
bst.insert(12)
109+
bst.insert(4)
110+
bst.insert(8)
111+
bst.insert(15)
112+
assert.deepEqual(bst.search(15).key, 15)
113+
assert.deepEqual(bst.min().key, 4)
114+
assert.deepEqual(bst.max().key, 15)
115+
assert.deepEqual(bst.toArray(), [4,8,12,15])
116+
assert.deepEqual(bst.length(), 4)
117+
assert.end()
118+
})

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /