implemented linked list, basic testing

This commit is contained in:
Mikayla Dobson
2022-12-19 17:20:18 -06:00
parent e0771e4511
commit 12af1f9af6
5 changed files with 204 additions and 28 deletions

58
src/lib/Graph/index.ts Normal file
View File

@@ -0,0 +1,58 @@
import GraphNode from "./GraphNode";
export default class Graph<T> {
isWeighted: boolean;
isDirected: boolean;
points: GraphNode<T>[];
constructor(isWeighted: boolean, isDirected: boolean, points?: GraphNode<T>[]) {
this.isWeighted = isWeighted;
this.isDirected = isDirected
this.points = points || new Array<GraphNode<T>>();
}
createPoint(data: T) {
const newPoint = new GraphNode<T>(data);
this.points.push(newPoint);
return newPoint;
}
addPoints(...points: GraphNode<T>[]) {
for (let point of points) {
this.points.push(point);
}
}
removePoint(target: GraphNode<T>) {
this.points.filter((point: GraphNode<T>) => point !== target);
}
removePointByData(data: T) {
this.points.filter((point: GraphNode<T>) => point.data !== data);
}
addEdge(start: GraphNode<T>, end: GraphNode<T>, weight?: number) {
if (this.isWeighted && weight) {
start.addEdge(end, weight);
end.addEdge(start, weight);
} else {
start.addEdge(end);
end.addEdge(start);
}
}
removeEdge(start: GraphNode<T>, end: GraphNode<T>) {
try {
start.removeEdge(end);
end.removeEdge(start);
} catch(e: any) {
throw new Error(e);
}
}
print() {
for (let each of this.points) {
each.print();
}
}
}