Skip to content

Commit d6be338

Browse files
committedApr 2, 2018
Add binary search tree.
1 parent 00e40a0 commit d6be338

File tree

8 files changed

+184
-1
lines changed

8 files changed

+184
-1
lines changed
 

‎.eslintrc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"jest/globals": true
77
},
88
"rules": {
9-
"no-bitwise": "off"
9+
"no-bitwise": "off",
10+
"no-lonely-if": "off"
1011
}
1112
}

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
4. [Hash Table](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/hash-table)
1111
5. [Heap](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/heap)
1212
5. [Trie](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/trie)
13+
6. [Binary Search Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/binary-search-tree)
1314

1415
## [Algorithms](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms)
1516

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
export default class BinaryTreeNode {
2+
constructor(value = null, left = null, right = null) {
3+
this.left = left;
4+
this.right = right;
5+
this.value = value;
6+
}
7+
8+
addLeft(node) {
9+
this.left = node;
10+
return this;
11+
}
12+
13+
addRight(node) {
14+
this.right = node;
15+
return this;
16+
}
17+
18+
hasLeft() {
19+
return !!this.left;
20+
}
21+
22+
hasRight() {
23+
return !!this.right;
24+
}
25+
26+
traverseInOrder() {
27+
return Array.prototype.concat(
28+
this.left ? this.left.traverseInOrder() : [null],
29+
[this.value],
30+
this.right ? this.right.traverseInOrder() : [null],
31+
);
32+
}
33+
34+
toString() {
35+
return this.traverseInOrder().filter(value => !!value).toString();
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import BinaryTreeNode from '../BinaryTreeNode';
2+
3+
describe('BinaryTreeNode', () => {
4+
it('should create node', () => {
5+
const node = new BinaryTreeNode();
6+
7+
expect(node).toBeDefined();
8+
9+
expect(node.value).toBeNull();
10+
expect(node.left).toBeNull();
11+
expect(node.right).toBeNull();
12+
13+
expect(node.hasLeft()).toBeFalsy();
14+
expect(node.hasRight()).toBeFalsy();
15+
16+
const leftNode = new BinaryTreeNode(1);
17+
const rightNode = new BinaryTreeNode(3);
18+
const rootNode = new BinaryTreeNode(2);
19+
20+
rootNode
21+
.addLeft(leftNode)
22+
.addRight(rightNode);
23+
24+
expect(rootNode.value).toBe(2);
25+
expect(rootNode.left.value).toBe(1);
26+
expect(rootNode.right.value).toBe(3);
27+
});
28+
29+
it('should traverse node', () => {
30+
const leftNode = new BinaryTreeNode(1);
31+
const rightNode = new BinaryTreeNode(3);
32+
const rootNode = new BinaryTreeNode(2, leftNode, rightNode);
33+
34+
expect(rootNode.traverseInOrder()).toEqual([null, 1, null, 2, null, 3, null]);
35+
36+
expect(rootNode.toString()).toBe('1,2,3');
37+
});
38+
});
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import BinarySearchTreeNode from './BinarySearchTreeNode';
2+
3+
export default class BinarySearchTree {
4+
constructor() {
5+
this.root = new BinarySearchTreeNode();
6+
}
7+
8+
insert(value) {
9+
this.root.insert(value);
10+
}
11+
12+
contains(value) {
13+
return this.root.contains(value);
14+
}
15+
16+
toString() {
17+
this.root.toString();
18+
}
19+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import BinaryTreeNode from '../BinaryTreeNode';
2+
3+
export default class BinarySearchTreeNode extends BinaryTreeNode {
4+
insert(value) {
5+
if (value < this.value) {
6+
// Insert to the left.
7+
if (this.left) {
8+
this.left.insert(value);
9+
} else {
10+
this.left = new BinarySearchTreeNode(value);
11+
}
12+
} else {
13+
// Insert to the right.
14+
if (this.right) {
15+
this.right.insert(value);
16+
} else {
17+
this.right = new BinarySearchTreeNode(value);
18+
}
19+
}
20+
21+
return this;
22+
}
23+
24+
contains(value) {
25+
// Check the root.
26+
if (this.value === value) {
27+
return true;
28+
}
29+
30+
if (value < this.value && this.left) {
31+
return this.left.contains(value);
32+
} else if (this.right) {
33+
return this.right.contains(value);
34+
}
35+
36+
return false;
37+
}
38+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
describe('BinarySearchTree', () => {
2+
it('should create binary search tree', () => {
3+
4+
});
5+
});
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import BinarySearchTreeNode from '../BinarySearchTreeNode';
2+
3+
describe('BinarySearchTreeNode', () => {
4+
it('should create binary search tree', () => {
5+
const bstNode = new BinarySearchTreeNode(2);
6+
7+
expect(bstNode.value).toBe(2);
8+
expect(bstNode.left).toBeNull();
9+
expect(bstNode.right).toBeNull();
10+
});
11+
12+
it('should insert nodes in correct order', () => {
13+
const bstNode = new BinarySearchTreeNode(2);
14+
bstNode.insert(1);
15+
16+
expect(bstNode.toString()).toBe('1,2');
17+
expect(bstNode.contains(1)).toBeTruthy();
18+
expect(bstNode.contains(3)).toBeFalsy();
19+
20+
bstNode.insert(3);
21+
22+
expect(bstNode.toString()).toBe('1,2,3');
23+
expect(bstNode.contains(3)).toBeTruthy();
24+
expect(bstNode.contains(4)).toBeFalsy();
25+
26+
bstNode.insert(7);
27+
28+
expect(bstNode.toString()).toBe('1,2,3,7');
29+
expect(bstNode.contains(7)).toBeTruthy();
30+
expect(bstNode.contains(8)).toBeFalsy();
31+
32+
bstNode.insert(4);
33+
34+
expect(bstNode.toString()).toBe('1,2,3,4,7');
35+
expect(bstNode.contains(4)).toBeTruthy();
36+
expect(bstNode.contains(8)).toBeFalsy();
37+
38+
bstNode.insert(6);
39+
40+
expect(bstNode.toString()).toBe('1,2,3,4,6,7');
41+
expect(bstNode.contains(6)).toBeTruthy();
42+
expect(bstNode.contains(8)).toBeFalsy();
43+
});
44+
});

0 commit comments

Comments
 (0)