Skip to content

Commit 14c0dac

Browse files
committed
adding graph data structure with adjancency matrix logic to store the graph values
1 parent cdca901 commit 14c0dac

File tree

3 files changed

+217
-0
lines changed

3 files changed

+217
-0
lines changed

10-chapter-Binary-Trees.js

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// Node class example for binary tree data structure
2+
class Node {
3+
constructor(data, left = null, right = null) {
4+
this.data = data;
5+
this.left = left;
6+
this.right = right;
7+
}
8+
9+
show() {
10+
console.log('Node ->', this.data);
11+
}
12+
}
13+
14+
class BinaryTree {
15+
constructor(root = null, nodes = 0) {
16+
this.root = root;
17+
this.numNodes = nodes;
18+
}
19+
20+
insert(data) {
21+
const n = new Node(data);
22+
if(this.root === null) {
23+
this.root = n;
24+
} else {
25+
let current = this.root;
26+
while(!(current === null)) {
27+
let parent = current;
28+
if(data < current.data) {
29+
current = current.left;
30+
if(current === null) {
31+
parent.left = n;
32+
}
33+
} else {
34+
current = current.right;
35+
if (current === null) {
36+
parent.right = n;
37+
}
38+
}
39+
}
40+
}
41+
this.numNodes++;
42+
}
43+
44+
inOrder(node) {
45+
if(!(node === null)) {
46+
this.inOrder(node.left);
47+
node.show();
48+
this.inOrder(node.right);
49+
}
50+
}
51+
52+
preOrder(node) {
53+
if(!(node === null)) {
54+
node.show();
55+
this.preOrder(node.left);
56+
this.preOrder(node.right);
57+
}
58+
}
59+
60+
postOrder(node) {
61+
if(!(node === null)) {
62+
this.postOrder(node.left);
63+
this.postOrder(node.right);
64+
node.show();
65+
}
66+
}
67+
68+
get minValue() {
69+
let current = this.root;
70+
while (!(current.left === null)) {
71+
current = current.left;
72+
}
73+
return current.data;
74+
}
75+
76+
get maxValue() {
77+
let current = this.root;
78+
while (!(current.right === null)) {
79+
current = current.right;
80+
}
81+
return current.data;
82+
}
83+
84+
get totalNodes() {
85+
return this.numNodes;
86+
}
87+
88+
totalEdges(node) {
89+
if (node === null) {
90+
return 0;
91+
}
92+
if (node.left === null && node.right === null) {
93+
return 1;
94+
}
95+
else {
96+
return this.totalEdges(node.left) + this.totalEdges(node.right);
97+
}
98+
}
99+
100+
find(data) {
101+
let current = this.root;
102+
while((current.data !== data)) {
103+
let parent = current;
104+
if(data < current.data) {
105+
current = current.left;
106+
} else {
107+
current = current.right;
108+
}
109+
if(current === null) {
110+
return undefined;
111+
}
112+
}
113+
return current;
114+
}
115+
116+
remove(data) {
117+
let current = this.root;
118+
119+
}
120+
121+
}
122+
123+
// Implementation
124+
// ####################################
125+
const nums = new BinaryTree();
126+
nums.insert(23);
127+
nums.insert(45);
128+
nums.insert(16);
129+
nums.insert(37);
130+
nums.insert(3);
131+
nums.insert(99);
132+
nums.insert(22);
133+
console.log("Inorder traversal: ");
134+
nums.inOrder(nums.root);
135+
console.log("\nPreorder traversal: ");
136+
nums.preOrder(nums.root);
137+
console.log("\nPostorder traversal: ");
138+
nums.postOrder(nums.root);
139+
console.log(`Find value 45: `);
140+
console.log(nums.find(45));
141+
/*
142+
1. Add a function to the BST class that counts the number of nodes in a BST.
143+
*/
144+
console.log('\n\n### Exercise 1');
145+
console.log(`Total of nodes: ${nums.totalNodes} `);
146+
/*
147+
2. Add a function to the BST class that counts the number of edges in a BST..
148+
*/
149+
console.log('\n\n### Exercise 2');
150+
console.log(`Total of edges: ${nums.totalEdges(nums.root)} `);
151+
/*
152+
3. Add a max() function to the BST class that finds the maximum value in a BST.
153+
*/
154+
console.log('\n\n### Exercise 3');
155+
console.log(`Get Max value of the tree: ${nums.maxValue} `);
156+
/*
157+
4. Add a min() function to the BST class that finds the minimum value in a BST.
158+
*/
159+
console.log('\n\n### Exercise 4');
160+
console.log(`Min value of the tree: ${nums.minValue}`);

