A node of a singly-linked list.
ListNode<T>(value: T)
value: T
Value held by node.next: ListNode<T> | null
Next pointer of node.
value: T
(mandatory): value held by list node.
A simple, singly-linked list.
LinkedList<T>(initArray?: T[])
initArray: T[]
(optional): Array of elements with which linked list will be initialized, inserted in order
-
length(): number
Returns length of list. -
insertAt(index: number, value: T): LinkedList<T>
Insertsvalue
atindex
in the list, returns the list. -
insertAtHead(value: T): LinkedList<T>
Inserts value at head, returns the list. -
insertAtTail(value: T): LinkedList<T>
Inserts value at tail, returns the list. -
get(index: number): T | null
Returns element atindex
. Returnsnull
if index is out of bounds for the list. -
deleteAt(index: number): T | null
Deletes element atindex
, returns the deleted element. Returnsnull
if index is out of bounds and delete is not performed. -
toArray(): T[]
Returns an array of elements of the list, in order. -
getHeadNode(): ListNode<T> | null
Returns the head node of the list, returnsnull
if list is empty. NOTE: Should be used ONLY if the user does not wish to use the list functions, and just wants a reference to the head node of the list.
Stack<T>(initArray?: T[])
initArray: T[]
(optional): Array of elements with which the stack will be initialized (insertion of each element performed in reverse order, i.e, first element of array will be top of stack)
-
size(): number
Returns size of the stack (number of elements). -
peek(): T | null
Returns top element of stack. Returnsnull
if stack empty. -
isEmpty(): boolean
Returnstrue
if stack empty,false
if otherwise. -
push(value: T): Stack<T>
Pushesvalue
on top of stack, returns theStack
instance. -
pop(): T | null
Pops and returns top value from stack. Returnsnull
if stack empty.
A simple, single-ended queue.
Queue<T>(initArray?: T[])
initArray: T[]
(optional): Array of element from which the queue will be initialized (insertion of each element performed in order)
-
enqueue(value: T): Queue<T>
Addsvalue
to rear of queue, returns theQueue
instance. -
dequeue(): T | null
Removes and returns element at front of queue. Returnsnull
if queue empty. -
getFront(): T | null
Returns value in front of the queue. Returnsnull
if queue empty. -
isEmpty(): boolean
Returnstrue
if queue empty,false
if otherwise. -
length(): number
Returns length of queue.
A binary heap with internally implemented with an array. Can also be considered implementation of a priority queue.
Heap<T>(comparatorFunction: (firstElement: T, secondElement: T) => number, initArray?: Array<T>)
comparatorFunction: (firstElement: T, secondElement: T) => number
(mandatory): A comparator function to return a number. If number is greater than 0, bubble-up will be performed in the heapify operation.initArray: Array<T>
(optional): Array which will be heapified into initial heap.
-
insert(value: T): Heap<T>
Insertsvalue
in the heap. -
extract(): T | null
Removes and returns value from root of heap (highest priority). Returnsnull
if heap empty. -
peek(): T | null
Returns value at root of heap. -
size(): number
Returns size of heap (number of elements)
BinaryTreeNode<T>(value: T)
value
(mandatory): Value for the binary tree node.
value: T
value of nodeleft: BinaryTreeNode<T> | null
left of current noderight: BinaryTreeNode<T> | null
right of current node
-
isLeafNode(): boolean
Returnstrue
is node is leaf node,false
if otherwise. -
height(): number
Returns height of the subtree with current node as root. -
getInorderTraversal(): Array<T>
Returns array of elements in subtree rooted at node in an inorder fasion. -
getPreorderTraversal(): Array<T>
Returns array of elements in subtree rooted at node in a preorder fasion. -
getPostorderTraversal(): Array<T>
Returns array of elements in subtree rooted at node in a postorder fasion. -
invert(): BinaryTreeNode<T> | null
Inverts subtree with node as root, returns the node. -
isBalanced(): boolean
Returnstrue
if subtree rooted at node is height balanced,false
if otherwise. -
isBinarySearchTree(keyFunction: (value: T) => number)
Returnstrue
if subtree rooted at node is a valid binary search tree, needs akeyFunction
to get numeric value from data stored in node (seeBinarySearchTree
)
Abstract class - therefore, no constructor definition.
root: BinaryTreeNode<T> | null
Root node of the binary tree.
-
getInorderTraversal(): Array<T>
Returns array of elements in tree in an inorder fasion from root of tree. -
getPreorderTraversal(): Array<T>
Returns array of elements in tree in a preorder fasion from root of tree. -
getPostorderTraversal(): Array<T>
Returns array of elements in tree in a postorder fasion from root of tree. -
height(): number
Returns height of the tree. -
invert(): BinaryTree<T>
Inverts the BinaryTree, returns the tree. -
isBalanced(): boolean
Returnstrue
if binary tree is height balanced,false
if otherwise.
Derived class: Derived from BinaryTree
.
BinarySearchTree<T>(keyFunction: (value: T) => number, initArray?: Array<T>)
-
keyFunction: (value: T) => number
(mandatory): Function that takesvalue
of tree node as a parameter to get the key value, on the basis of which BST inserts/search will be performed -
initArray: Array<T>
(optional): Array with which tree is initialized by inserting elements of array in order.
root: BinaryTreeNode<T> | null
Root node of the binary tree
-
insert(value: T): BinarySearchTree<T>
Inserts new node withvalue
in BST, returns BST instance. -
delete(value: T): BinarySearchTree<T>
Deletes node withvalue
in BST, returns BST instance. -
search(value: T): BinaryTreeNode<T> | null
Returns tree node withvalue
in BST,null
if node not found. -
getMin(): T | null
Returns value in tree with min key,null
if tree empty. -
getMax(): T | null
Returns value in tree with max key,null
if tree empty.
Derived class: Derived from BinarySearchTree
.
AvlTree<T>(keyFunction: (value: T) => number, initArray?: Array<T>)
-
keyFunction: (value: T) => number
(mandatory): Function that takesvalue
of tree node as a parameter to get the key value, on the basis of which BST inserts/search will be performed -
initArray: Array<T>
(optional): Array with which tree is initialized by inserting elements of array in order.
root: BinaryTreeNode<T> | null
Root node of the binary tree - (NOTE: Access this property ONLY if you do not wish to use the methods of the class, and want to write your own functions)
-
insert(value: T): AvlTree<T>
Inserts new node withvalue
in BST, returns AvlTree instance. -
delete(value: T): AvlTree<T>
Deletes node withvalue
in BST, returns AvlTree instance. -
search(value: T): BinaryTreeNode<T> | null
Returns tree node withvalue
in BST,null
if node not found. -
getMin(): T | null
Returns value in tree with min key,null
if tree empty. -
getMax(): T | null
Returns value in tree with max key,null
if tree empty.