Data Structure Lite
Data Structure Lite is a JavaScript library for data structure focused on a few main functions. It supports data structures as follows: queue, priority queue, stack, linked list, doubly linked list, tree, binary search tree, heap. Each data structure can be used as an independent module. The data visualisation for Tree, Binary Search Tree and Heap is being prepared.
Getting Started
Installing
Install with npm:
npm install data-structure-lite
Import into your file (you can download the minimized js file at: github.com/birdieKim/data-structure-lite):
Usage
If you need a whole library:
var DSLite = ; // then you can use each modulevar queue = ;
If you need a certain module in this library:
// An example of queuevar Queue = ; // An example of priority queuevar PriorityQueue = ; // An example of stackvar Stack = ; // An example of linked listvar LinkedList = ; // An example of doubly linked listvar DoublyLinkedList = ; // An example of treevar Tree = ; // An example of binary search treevar BinarySearchTree = ; // An example of heapvar Heap = ;
Running Unit Tests
If you want to run the test:
npm run test
Exporting
If you want to export a whole library to a minimized file:
npm run prod
Then, you will get data-structure-lite.min.js file in 'dist' folder.
If you want to export only a certain module to a minimized file:
npm run prod_queue
Then, you will get queue.min.js file. Exporting other modules works in the same way as below:
npm run prod_priorityQueue
npm run prod_stack
npm run prod_linkedList
npm run prod_doublyLinkedList
npm run prod_tree
npm run prod_binarySearchTree
npm run prod_heap
Supported Data Structure
Queue
Create a queue:
var queue = ;
If you have a data for the first element in the queue:
// If you have 10 as the first element for the queuevar queue = 10;
Examples:
var queue = ; queue; // return: 1, queue: [100]queue; // return: 2, queue: [100, 200]queue; // return: 3, queue: [100, 200, 300] queue; // return: 100queue; // return: 100, queue: [200, 300]queue; // return: 200, queue: [300]queue; // return: 100, queue: []queueclear; // queue: []queue; // return: truequeue; // return: undefined
Priority Queue
Create a priority queue:
var pqueue = ;
Examples:
var pqueue = ; pqueue; // return: 10, queue: {10: ['Bear']}pqueue; // return: falsepqueue; // return: 10, queue: {10: ['Bear', 'Bird']}pqueuemaxPriority; // return: 10 pqueue; // return: 10, queue: {10: ['Bear', 'Bird'], 8: ['Dog']}pqueuemaxPriority; // return: 10pqueuesize; // return: 3pqueue; // return: 'Dog', queue: {10: ['Bear', 'Bird']} pqueuesize10; // return: 2pqueuesize8; // return: undefined pqueue; // return: undefinedpqueue; // return: undefined (the priority passed in is not a natural number)
Stack
Create a stack:
var stack = ;
If you have a data for the first element in the stack:
// If you have 10 as the first element for the stackvar stack = 10;
Examples:
var stack = ; stacktop; // return: -1 stack; // return: 1, stack: [10]stack; // return: 2, stack: [10, 20]stack; // return: 3, stack: [10, 20, 30] stacktop; // return: 2stack; // return: 30, stack: [10, 20]stack; // return: 20, stack: [10]stack; // return: 10, stack: []stack; // return: undefinedstack; // return: true stack; // return: 1, stack: [15]stack; // return: 2, stack: [15, 25]stack; // return: 3, stack: [15, 25, 35]stack; // return: 4, stack: [15, 25, 35, 45] stacktop; // return: 3 stackclear; // stack: [] stacktop; // return: -1stack; // return: true
Linked List
Create a linked list:
var linkedList = ;
If you have a data for the first data in the list:
// If you have 10 as the first data for the listvar linkedList = 10;
Examples:
var linkedList = ; linkedListhead; // return: nulllinkedList; // return: undefined (index passed in is not a natural number)linkedList; // return: undefined (index passed in is not a natural number) linkedList; // return: {data: 5, next: null}linkedListhead; // return: {data: 5, next: null} linkedList; // return: {data: 7, next: {data:5, next: null}}linkedList; // return: {data: 10, next: {data:7, next: {data:5, next: null}}}linkedList; // return: {data: 15, next: {data:10, // next: {data:7, next: {data:5, next: null}}}}linkedListheaddata; // return: 15 linkedList; // return: {data: 8, next: {data:5, next: null}}linkedListdata; // return: 8linkedListlength; // return: 5 linkedList; // return: {data: 7, next: {data: 8, next: {data:5, next: null}}}linkedListdata; // return: 8linkedListlength; // return: 4 linkedList; // return: {data:5, next: null} linkedListclear;linkedList; // return: true
Doubly Linked List
Create a doubly linked list:
var doublyLinkedList = ;
If you have a data for the first data in the list:
// If you have 10 as the first data for the listvar doublyLinkedList = 10;
Examples:
var doublyLinkedList = ; doublyLinkedListhead; // return: nulldoublyLinkedListlength; // return: 0 doublyLinkedList; // return: {data: 10, next: null, previous: null}doublyLinkedListhead; // return: {data: 10, next: null, previous: null}doublyLinkedListtail; // return: {data: 10, next: null, previous: null} doublyLinkedList; // return: {data: 20, next: null, previous: {data: 10, next: null, previous: null}}doublyLinkedListhead; // return: {data: 10, next: {data: 20, next: null, // previous: {data: 10, next: null, previous: null}}, // previous: null}doublyLinkedListtail; // return: {data: 20, next: null, previous: {data: 10, next: null, previous: null}} doublyLinkedList; // return: {data: 40, next: null, // previous: {data: 20, next: null, previous: {data: 10, next: null, previous: null}}doublyLinkedListtaildata; // return: 40doublyLinkedListtailpreviousdata; // return: 20 doublyLinkedList; // return: {data: 30, // next: {data: 40, next: null, previous: {data: 30, next: {data: 40, ...}}, // previous: {data: 20, next: {data: 30, ...}, previous: {data: 10, ...}}}doublyLinkedListtaildata; // return: 40doublyLinkedListtailpreviousdata; // return: 30 doublyLinkedList; // return: {data: 50, next: null, // previous: {data: 40, next: {data: 50, ...}, previous: {data: 30, ...}}}doublyLinkedList; // return: {data: 60, next: null, // previous: {data: 50, next: {data: 60, ...}, previous: {data: 40, ...}}}doublyLinkedList; // return: {data: 10, // next: {data: 20, next: {data: 30, ...}, previous: {data: 10, ...}}, // previous: null}doublyLinkedList; // return: {data: 30, // next: {data: 40, next: {data: 50, ...}, previous: {data: 30, ...}}, // previous: {data: 20, ...}} doublyLinkedList; // return: {data: 60, next: null, previous: {data: 50, ...}}doublyLinkedList; // return: undefined doublyLinkedListclear;doublyLinkedList; // return: true
Tree
Create a tree:
var tree = ;
If you have a data for the root node, the maximum number of children and a function for checking the equality between nodes:
// A Function for checking the equality between nodes// SHOULD have 2 parameters and return boolean valuevar { if a === b return true; else return false; };// If you have 10 as data of the first node, 2 as the max number of children that the parent node can have// and a function for checking the equality between nodes// If the equalFunc is undefined, the default function would be same as above.var tree = 10 2 equalFunc;
Add & Remove event callbacks
You can add or remove event callbacks using addEventListener() function. For now, the library supports only 'change' event which is occurred whenever data is changed in the tree. The callback provides an event object which has 2 properties: data, triggeredBy['insert', 'delete', 'clear', 'addToRoot']
tree;tree;
Examples:
var { if a === b return true; else return false; }; var tree = 'one' 2 equalFunc; tree_root; // return: {data: 'one', parent: null, children: []}tree; // tree: {data: 'one', parent: null, // children: [{data: 'two', parent: {data: 'one', ...}, children: []}]} tree_rootchildren0parentdata; // return: 'one' tree; // tree: {data: 'one', parent: null, // children: [{data: 'two', ...}, {data: 'three, ...'}]} tree; //=> Error! This tree has the max number of children(2) // so the parent node cannot have more than 2 children nodes tree; // tree: {data: 'one', parent: null, // children: [{data: 'two', parent: {data: 'one', ...}, children: [{data: 'four', ...}]}, // {data: 'three', ...}]} tree; // return: ['one', 'two', 'three', 'four']tree; // return: ['one', 'two', 'four', 'three'] tree; // tree: {data: 'one', parent: null, // children: [{data: 'two', children: [{data: 'four', ...}], ...}, // {data: 'three', children: [{data: 'five', ...}], ...}]}tree; // return: ['one!', 'two!', 'three!', 'four!'] tree; // tree: {data: 'one!', parent: null, // children: [{data: 'two!', children: [{data: 'four!', ...}], ...}]}tree; // return: ['one!', 'two!', 'four!'] tree; // tree: {data: 'zero', parent: null, // children: [{data: 'one!', parent: {data: 'zero', ...}, // children: [{data: 'two!', ...}]}]}tree_rootdata; // return: 'zero'tree; // return: ['zero', 'one!', 'two!', four!'] treeclear;tree; // return: true // create a callback function for the 'change' eventvar { console;};// add the callback for the 'change' eventtree;tree;// remove the callback for the 'change' eventtree;
Binary Search Tree
Create a binary search tree:
var tree = ;
If you have a data for the root node, the maximum number of children and a function for comparison between nodes:
// A Function for comparison between nodes// SHOULD have 2 parameters and return these:// a < b : return negative value// a === b : return 0// a > b : return positive valuevar { return a - b;};// If you have 10 as data of the first node// and a function for checking the equality between nodes// If the compareFunc is undefined, the default function would be same as above.var tree = 10 compareFunc;
Add & Remove event callbacks
You can add or remove event callbacks using addEventListener() function. For now, the library supports only 'change' event which is occurred whenever data is changed in the tree. The callback provides an event object which has 2 properties: data, triggeredBy['insert', 'delete', 'clear']
tree;tree;
Examples:
var { return a - b;}; var tree = 10 compareFunc; tree_root; // return: {data: 10, left: null, right: null} tree;tree;tree;tree;tree; tree; // return: [5, 7, 10, 15, 20, 25] tree; tree;tree; // return: undefined (50 is not in the binary search tree) tree; // return: [15, 5, 3, 7, 20, 25]tree; // return: undefined (0 is not in the binary search tree) tree; // return: {data: 15, left: {data: 5, ...}, right: {data: 20, ...}} tree; // return: [3, 7, 5, 25, 20, 15] tree; // return: {data: 3, left: null, right: nulltree; // return: {data: 15, left: null, right: null treeclear;tree; // return: truetree_root; // return: undefinedtree; // return: undefinedtree; // return: undefined (0 is not in the binary search tree)tree; // return: undefined (The binary search tree is empty) // create a callback function for the 'change' eventvar { console;};// add the callback for the 'change' eventtree;tree;// remove the callback for the 'change' eventtree;
Heap
Create a heap:
var heap = ;
If you have a data for the root node or an array data for the heap, the maximum number of children and a function for comparison between nodes and the type for the heap ('MinHeap'/'MaxHeap' - 'MinHeap' is a default):
// A Function for comparison between nodes// SHOULD have 2 parameters and return these:// a < b : return negative value// a === b : return 0// a > b : return positive valuevar { return a - b;};// If you have 10 as data of the first node// and a function for checking the equality between nodes// If the compareFunc is undefined, the default function would be same as above.// If you want to have a min heap:var heap = 10 compareFunc 'MinHeap'; // If you have an array [1 ,2 , 3, 4, 5]// and a function for checking the equality between nodes// If the compareFunc is undefined, the default function would be same as above.// If you want to have a max heap:var anotherHeap = 1 2 3 4 5 compareFunc 'MaxHeap';
Add & Remove event callbacks
You can add or remove event callbacks using addEventListener() function. For now, the library supports only 'change' event which is occurred whenever data is changed in the tree. The callback provides an event object which has 2 properties: data, index, triggeredBy['insert', 'deleteRoot', 'clear']
tree;tree;
Examples:
var { return a - b;}; var heap = 10 compareFunc 'MaxHeap'; heap; // return: 10heap_type; // 'MaxHeap'heap;heap_heapArray; // return: [30, 10] heap = 50 40 30 20 10; heap; // return: 10heap_type; // return: 'MinHeap'heap; // return: 10heap_heapArray; // return: [20, 30, 40, 50] heap = 100 80 70 120 50 undefined 'MaxHeap'; heap_heapArray; // return: [120, 100, 70, 80, 50]heap; // return: 120heap; // return: 100heap; // return: falseheapclear;heap; // return: undefined (The heap is empty)heap; // return: true // create a callback function for the 'change' eventvar { console;};// add the callback for the 'change' eventheap;heap;// remove the callback for the 'change' eventheap;