Skip to content

BinarySearchTree<T> ​

Guide: /guide/data-structures/binary-tree

Generic Types ​

T - type of collection elements

Implements interfaces ​

IBinaryTree ​

Methods ​

constructor(fnCompare?: number): Stack<T> ​

Creates empty instance

Params: ​
NameTypeRequiredDefaultDescription
fnCompareFnCompareTwo-(a: T, b: T) => a > bCallback function that will be called for comparing LEFT and RIGHT nodes



insert(value: T): void ​

Add value to top of tree

Params: ​
NameTypeRequiredDefaultDescription
valueT+-
Throws: IsAlreadyExistsException ​



has(value: T): boolean ​

Check is tree has given value

Params: ​
NameTypeRequiredDefaultDescription
valueT+-



delete(value: T): void ​

Delete value from tree and restructure it

Throws: IsNotFoundException when node to delete was not found ​



subtree(value: T): IBinaryTree<T> ​

Copy subtree from given root node

Params: ​
NameTypeRequiredDefaultDescription
valueT+-Root node of copy tree



max(): T ​

Find maximum value in the tree

Throws: CollectionIsEmptyException when tree is empty ​



min(): T ​

Find minimum value in the tree

Throws: CollectionIsEmptyException when tree is empty ​



traverse(type: EnumTreeTraversalType): Array<T> ​

Convert a tree into array by using given traversing method

Params: ​
NameTypeRequiredDefaultDescription
typeEnumTreeTraversalType+-preorder/inorder/postorder
Throws: CollectionIsEmptyException when tree is empty ​



length(): number ​

Get count of nodes in a tree

height(): number ​

Get max length from all branches in a tree

MIT Licensed