이 글에선 Graph, Tree, Binary Search Tree에 최소한의 알아야 할 정보들을 기록한다.
{
A: [B, C],
B: [A, C],
C: [A, B]
}
class Graph {
constructor() {
this.adjacencyList = {};
}
addVertex(vertex) {
if (!this.adjacencyList[vertex]) {
this.adjacencyList[vertex] = [];
return true;
}
return false;
}
addEdge(vertex1, vertex2) {
if (this.adjacencyList[vertex1] && this.adjacencyList[vertex2]) {
this.adjacencyList[vertex1].push(vertex2);
this.adjacencyList[vertex2].push(vertex1);
return true;
}
return false;
}
removeEdge(vertex1, vertex2) {
if (this.adjacencyList[vertex1] && this.adjacencyList[vertex2]) {
this.adjacencyList[vertex1] = this.adjacencyList[vertex1].filter(
(v) => v !== vertex2
);
this.adjacencyList[vertex2] = this.adjacencyList[vertex2].filter(
(v) => v !== vertex1
);
return true;
}
return false;
}
removeVertex(vertex) {
if (!this.adjacencyList[vertex]) return undefined;
while (this.adjacencyList[vertex].length) {
let temp = this.adjacencyList[vertex].pop();
this.removeEdge(vertex, temp);
}
delete this.adjacencyList[vertex];
}
}