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:
Name | Type | Required | Default | Description |
---|---|---|---|---|
fnCompare | FnCompareTwo | - | (a: T, b: T) => a > b | Callback function that will be called for comparing LEFT and RIGHT nodes |
insert(value: T): void
Add value to top of tree
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
value | T | + | - |
IsAlreadyExistsException
Throws: has(value: T): boolean
Check is tree has given value
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
value | T | + | - |
delete(value: T): void
Delete value from tree and restructure it
IsNotFoundException
when node to delete was not found
Throws: subtree(value: T): IBinaryTree<T>
Copy subtree from given root node
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
value | T | + | - | Root node of copy tree |
max(): T
Find maximum value in the tree
CollectionIsEmptyException
when tree is empty
Throws: min(): T
Find minimum value in the tree
CollectionIsEmptyException
when tree is empty
Throws: traverse(type: EnumTreeTraversalType): Array<T>
Convert a tree into array by using given traversing method
Params:
Name | Type | Required | Default | Description |
---|---|---|---|---|
type | EnumTreeTraversalType | + | - | preorder/inorder/postorder |
CollectionIsEmptyException
when tree is empty
Throws: length(): number
Get count of nodes in a tree
height(): number
Get max length from all branches in a tree