@datastructures-es6/core
v2.1.3
Published
Implementation of the most common data structures in Javascript
Downloads
5
Maintainers
Readme
Data Structures ES6
Implementation of the most common data structures in Javascript (ES6+), including linear and non-linear ones, such as Array
, Linked List
, Queue
, Stack
and Binary Tree
.
Futhermore, you will find here implementation of some interesting algorithms like Quick Sort
for sorting arrays and Breadth First Search
or Depth First Search (inOrder, preOrder, postOrder)
for binary trees.
Instalation
npm i @datastructures-es6/core
How to use this lib
- After install the package on your personal project, you may import the code in your files:
import {
Array,
LinkedList,
Queue,
Stack,
BinaryTree
} from '@datastructures-es6/core/src/index.js';
- Creating and using
data structures
(examples).
Array (code implementation)
const array = new Array();
array.push(10);
array.push(6);
array.push(2);
array.sort();
console.log(array.toString());
Linked List (code implementation)
const linkedList = new LinkedList();
linkedList.prepend(444);
linkedList.append(3);
linkedList.insert(1, 22);
linkedList.delete(0);
console.log(linkedList.get());
Queue (code implementation)
const queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
queue.enqueue(5);
queue.dequeue();
queue.dequeue();
let currentQueueItem = queue.peek();
while (currentQueueItem) {
console.log(currentQueueItem.value);
currentQueueItem = currentQueueItem.next;
}
Stack (code implementation)
const stack = new Stack();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.pop();
stack.pop();
let currentStackItem = stack.peek();
while (currentStackItem) {
console.log(currentStackItem.value);
currentStackItem = currentStackItem.next;
}
Binary Tree (code implementation)
const binaryTree = new BinaryTree();
binaryTree.insert(5);
binaryTree.insert(9);
binaryTree.insert(6);
binaryTree.insert(2);
binaryTree.insert(4);
binaryTree.insert(3);
binaryTree.insert(8);
console.log(binaryTree.breadthFirstSearch());
Available methods
ARRAY
- get(index?: number): returns the element value for a provided index or returns the entire array in case you do not provide an index.
- push(value: any): pushes the provided value in the current array.
- length(): returns the length of the current array.
- pop(): removes the last element from the current array and returns it.
- update(index: number, value: any): updates the value for a new provided one in the provided index.
- shift(): removes the first element from the current array and returns the resulting array.
- unshift(value: any): inserts the provided value in the beginning of the current array.
- find(value: any): if the current array has the provided value, it returns the finded index, else it returns -1.
- sort(): returns the sorted array, using
Quick sort
algorithm. - toString(): returns the entire array in a
string
format. - join(term: string): join all values of the current array into a string, separated by the provided term.
- concat(term: Array | any): concat the current array with a provided term (it can be any value or other Array).
- splice(startIndex: number, quantityToDelete: number, items: any): returns a new array with the provided value inserted in the provided index, shifting the remaining elements to right. However, if you provide a
n
value greater than zero in thequantity to delete
argument, it will delete then
elements after the provided index and replace the value instead of them. - slice(startIndex: number, endIndex?: number): returns a slice of the current array, based on the start/end indexes provided (if you do not provide the end index, it will consider the length of the current array).
LINKED LIST
- prepend(value: any): inserts the provided element in the beginning of the linked list and this element becomes the
head
. - append(value: any): inserts the provided element in the end of the linked list and this element becomes the
tail
. - lookup(value: any): finds the provided element in the linked list.
- insert(position: number, value: any): inserts the provided element in the provided position.
- delete(position: number): removes an element from the provided position.
- get(): returns a string containing the entire linked list.
QUEUE
- enqueue(value: any): inserts the selected value in the end of the queue.
- dequeue(): removes the first element of the queue.
- lookup(value: any): finds the selected element in the queue and return it.
- peek(): return the first element of the queue.
STACK
- push(value: any): inserts the provided value in the top of the stack.
- pop(): removes the element in the top of the stack.
- lookup(value: any): finds the provided element in the stack.
- peek(): return the element in the top of the stack.
BINARY TREE
- insert(value: any): inserts the provided value in the correct position on the current binary tree.
- delete(): removes the provided value from the binary tree.
- lookup(value: any): finds the provided element on the current binary tree.
- breadthFirstSearch(): return current binary tree after parse through the
Breadth First Search
algorithm. - depthFirstSearch(type: string): return current binary tree after parse through the
Depth First Search
algorithm using the approach provided by the type argument (it can beinOrder
,preOrder
orpostOrder
).
Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
Please make sure to update tests as appropriate.