Collection of data structures(LinkedList, DoubleLinkedList, Stack, Queue, Dictionary and etc...) for TypeScript.
npm i datastructure-ts --save
1->2->3->4
Returns the 'Head' node
Returns 'true' if 'LinkedList' is empty and 'false' if no.
Returns the amount of nodes
Append given value to the 'linkedList' and returns updated 'LinkedList'
Returns the 'Last' node
Returns array of all items.
const linkedList: LinkedList<number>; // _head = null, _last = null
linkedList.getHead(); // null
linkedList.append(1);
linkedList.getHead(); // 1
linkedList.isEmpty(); // false
linkedList.append(2);
linkedList.size(); // 2
linkedList.toArray(); // [1,2]
linkedList.getLast(); // 21<->2<->3<->4
Returns the 'Head' node
Returns 'true' if 'DoubleLinkedList' is empty and 'false' if no.
Returns the amount of nodes
Append given value to the 'DoubleLinkedList' and returns updated 'DoubleLinkedList'
Returns the 'Last' node
Returns array of all items.
Inserts given value to the DoubleLinkedList and returns updated DoubleLinkedList.
Inserts given value to the DoubleLinkedList and returns updated DoubleLinkedList.
Insert given value to the 'DoubleLinkedList' at the end and returns updated 'DoubleLinkedList'.
Inserts given value to the 'DoubleLinkedList' at the beginning and returns updated 'DoubleLinkedList'.
Get node by given index.
const doubleLinkedList: DoubleLinkedList<number>; // _head = null, _last = null
doubleLinkedList.getHead(); // null
doubleLinkedList.append(1);
doubleLinkedList.getHead(); // 1
doubleLinkedList.isEmpty(); // false
doubleLinkedList.append(2);
doubleLinkedList.size(); // 2
doubleLinkedList.toArray(); // [1,2]
doubleLinkedList.getLast(); // 2Returns the number of entries (distinct keys) in this dictionary.
Returns 'true' if Dictionary is empty and 'false' if no.
Add new item to dictionary.
Returns item by given key.
Removes item from dictionary.
Removes all items from dictionary.
Returns string array of items keys.
Returns array of all items.
Checks is item with given key exist.
const dictionary = new Dictionary<number>();
dictionary.isEmpty(); // true
dictionary.size(); // 0
dictionary.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(); // 0Pushes value to stack
Fetchs the last value from the stack
The method returns the element at the top of the Stack else returns NULL if the Stack is empty.
Returns the size of stack
Returns array of all items
const stack = new Stack<number>();
stack.push(1);
stack.push(2);
stack.push(3);
stack.pop(); // 3
stack.size(); // 2Pushes value to stack
Fetchs the first value from the stack
The method returns the element at the top of the Queue else returns NULL if the Queue is empty.
Returns the size of queue
Returns array of all items
const queue = new Queue<number>();
queue.push(1);
queue.push(2);
queue.push(3);
queue.pop(); // 1
queue.size(); // 2Insert an element into the queue with an associated priority.
Removes and returns the element with the highest priority, or null if the queue is empty.
Returns the element with the highest priority without removing it, or null if the queue is empty.
Returns the number of elements in the queue.
Returns true if the queue has no elements, otherwise false.
Returns an array of the values in priority order.
const pq = new PriorityQueue<number>();
pq.push(5, 5);
pq.push(1, 1);
pq.push(3, 3);
pq.pop(); // 1
pq.peek(); // 3
pq.size(); // 2Adds a vertex to the graph.
Adds a directed edge between two vertices with optional weight.
Traverse the graph in breadth-first order starting from the given vertex.
Traverse the graph in depth-first order starting from the given vertex.
Returns the shortest path between two vertices.
const g = new Graph<number>();
g.addEdge(1, 2);
g.addEdge(1, 3);
const path = g.shortestPath(1, 3);AVLTree maintains balance automatically after each insertion to keep
operations fast.
const avl = new AVLTree<number>();
avl.insert(1);
avl.insert(2);
avl.insert(3); // tree rotates, keeping 2 as the root
avl.contains(3); // true
avl.toArray(); // [1, 2, 3]Returns 'root' node
Returns node with a given value
Deletes node from tree
Returns in order traversal
Returns pre-order traversal of tree
Returns level-order traversal of tree
Returns the height of the tree
Returns the depth of the node
Returns the max node in tree, if given node is null, returns max node in tree
Returns an array of nodes in tree
Returns a string representation of the tree
const binaryTree = new BinaryTree<number>(1);
binaryTree.getRoot().append(2);
binaryTree.getRoot().append(3);
binaryTree.getRoot().getLeft().append(4);
binaryTree.getRoot().getLeft().append(5);
binaryTree.getRoot().getRight().setLeft(6);
binaryTree.getRoot().getRight().setRight(7);
// 1
// / \
// 2 3
// / \ / \
// 4 5 6 7
binaryTree.findNode(6).getValue(); // 6Inserts the specified key-value pair into the map. If the key already exists, its value will be updated with the new value.
Returns the value associated with the specified key, or null if the key is not present in the map.
Removes the entry with the specified key from the map, if it exists. Returns true if the entry was removed, false if it did not exist in the map.
Returns true if the map contains the specified key, otherwise false.
Returns true if the map contains the specified value, otherwise false.
Returns a Set of all the keys in the map.
Returns an array of all the values in the map.
Returns a Set of all the key-value pairs (as tuples) in the map.
Returns the number of key-value pairs in the map.
Returns true if the map is empty, otherwise false.
Removes all entries from the map.
Returns the value of the node.
Sets the value of the node.
Returns the parent of the node.
Sets the parent of the node.
Returns the children of the node.
Adds a child to the node.
Removes a child from the node.
Removes all children from the node.
Returns the height of the node.
Returns the depth of the node.
Traverse the tree in preorder (root, left subtree, right subtree) and apply the given callback function to each node.
Traverse the tree in postorder (left subtree, right subtree, root) and apply the given callback function to each node.
Traverse the tree in inorder (left subtree, root, right subtree) and apply the given callback function to each node.
Traverse the tree level by level, starting at the root, and apply the given callback function to each node.
Returns the root node of the tree.
Adds a child node to a given parent node. Returns the newly created child node.
Removes a node and all of its children from the tree.
Returns the number of nodes in the tree.
Checks if the tree is empty (i.e., has no nodes).
Traverse the tree in breadth-first order and apply the given callback function to each node.
If you have an advice, please feel free to contact with me
This project uses git hooks stored in the .githooks directory. The
pre-commit hook automatically bumps the package patch version each time you
commit, while the commit-msg hook appends the commit message to the
CHANGELOG.md under the Unreleased section. Enable the hooks with the
following command:
git config core.hooksPath .githooksInstall dependencies before running lint or tests:
npm install
npm run lint