ts-jadsl v0.0.3
(WIP)
Data Structures:
ListNode:
A node of a singly-linked list.
Constructor:
ListNode<T>(value: T)Properties
value: TValue held by node.next: ListNode<T> | nullNext 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(): numberReturns length of list.insertAt(index: number, value: T): LinkedList<T>Insertsvalueatindexin 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 | nullReturns element atindex. Returnsnullif index is out of bounds for the list.deleteAt(index: number): T | nullDeletes element atindex, returns the deleted element. Returnsnullif 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> | nullReturns the head node of the list, returnsnullif 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(): numberReturns size of the stack (number of elements).peek(): T | nullReturns top element of stack. Returnsnullif stack empty.isEmpty(): booleanReturnstrueif stack empty,falseif otherwise.push(value: T): Stack<T>Pushesvalueon top of stack, returns theStackinstance.pop(): T | nullPops and returns top value from stack. Returnsnullif 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>Addsvalueto rear of queue, returns theQueueinstance.dequeue(): T | nullRemoves and returns element at front of queue. Returnsnullif queue empty.getFront(): T | nullReturns value in front of the queue. Returnsnullif queue empty.isEmpty(): booleanReturnstrueif queue empty,falseif otherwise.length(): numberReturns 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>Insertsvaluein the heap.extract(): T | nullRemoves and returns value from root of heap (highest priority). Returnsnullif heap empty.peek(): T | nullReturns value at root of heap.size(): numberReturns size of heap (number of elements)
BinaryTreeNode:
Constructor
BinaryTreeNode<T>(value: T)Parameters:
value(mandatory): Value for the binary tree node.
Properties:
value: Tvalue of nodeleft: BinaryTreeNode<T> | nullleft of current noderight: BinaryTreeNode<T> | nullright of current node
Methods:
isLeafNode(): booleanReturnstrueis node is leaf node,falseif otherwise.height(): numberReturns 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> | nullInverts subtree with node as root, returns the node.isBalanced(): booleanReturnstrueif subtree rooted at node is height balanced,falseif otherwise.isBinarySearchTree(keyFunction: (value: T) => number)Returnstrueif subtree rooted at node is a valid binary search tree, needs akeyFunctionto get numeric value from data stored in node (seeBinarySearchTree)
BinaryTree:
Abstract class - therefore, no constructor definition.
Properties:
root: BinaryTreeNode<T> | nullRoot 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(): numberReturns height of the tree.invert(): BinaryTree<T>Inverts the BinaryTree, returns the tree.isBalanced(): booleanReturnstrueif binary tree is height balanced,falseif 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 takesvalueof 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> | nullRoot node of the binary tree
Methods:
insert(value: T): BinarySearchTree<T>Inserts new node withvaluein BST, returns BST instance.delete(value: T): BinarySearchTree<T>Deletes node withvaluein BST, returns BST instance.search(value: T): BinaryTreeNode<T> | nullReturns tree node withvaluein BST,nullif node not found.getMin(): T | nullReturns value in tree with min key,nullif tree empty.getMax(): T | nullReturns value in tree with max key,nullif 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 takesvalueof 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> | nullRoot 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 withvaluein BST, returns AvlTree instance.delete(value: T): AvlTree<T>Deletes node withvaluein BST, returns AvlTree instance.search(value: T): BinaryTreeNode<T> | nullReturns tree node withvaluein BST,nullif node not found.getMin(): T | nullReturns value in tree with min key,nullif tree empty.getMax(): T | nullReturns value in tree with max key,nullif tree empty.