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 | + | - |
Throws: IsAlreadyExistsException
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
Throws: IsNotFoundException when node to delete was not found
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
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:
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
| type | EnumTreeTraversalType | + | - | 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