ts-jadsl
v0.0.3
Published
Just another data structures library
Downloads
2
Maintainers
Readme
(WIP)
Data Structures:
ListNode:
A node of a singly-linked list.
Constructor:
ListNode<T>(value: T)
Properties
value: T
Value held by node.next: ListNode<T> | null
Next pointer of node.
Parameters:
value: T
(mandatory): value held by list node.
LinkedList:
A simple, singly-linked list.
Constructor:
LinkedList<T>(initArray?: T[])
Parameters:
initArray: T[]
(optional): Array of elements with which linked list will be initialized, inserted in order
Methods:
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:
Constructor:
Stack<T>(initArray?: T[])
Parameters:
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)
Methods:
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.
Queue:
A simple, single-ended queue.
Constructor:
Queue<T>(initArray?: T[])
Parameters:
initArray: T[]
(optional): Array of element from which the queue will be initialized (insertion of each element performed in order)
Methods:
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.
Heap:
A binary heap with internally implemented with an array. Can also be considered implementation of a priority queue.
Constructor:
Heap<T>(comparatorFunction: (firstElement: T, secondElement: T) => number, initArray?: Array<T>)
Parameters:
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.
Methods:
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:
Constructor
BinaryTreeNode<T>(value: T)
Parameters:
value
(mandatory): Value for the binary tree node.
Properties:
value: T
value of nodeleft: BinaryTreeNode<T> | null
left of current noderight: BinaryTreeNode<T> | null
right of current node
Methods:
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
)
BinaryTree:
Abstract class - therefore, no constructor definition.
Properties:
root: BinaryTreeNode<T> | null
Root node of the binary tree.
Methods:
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.
BinarySearchTree:
Derived class: Derived from BinaryTree
.
Constructor
BinarySearchTree<T>(keyFunction: (value: T) => number, initArray?: Array<T>)
Parameters
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 performedinitArray: Array<T>
(optional): Array with which tree is initialized by inserting elements of array in order.
Properties:
root: BinaryTreeNode<T> | null
Root node of the binary tree
Methods:
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.
AvlTree:
Derived class: Derived from BinarySearchTree
.
Constructor
AvlTree<T>(keyFunction: (value: T) => number, initArray?: Array<T>)
Parameters
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 performedinitArray: Array<T>
(optional): Array with which tree is initialized by inserting elements of array in order.
Properties:
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)
Methods:
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.