11-chapter-Graphs.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
class Vertex {
2+
constructor(label) {
3+
this.label = label;
4+
}
5+
}
6+
7+
class Graph {
8+
constructor(vertices = []) {
9+
this.vertices = vertices;
10+
this.adjencency = [];
11+
for (let i = 0; i < this.vertices.length; i++) {
12+
this.adjencency[i] = [];
13+
for (let j = 0; j < this.vertices.length; j++) {
14+
this.adjencency[i].push(0);
15+
}
16+
}
17+
}
18+
19+
addEdge(v,w) {
20+
const a = this.vertices.indexOf(v);
21+
const b = this.vertices.indexOf(w);
22+
23+
this.adjencency[a][b] = 1;
24+
this.adjencency[b][a] = 1;
25+
}
26+
27+
showGraph() {
28+
let val = ' -> ';
29+
for (const v in this.vertices) {
30+
val += `${this.vertices[v]} `
31+
}
32+
console.log(val);
33+
for (const v in this.adjencency) {
34+
let value = `${this.vertices[v]} -> `;
35+
for (const w in this.adjencency[v]) {
36+
if (this.adjencency[v][w] !== undefined) {
37+
value += `${this.adjencency[v][w]} `
38+
}
39+
}
40+
console.log(value);
41+
}
42+
}
43+
44+
}
45+
46+
// Implementation
47+
// #############################################
48+
const vertices = ['a','b','c','d','e'];
49+
50+
let g = new Graph(vertices);
51+
g.addEdge('a','b');
52+
g.addEdge('a','c');
53+
g.addEdge('b','d');
54+
g.addEdge('c','e');
55+
g.showGraph();

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ The purpose of this repo is to update the exercises to ES6 standards and to corr
2121
| 8.- | [Dictionaries](./07-chapter-Dictionaries.js) | 3 | A `Dictionary` is a data structure that stores data as key-value pairs.
2222
| 9.- | [Hashing](./08-chapter-Hashing.js) | 2 | `Hashing` is a common technique for storing data in such a way that the data can be inserted and retrieved very quickly. Hashing uses a data structure called a hash table. Although hash tables provide fast insertion, deletion, and retrieval, they perform poorly for operations that involve searching.
2323
| 10.- | [Set](./09-chapter-Set.js) | 4 | A `Set` is a collection of unique elements. The elements of a set are called members. The two most important properties of sets are that the members of a set are unordered and that no member can occur in a set more than once.
24+
| 11.- | [Binary Trees and Binary Search Trees](./10-chapter-Binary-Trees.js) | 4 | `Binary trees` are chosen over other more primary data structures because you can search a binary tree very quickly (as opposed to a linked list, for example) and you can quickly insert and delete data from a binary tree (as opposed to an array).
25+
| 12.- | [Graphs and Graphs Algorithms](./11-chapter-Graphs.js) | | A graph consists of a set of vertices and a set of edges. Think of a map of a US state. Each town is connected with other towns via some type of road. A map is a type of graph where each town is a vertex, and a road that connects two towns is an edge. Edges are defined as a pair (v1, v2), where v1 and v2 are two vertices in a graph
2426

2527

2628
### To run the examples we need the following:

0 commit comments

Comments
 (0)