This module provides a simple implementation of a Binary Tree in TypeScript. It includes a Node class to represent each node in the tree and a BinaryTree class to manage the tree.
A Binary Tree is a hierarchical data structure in which each node has at most two children, referred to as the left child and the right child. This implementation supports basic operations such as inserting values, finding the minimum value, and clearing the tree.
const tree: BinaryTree<Number> = new BinaryTree<Number>()
tree.insert(10)
tree.insert(5)
tree.insert(15)
tree.remove(5)
const minNode = tree.root?.findMin()
tree.clear()
constructor(value: Number)
: Creates a new node with the specified value.
root: Node<Number>
: The root node of the Binary Tree
clear(): void
: Clear the entire treeinsert(value: Number): void
: Insert a value into the treefind(value: Number): Node<Number> | null
: Find a value in the treeremove(value: Number): void
: Remove a value from the tree
const tree: BinaryTree<Number> = new BinaryTree<Number>()
tree.insert(10)
tree.insert(5)
tree.insert(15)
console.log(`The lowest value in the tree is: ${tree.root?.findMin()?.value}`)
- This implementation is for educational purposes and may not be suitable for production use.