Skip to content

Commit cb48efe

Browse files
committed
Make it possible to delete edge from graph vertex.
1 parent 1af824f commit cb48efe

File tree

4 files changed

+65
-8
lines changed

4 files changed

+65
-8
lines changed

src/data-structures/graph/GraphVertex.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,22 @@ export default class GraphVertex {
99
throw new Error('Graph vertex must have a value');
1010
}
1111

12+
/**
13+
* @param {GraphEdge} edgeA
14+
* @param {GraphEdge} edgeB
15+
*/
16+
const edgeComparator = (edgeA, edgeB) => {
17+
if (edgeA.getKey() === edgeB.getKey()) {
18+
return 0;
19+
}
20+
21+
return edgeA.getKey() < edgeB.getKey() ? -1 : 1;
22+
};
23+
1224
// Normally you would store string value like vertex name.
1325
// But generally it may be any object as well
1426
this.value = value;
15-
this.edges = new LinkedList();
27+
this.edges = new LinkedList(edgeComparator);
1628
}
1729

1830
/**
@@ -25,6 +37,13 @@ export default class GraphVertex {
2537
return this;
2638
}
2739

40+
/**
41+
* @param {GraphEdge} edge
42+
*/
43+
deleteEdge(edge) {
44+
this.edges.delete(edge);
45+
}
46+
2847
/**
2948
* @returns {GraphVertex[]}
3049
*/

src/data-structures/graph/__test__/GraphVertex.test.js

+33
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,39 @@ describe('GraphVertex', () => {
3737
expect(vertexA.getEdges()[0].toString()).toBe('A_B');
3838
});
3939

40+
it('should delete edges from vertex', () => {
41+
const vertexA = new GraphVertex('A');
42+
const vertexB = new GraphVertex('B');
43+
const vertexC = new GraphVertex('C');
44+
45+
const edgeAB = new GraphEdge(vertexA, vertexB);
46+
const edgeAC = new GraphEdge(vertexA, vertexC);
47+
vertexA
48+
.addEdge(edgeAB)
49+
.addEdge(edgeAC);
50+
51+
expect(vertexA.hasEdge(edgeAB)).toBeTruthy();
52+
expect(vertexB.hasEdge(edgeAB)).toBeFalsy();
53+
54+
expect(vertexA.hasEdge(edgeAC)).toBeTruthy();
55+
expect(vertexC.hasEdge(edgeAC)).toBeFalsy();
56+
57+
expect(vertexA.getEdges().length).toBe(2);
58+
59+
expect(vertexA.getEdges()[0].toString()).toBe('A_B');
60+
expect(vertexA.getEdges()[1].toString()).toBe('A_C');
61+
62+
vertexA.deleteEdge(edgeAB);
63+
expect(vertexA.hasEdge(edgeAB)).toBeFalsy();
64+
expect(vertexA.hasEdge(edgeAC)).toBeTruthy();
65+
expect(vertexA.getEdges()[0].toString()).toBe('A_C');
66+
67+
vertexA.deleteEdge(edgeAC);
68+
expect(vertexA.hasEdge(edgeAB)).toBeFalsy();
69+
expect(vertexA.hasEdge(edgeAC)).toBeFalsy();
70+
expect(vertexA.getEdges().length).toBe(0);
71+
});
72+
4073
it('should return vertex neighbors in case if current node is start one', () => {
4174
const vertexA = new GraphVertex('A');
4275
const vertexB = new GraphVertex('B');

src/data-structures/linked-list/LinkedList.js

+9-7
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,15 @@ export default class LinkedList {
6767

6868
let currentNode = this.head;
6969

70-
// If next node must be deleted then make next node to be a next next one.
71-
while (currentNode.next) {
72-
if (this.compare.equal(currentNode.next.value, value)) {
73-
deletedNode = currentNode.next;
74-
currentNode.next = currentNode.next.next;
75-
} else {
76-
currentNode = currentNode.next;
70+
if (currentNode !== null) {
71+
// If next node must be deleted then make next node to be a next next one.
72+
while (currentNode.next) {
73+
if (this.compare.equal(currentNode.next.value, value)) {
74+
deletedNode = currentNode.next;
75+
currentNode.next = currentNode.next.next;
76+
} else {
77+
currentNode = currentNode.next;
78+
}
7779
}
7880
}
7981

src/data-structures/linked-list/__test__/LinkedList.test.js

+3
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ describe('LinkedList', () => {
6767

6868
expect(linkedList.head.toString()).toBe('2');
6969
expect(linkedList.tail.toString()).toBe('2');
70+
71+
linkedList.delete(2);
72+
expect(linkedList.toString()).toBe('');
7073
});
7174

7275
it('should delete linked list tail', () => {

0 commit comments

Comments
 (0)