Skip to content

Commit 83c5267

Browse files
author
Abhishek Prakash
committed
Merge branch 'master' into feature/#20_singly_linked_list
Merging changes of master to singly linked list
2 parents 61d6efd + a2360e0 commit 83c5267

File tree

12 files changed

+6062
-466
lines changed

12 files changed

+6062
-466
lines changed

.npmignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
coverage/
3+
npm-debug.log
4+
demo/

CNAME

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data-structures.abhishekprakash.com

README.md

Lines changed: 152 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,25 +9,42 @@ Data Structures in Javascript
99
# Background
1010
There are neither a lot of resources on internet nor any book which guides and dictates best practices in the implementation of popular Data Structures using Javascript. The purpose of this library is to provide cooked implementation of populare data structures in javascript.
1111

12+
# Installation
13+
npm - `npm install es6-data-structures`
14+
1215
# Getting hands dirty
1316
Clone the repo
14-
```git clone https://github.com/linux-nerd/data-structures.js.git```
17+
`git clone https://github.com/linux-nerd/data-structures.js.git`
1518

1619
Install the dependencies
17-
```npm install```
20+
`npm install`
1821

1922
Run dev build
20-
```npm run dev```
23+
- For linux and darwin
24+
`npm run dev`
25+
26+
- For windows
27+
`npm run dev:win32`
2128

2229
To execute unit test cases
23-
```npm test```
30+
- For linux and darwin
31+
`npm test`
32+
33+
- For windows
34+
`npm test:win32`
2435

2536
Trigger production build
26-
```npm run build```
37+
- For linux and darwin
38+
`npm run build`
39+
40+
- For windows
41+
`npm run build:win32`
2742

2843
# List of Data Structures
2944
Data structures covered so far -
30-
- [Binary Search Tree](https://github.com/linux-nerd/data-structures.js/wiki/Binary-Search-Tree)
45+
- [Binary Search Tree](#binary-search-tree)
46+
- [Graph](#graph)
47+
- [Queue](#queue)
3148

3249
# Contribution
3350
Your contribution is highly appreciated. You can contribute in several ways -
@@ -47,7 +64,135 @@ and can be written in -
4764
* typescript
4865

4966
and will be published in
50-
- npm
67+
- ~~npm~~
5168
- bower
5269

5370

71+
# <a name="binary-search-tree"></a>Binary Search Tree
72+
Import BST class and instantiate it
73+
74+
```js
75+
import { BST } from 'es6-data-structures/lib/ds';
76+
const bst = new BST
77+
```
78+
79+
Insert values in binary search Tree
80+
81+
```js
82+
bst.insert(5);
83+
bst.insert(20);
84+
bst.insert(10);
85+
```
86+
87+
Find size of the binary search tree
88+
89+
```js
90+
bst.len // 3
91+
```
92+
93+
Find an item in the binary search tree
94+
95+
```js
96+
bst.lookup(10) // returns an object with keys hasVal, currentNode and parentNode
97+
```
98+
99+
Height of the binary search tree or a node
100+
101+
```js
102+
bst.height() //gives height of the BST 1
103+
bst.height(bst.lookup(10).currentNode) // gives the height of the node - 0
104+
```
105+
106+
Traverse the BST and return a List
107+
108+
```js
109+
bst.traverse('inOrder') // traverse method expects a parameter - inOrder|preOrder|postOrder| levelOrder
110+
```
111+
112+
Delete elements from binary search tree
113+
114+
```js
115+
bst.delete(10);
116+
bst.delete(20);
117+
```
118+
119+
# <a name="graph"></a> Graph
120+
Import Graph class and instantiate it and create an object of adjacency list implementation of Graph. To create a directed graph pass the string argument '**directed**'. If the Graph class is called without a parameter then by default its undirected graph.
121+
122+
```js
123+
import { Graph } from 'es6-data-structures/lib/ds';
124+
const graph = new Graph; // this will create an undirected Graph
125+
const graph = new Graph('directed'); // this will create a directed graph or diGraph
126+
127+
const adjList = graph.createGraph('adjList'); // create Adjacency List implementation of graph
128+
```
129+
130+
Add and remove a node to the graph
131+
132+
```js
133+
// add a node
134+
adjList.addNode('A');
135+
adjList.addNode('B');
136+
137+
// remove a node
138+
adjList.removeNode('A');
139+
adjList.removeNode('B');
140+
```
141+
142+
Add and remove an edge between two nodes to the graph. iF a node is not added, then it first adds the node and then create an edge.
143+
144+
```js
145+
// add an edge
146+
adjList.addEdge('A', 'B', 200); // it will add an edge between A and B of weight 200
147+
adjList.addEdge('B', 'C'); // it will first add node C and then create an edge b/w B and C
148+
149+
// remove an edge
150+
adjList.removeEdge('A', 'B');
151+
adjList.removeEdge('B', 'C');
152+
```
153+
154+
Find size of the graph.
155+
156+
```js
157+
adjList.size // 3
158+
```
159+
160+
Find weight of the edge in weighted graph
161+
162+
```js
163+
adjList.getEdgeWeight('A', 'B');
164+
```
165+
166+
# <a name="queue"></a> Queue
167+
168+
Import Queue class and create a queue object.
169+
170+
```js
171+
import { Queue } from 'es6-data-structures/lib/ds';
172+
const queue = new Queue;
173+
```
174+
175+
Add and remove elements to and from the created queue respectively
176+
177+
```js
178+
// add elements to the queue
179+
queue.enqueue('A');
180+
queue.enqueue(123);
181+
182+
// remove elements from the queue
183+
queue.dequeue();
184+
queue.dequeue();
185+
```
186+
187+
Get size and top element in the queue
188+
189+
```js
190+
queue.size() // 2
191+
queue.top() // A
192+
```
193+
194+
Clear the entire queue at once
195+
196+
```js
197+
queue.clear() // this will empty the queue
198+
```

karma.conf.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ module.exports = function (config) {
1111
// base path that will be used to resolve all patterns (eg. files, exclude)
1212
basePath: '',
1313

14-
1514
// frameworks to use
1615
// available frameworks: https://npmjs.org/browse/keyword/karma-adapter
1716
frameworks: ['jasmine'],

0 commit comments

Comments
 (0)