mirror of
https://github.moeyy.xyz/https://github.com/trekhleb/javascript-algorithms.git
synced 2024-12-25 22:46:20 +08:00
Add binary search tree.
This commit is contained in:
parent
873024079a
commit
1513c536a6
@ -31,3 +31,8 @@ npm test
|
||||
```
|
||||
npm test -- -t 'LinkedList'
|
||||
```
|
||||
|
||||
## Useful Links
|
||||
|
||||
- [Data Structures and Algorithms on YouTube](https://www.youtube.com/playlist?list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
|
||||
|
||||
|
@ -1,9 +1,5 @@
|
||||
# Data Structures
|
||||
|
||||
**Useful Links**
|
||||
|
||||
- [YouTube Playlist](https://www.youtube.com/playlist?list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
|
||||
|
||||
## Common Data Structure Operations
|
||||
|
||||
![Common Data Structure Operations](https://github.com/trekhleb/javascript-algorithms/blob/master/assets/big-o-data-structures.png)
|
||||
|
@ -1,7 +1,7 @@
|
||||
export default class BinaryTreeNode {
|
||||
constructor(value = null, left = null, right = null) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
constructor(value = null) {
|
||||
this.left = null;
|
||||
this.right = null;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,11 @@ describe('BinaryTreeNode', () => {
|
||||
it('should traverse node', () => {
|
||||
const leftNode = new BinaryTreeNode(1);
|
||||
const rightNode = new BinaryTreeNode(3);
|
||||
const rootNode = new BinaryTreeNode(2, leftNode, rightNode);
|
||||
const rootNode = new BinaryTreeNode(2);
|
||||
|
||||
rootNode
|
||||
.addLeft(leftNode)
|
||||
.addRight(rightNode);
|
||||
|
||||
expect(rootNode.traverseInOrder()).toEqual([1, 2, 3]);
|
||||
|
||||
|
@ -21,20 +21,32 @@ export default class BinarySearchTreeNode extends BinaryTreeNode {
|
||||
return this;
|
||||
}
|
||||
|
||||
contains(value) {
|
||||
find(value) {
|
||||
// Check the root.
|
||||
if (this.value === value) {
|
||||
return true;
|
||||
return this;
|
||||
}
|
||||
|
||||
if (value < this.value && this.left) {
|
||||
// Check left nodes.
|
||||
return this.left.contains(value);
|
||||
return this.left.find(value);
|
||||
} else if (value > this.value && this.right) {
|
||||
// Check right nodes.
|
||||
return this.right.contains(value);
|
||||
return this.right.find(value);
|
||||
}
|
||||
|
||||
return false;
|
||||
return null;
|
||||
}
|
||||
|
||||
contains(value) {
|
||||
return !!this.find(value);
|
||||
}
|
||||
|
||||
findMin() {
|
||||
if (!this.left) {
|
||||
return this;
|
||||
}
|
||||
|
||||
return this.left.findMin();
|
||||
}
|
||||
}
|
||||
|
@ -56,4 +56,31 @@ describe('BinarySearchTreeNode', () => {
|
||||
expect(bstNode.contains(1)).toBeTruthy();
|
||||
expect(bstNode.contains(3)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should find min node', () => {
|
||||
const node = new BinarySearchTreeNode(10);
|
||||
|
||||
node.insert(20);
|
||||
node.insert(30);
|
||||
node.insert(5);
|
||||
node.insert(40);
|
||||
node.insert(1);
|
||||
|
||||
expect(node.findMin()).not.toBeNull();
|
||||
expect(node.findMin().value).toBe(1);
|
||||
});
|
||||
|
||||
it('should find node', () => {
|
||||
const node = new BinarySearchTreeNode(10);
|
||||
|
||||
node.insert(20);
|
||||
node.insert(30);
|
||||
node.insert(5);
|
||||
node.insert(40);
|
||||
node.insert(1);
|
||||
|
||||
expect(node.find(6)).toBeNull();
|
||||
expect(node.find(5)).not.toBeNull();
|
||||
expect(node.find(5).value).toBe(5);
|
||||
});
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user