Skip to content

Commit 1513c53

Browse files
committedApr 3, 2018
Add binary search tree.
1 parent 8730240 commit 1513c53

File tree

6 files changed

+57
-13
lines changed

6 files changed

+57
-13
lines changed
 

‎README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ npm test
3131
```
3232
npm test -- -t 'LinkedList'
3333
```
34+
35+
## Useful Links
36+
37+
- [Data Structures and Algorithms on YouTube](https://www.youtube.com/playlist?list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
38+

‎src/data-structures/README.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# Data Structures
22

3-
**Useful Links**
4-
5-
- [YouTube Playlist](https://www.youtube.com/playlist?list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
6-
73
## Common Data Structure Operations
84

95
![Common Data Structure Operations](https://github.com/trekhleb/javascript-algorithms/blob/master/assets/big-o-data-structures.png)

‎src/data-structures/tree/BinaryTreeNode.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export default class BinaryTreeNode {
2-
constructor(value = null, left = null, right = null) {
3-
this.left = left;
4-
this.right = right;
2+
constructor(value = null) {
3+
this.left = null;
4+
this.right = null;
55
this.value = value;
66
}
77

‎src/data-structures/tree/__test__/BinaryTreeNode.test.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@ describe('BinaryTreeNode', () => {
2626
it('should traverse node', () => {
2727
const leftNode = new BinaryTreeNode(1);
2828
const rightNode = new BinaryTreeNode(3);
29-
const rootNode = new BinaryTreeNode(2, leftNode, rightNode);
29+
const rootNode = new BinaryTreeNode(2);
30+
31+
rootNode
32+
.addLeft(leftNode)
33+
.addRight(rightNode);
3034

3135
expect(rootNode.traverseInOrder()).toEqual([1, 2, 3]);
3236

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,32 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
2121
return this;
2222
}
2323

24-
contains(value) {
24+
find(value) {
2525
// Check the root.
2626
if (this.value === value) {
27-
return true;
27+
return this;
2828
}
2929

3030
if (value < this.value && this.left) {
3131
// Check left nodes.
32-
return this.left.contains(value);
32+
return this.left.find(value);
3333
} else if (value > this.value && this.right) {
3434
// Check right nodes.
35-
return this.right.contains(value);
35+
return this.right.find(value);
36+
}
37+
38+
return null;
39+
}
40+
41+
contains(value) {
42+
return !!this.find(value);
43+
}
44+
45+
findMin() {
46+
if (!this.left) {
47+
return this;
3648
}
3749

38-
return false;
50+
return this.left.findMin();
3951
}
4052
}

‎src/data-structures/tree/binary-search-tree/__test__/BinarySearchTreeNode.test.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,31 @@ describe('BinarySearchTreeNode', () => {
5656
expect(bstNode.contains(1)).toBeTruthy();
5757
expect(bstNode.contains(3)).toBeFalsy();
5858
});
59+
60+
it('should find min node', () => {
61+
const node = new BinarySearchTreeNode(10);
62+
63+
node.insert(20);
64+
node.insert(30);
65+
node.insert(5);
66+
node.insert(40);
67+
node.insert(1);
68+
69+
expect(node.findMin()).not.toBeNull();
70+
expect(node.findMin().value).toBe(1);
71+
});
72+
73+
it('should find node', () => {
74+
const node = new BinarySearchTreeNode(10);
75+
76+
node.insert(20);
77+
node.insert(30);
78+
node.insert(5);
79+
node.insert(40);
80+
node.insert(1);
81+
82+
expect(node.find(6)).toBeNull();
83+
expect(node.find(5)).not.toBeNull();
84+
expect(node.find(5).value).toBe(5);
85+
});
5986
});

0 commit comments

Comments
 (0)
Please sign in to comment.