TSDataStructure
Collection of data structures(LinkedList, DoubleLinkedList, Stack, Queue, Dictionary and etc...) for TypeScript.
Installation
npm i datastructure-ts --save
Data Structures
LinkedList
1->2->3->4
getHead(): LinkedNode
Returns the 'Head' node
isEmpty(): boolean
Returns 'true' if 'LinkedList' is empty and 'false' if no.
size(): number
Returns the amount of nodes
append(value: T): LinkedList
Append given value to the 'linkedList' and returns updated 'LinkedList'
getLast(): LinkedNode
Returns the 'Last' node
toArray(): T[]
Returns array of all items.
; // _head = null, _last = nulllinkedList.getHead; // nulllinkedList.append1;linkedList.getHead; // 1linkedList.isEmpty; // falselinkedList.append2;linkedList.size; // 2linkedList.toArray; // [1,2]linkedList.getLast; // 2
DoubleLinkedList
1<->2<->3<->4
getHead(): LinkedNode
Returns the 'Head' node
isEmpty(): boolean
Returns 'true' if 'LinkedList' is empty and 'false' if no.
size(): number
Returns the amount of nodes
append(value: T): LinkedList
Append given value to the 'linkedList' and returns updated 'LinkedList'
getLast(): LinkedNode
Returns the 'Last' node
toArray(): T[]
Returns array of all items.
; // _head = null, _last = nulldoubleLinkedList.getHead; // nulldoubleLinkedList.append1;doubleLinkedList.getHead; // 1doubleLinkedList.isEmpty; // falsedoubleLinkedList.append2;doubleLinkedList.size; // 2doubleLinkedList.toArray; // [1,2]doubleLinkedList.getLast; // 2
Dictionary
size(): number
Returns the number of entries (distinct keys) in this dictionary.
isEmpty(): boolean
Returns 'true' if Dictionary is empty and 'false' if no.
put(key: string, value: T)
Add new item to dictionary.
get(key: string): T
Returns item by given key.
remove(key: string)
Removes item from dictionary.
clear()
Removes all items from dictionary.
keys(): string[]
Returns string array of items keys.
toArray(): T[]
Returns array of all items.
containsKey(key: string): boolean
Checks is item with given key exist.
;dictionary.isEmpty; // truedictionary.size; // 0dictionary.put'someKey', 1;dictionary.get'someKey'dictionary.remove'someKey'; dictionary.put'key1', 1;dictionary.put'key2', 2;dictionary.put'key3', 3;dictionary.keys; // ['key1', 'key2', 'key3']dictionary.toArray; // [1, 2, 3]dictionary.clear; dictionary.size; // 0
Stack
push(value: T)
Pushes value to stack
pop(): T
Fetchs the last value from the stack
peek(): T
The method returns the element at the top of the Stack else returns NULL if the Stack is empty.
size(): T
Returns the size of stack
toArray(): T[]
Returns array of all items
;stack.push1;stack.push2;stack.push3;stack.pop; // 3stack.size; // 2
Queue
push(value: T)
Pushes value to stack
pop(): T
Fetchs the first value from the stack
peek(): T
The method returns the element at the top of the Queue else returns NULL if the Queue is empty.
size(): T
Returns the size of queue
toArray(): T[]
Returns array of all items
;queue.push1;queue.push2;queue.push3;queue.pop; // 1queue.size; // 2
BinaryTree
getRoot(): BinaryTreeNode
Returns 'root' node
findNode(value: T, node: BinaryTreeNode = null): BinaryTreeNode
Returns node with a given value
;binaryTree.getRoot.append2;binaryTree.getRoot.append3; binaryTree.getRoot.getLeft.append4;binaryTree.getRoot.getLeft.append5; binaryTree.getRoot.getRight.setLeft6;binaryTree.getRoot.getRight.setRight7; // 1// / \// 2 3 // / \ / \// 4 5 6 7 binaryTree.findNode6.getValue; // 6
Todo
1) Graph
If you have an advice, please feel free to contact with me