-
-
Notifications
You must be signed in to change notification settings - Fork 450
feat(data-struct): binary search tree #106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
/** | ||
* Represents a node of a binary search tree. | ||
* | ||
* @template T The type of the value stored in the node. | ||
*/ | ||
class TreeNode<T> { | ||
constructor( | ||
public data: T, | ||
public leftChild?: TreeNode<T>, | ||
public rightChild?: TreeNode<T>, | ||
) {} | ||
} | ||
|
||
/** | ||
* An implementation of a binary search tree. | ||
* | ||
* A binary tree is a tree with only two children per node. A binary search tree on top sorts the children according | ||
* to following rules: | ||
* - left child < parent node | ||
* - right child > parent node | ||
* - all children on the left side < root node | ||
* - all children on the right side > root node | ||
* | ||
* For profound information about trees | ||
* @see https://www.geeksforgeeks.org/introduction-to-tree-data-structure-and-algorithm-tutorials/ | ||
* | ||
* @template T The data type of the values in the binary tree. | ||
*/ | ||
export class BinarySearchTree<T> { | ||
rootNode?: TreeNode<T>; | ||
|
||
/** | ||
* Instantiates the binary search tree. | ||
* | ||
* @param rootNode The root node. | ||
*/ | ||
constructor() { | ||
this.rootNode = undefined; | ||
} | ||
|
||
/** | ||
* Checks, if the binary search tree is empty, i. e. has no root node. | ||
* | ||
* @returns Whether the binary search tree is empty. | ||
*/ | ||
isEmpty(): boolean { | ||
return this.rootNode === undefined; | ||
} | ||
|
||
/** | ||
* Checks whether the tree has the given data or not. | ||
* | ||
* @param data The data to check for. | ||
*/ | ||
has(data: T): boolean { | ||
if (!this.rootNode) { | ||
return false; | ||
} | ||
|
||
let currentNode = this.rootNode; | ||
while (currentNode.data !== data) { | ||
if (data > currentNode.data) { | ||
if (!currentNode.rightChild) { | ||
return false; | ||
} | ||
|
||
currentNode = currentNode.rightChild; | ||
} else { | ||
if (!currentNode.leftChild) { | ||
return false; | ||
} | ||
|
||
currentNode = currentNode.leftChild; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Inserts the given data into the binary search tree. | ||
* | ||
* @param data The data to be stored in the binary search tree. | ||
* @returns | ||
*/ | ||
insert(data: T): void { | ||
if (!this.rootNode) { | ||
this.rootNode = new TreeNode<T>(data); | ||
return; | ||
} | ||
|
||
let currentNode: TreeNode<T> = this.rootNode; | ||
while (true) { | ||
if (data > currentNode.data) { | ||
if (currentNode.rightChild) { | ||
currentNode = currentNode.rightChild; | ||
} else { | ||
currentNode.rightChild = new TreeNode<T>(data); | ||
return; | ||
} | ||
} else { | ||
if (currentNode.leftChild) { | ||
currentNode = currentNode.leftChild; | ||
} else { | ||
currentNode.leftChild = new TreeNode<T>(data); | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Finds the minimum value of the binary search tree. | ||
* | ||
* @returns The minimum value of the binary search tree | ||
*/ | ||
findMin(): T { | ||
if (!this.rootNode) { | ||
throw new Error('Empty tree.'); | ||
} | ||
|
||
const traverse = (node: TreeNode<T>): T => { | ||
return !node.leftChild ? node.data : traverse(node.leftChild); | ||
}; | ||
|
||
return traverse(this.rootNode); | ||
} | ||
|
||
/** | ||
* Finds the maximum value of the binary search tree. | ||
* | ||
* @returns The maximum value of the binary search tree | ||
*/ | ||
findMax(): T { | ||
if (!this.rootNode) { | ||
throw new Error('Empty tree.'); | ||
} | ||
|
||
const traverse = (node: TreeNode<T>): T => { | ||
return !node.rightChild ? node.data : traverse(node.rightChild); | ||
}; | ||
|
||
return traverse(this.rootNode); | ||
} | ||
|
||
/** | ||
* Traverses to the binary search tree in in-order, i. e. it follow the schema of: | ||
* Left Node -> Root Node -> Right Node | ||
* | ||
* @param array The already found node data for recursive access. | ||
* @returns | ||
*/ | ||
inOrderTraversal(array: T[] = []): T[] { | ||
if (!this.rootNode) { | ||
return array; | ||
} | ||
|
||
const traverse = (node?: TreeNode<T>, array: T[] = []): T[] => { | ||
if (!node) { | ||
return array; | ||
} | ||
|
||
traverse(node.leftChild, array); | ||
array.push(node.data); | ||
traverse(node.rightChild, array); | ||
return array; | ||
}; | ||
|
||
return traverse(this.rootNode); | ||
} | ||
|
||
/** | ||
* Traverses to the binary search tree in pre-order, i. e. it follow the schema of: | ||
* Root Node -> Left Node -> Right Node | ||
* | ||
* @param array The already found node data for recursive access. | ||
* @returns | ||
*/ | ||
preOrderTraversal(array: T[] = []): T[] { | ||
if (!this.rootNode) { | ||
return array; | ||
} | ||
|
||
const traverse = (node?: TreeNode<T>, array: T[] = []): T[] => { | ||
if (!node) { | ||
return array; | ||
} | ||
|
||
array.push(node.data); | ||
traverse(node.leftChild, array); | ||
traverse(node.rightChild, array); | ||
|
||
return array; | ||
}; | ||
|
||
return traverse(this.rootNode); | ||
} | ||
|
||
/** | ||
* Traverses to the binary search tree in post-order, i. e. it follow the schema of: | ||
* Left Node -> Right Node -> Root Node | ||
* | ||
* @param array The already found node data for recursive access. | ||
* @returns | ||
*/ | ||
postOrderTraversal(array: T[] = []): T[] { | ||
if (!this.rootNode) { | ||
return array; | ||
} | ||
|
||
const traverse = (node?: TreeNode<T>, array: T[] = []): T[] => { | ||
if (!node) { | ||
return array; | ||
} | ||
|
||
traverse(node.leftChild, array); | ||
traverse(node.rightChild, array); | ||
array.push(node.data); | ||
|
||
return array; | ||
}; | ||
|
||
return traverse(this.rootNode); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { BinarySearchTree } from '../binary_search_tree'; | ||
|
||
describe('BinarySearchTree', () => { | ||
describe('with filled binary search tree (insert)', () => { | ||
let binarySearchTree: BinarySearchTree<number>; | ||
|
||
beforeEach(() => { | ||
binarySearchTree = new BinarySearchTree<number>(); | ||
binarySearchTree.insert(25); | ||
binarySearchTree.insert(80); | ||
binarySearchTree.insert(12); | ||
binarySearchTree.insert(5); | ||
binarySearchTree.insert(64); | ||
}); | ||
|
||
it('should return false for isEmpty when binary search tree is not empty', () => { | ||
expect(binarySearchTree.isEmpty()).toBeFalsy(); | ||
}); | ||
|
||
it('should return correct root node for search', () => { | ||
expect(binarySearchTree.rootNode?.data).toBe(25); | ||
}); | ||
|
||
it('should return whether an element is in the set', () => { | ||
expect(binarySearchTree.has(5)).toBe(true); | ||
expect(binarySearchTree.has(42)).toBe(false); | ||
}); | ||
|
||
it('should traverse in in-order through the tree', () => { | ||
expect(binarySearchTree.inOrderTraversal()).toStrictEqual([5, 12, 25, 64, 80]); | ||
}); | ||
|
||
it('should traverse in pre-order through the tree', () => { | ||
console.log(binarySearchTree.preOrderTraversal()); | ||
|
||
expect( | ||
binarySearchTree.preOrderTraversal(), | ||
).toStrictEqual([25, 12, 5, 80, 64]); | ||
}); | ||
|
||
it('should traverse in post-order through the tree', () => { | ||
expect( | ||
binarySearchTree.postOrderTraversal(), | ||
).toStrictEqual([5, 12, 64, 80, 25]); | ||
}); | ||
|
||
it('should return the minimum value of the binary search tree', () => { | ||
expect(binarySearchTree.findMin()).toBe(5); | ||
}); | ||
|
||
it('should return the maximum value of the binary search tree', () => { | ||
expect(binarySearchTree.findMax()).toBe(80); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.