@@ -11,23 +11,25 @@ There are neither a lot of resources on internet nor any book which guides and d
1111
1212# Getting hands dirty
1313Clone the repo
14- ``` git clone https://github.com/linux-nerd/data-structures.js.git `` `
14+ ` git clone https://github.com/linux-nerd/data-structures.js.git `
1515
1616Install the dependencies
17- ``` npm install `` `
17+ ` npm install `
1818
1919Run dev build
20- ``` npm run dev `` `
20+ ` npm run dev `
2121
2222To execute unit test cases
23- ``` npm test `` `
23+ ` npm test `
2424
2525Trigger production build
26- ``` npm run build `` `
26+ ` npm run build `
2727
2828# List of Data Structures
2929Data structures covered so far -
3030- [ Binary Search Tree] ( #binary-search-tree )
31+ - [ Graph] ( #graph )
32+ - [ Queue] ( #queue )
3133
3234# Contribution
3335Your contribution is highly appreciated. You can contribute in several ways -
@@ -89,3 +91,76 @@ Delete elements from binary search tree
8991bst .delete (10 );
9092bst .delete (20 );
9193```
94+
95+ # <a name =" graph " ></a > Graph
96+ 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.
97+ ``` js
98+ import { Graph } from ' data-structures.js/lib/data-structures' ;
99+ const graph = new Graph ; // this will create an undirected Graph
100+ const graph = new Graph (' directed' ); // this will create a directed graph or diGraph
101+
102+ const adjList = graph .createGraph (' adjList' ); // create Adjacency List implementation of graph
103+ ```
104+
105+ Add and remove a node to the graph
106+ ``` js
107+ // add a node
108+ adjList .addNode (' A' );
109+ adjList .addNode (' B' );
110+
111+ // remove a node
112+ adjList .removeNode (' A' );
113+ adjList .removeNode (' B' );
114+ ```
115+
116+ 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.
117+ ``` js
118+ // add an edge
119+ adjList .addEdge (' A' , ' B' , 200 ); // it will add an edge between A and B of weight 200
120+ adjList .addEdge (' B' , ' C' ); // it will first add node C and then create an edge b/w B and C
121+
122+ // remove an edge
123+ adjList .removeEdge (' A' , ' B' );
124+ adjList .removeEdge (' B' , ' C' );
125+ ```
126+
127+ Find size of the graph.
128+ ``` js
129+ adjList .size // 3
130+ ```
131+
132+ Find weight of the edge in weighted graph
133+ ``` js
134+ adjList .getEdgeWeight (' A' , ' B' );
135+ ```
136+
137+ # <a name =" queue " ></a > Queue
138+
139+ Import Queue class and create a queue object.
140+
141+ ``` js
142+ import { Queue } from ' data-structures.js/lib/data-structures' ;
143+ const queue = new Queue ;
144+ ```
145+
146+ Add and remove elements to and from the created queue respectively
147+ ``` js
148+ // add elements to the queue
149+ queue .enqueue (' A' );
150+ queue .enqueue (123 );
151+
152+ // remove elements from the queue
153+ queue .dequeue ();
154+ queue .dequeue ();
155+ ```
156+
157+ Get size and top element in the queue
158+ ``` js
159+ queue .size () // 2
160+ queue .top () // A
161+ ```
162+
163+ Clear the entire queue at once
164+ ``` js
165+ queue .clear () // this will empty the queue
166+ ```
0 commit comments