testing graph structure

This commit is contained in:
Mikayla Dobson
2022-12-19 18:10:39 -06:00
parent b16f19e500
commit b377cefa78
5 changed files with 52 additions and 22 deletions

View File

@@ -9,7 +9,7 @@
</head>
<body>
<div id="app">
<h1>Wew</h1>
<h1>Workshopping DS/A for Procedural Music Generation</h1>
<div id="where-thing-go">
</div>

View File

@@ -3,10 +3,12 @@ import Edge from "./Edge";
export default class GraphNode<T> extends Node<T> {
edges: Edge<T>[]
id: number
constructor(data: T, edges?: Edge<T>[]) {
constructor(data: T, id: number, edges?: Edge<T>[]) {
super(data);
this.edges = edges || new Array<Edge<T>>();
this.id = id;
}
addEdge(destination: GraphNode<T>, weight?: number) {
@@ -19,9 +21,11 @@ export default class GraphNode<T> extends Node<T> {
override print() {
if (this.edges.length) {
for (let edge of this.edges) {
console.log(`Event ${this.id} --> Event ${edge.end.id}`);
}
} else {
console.log(this.data);
console.log(`Event ${this.id} -->`);
}
}
}

View File

@@ -1,9 +1,10 @@
import GraphNode from "./GraphNode";
export default class Graph<T> {
isWeighted: boolean;
isDirected: boolean;
points: GraphNode<T>[];
private isWeighted: boolean;
private isDirected: boolean;
private points: GraphNode<T>[];
private count = 0;
constructor(isWeighted: boolean, isDirected: boolean, points?: GraphNode<T>[]) {
this.isWeighted = isWeighted;
@@ -12,17 +13,28 @@ export default class Graph<T> {
}
createPoint(data: T) {
const newPoint = new GraphNode<T>(data);
const newPoint = new GraphNode<T>(data, this.count);
this.points.push(newPoint);
this.count++;
return newPoint;
}
addPoints(...points: GraphNode<T>[]) {
addPoints(...points: T[]) {
for (let point of points) {
this.points.push(point);
this.createPoint(point);
}
}
getPointByData(data: T): GraphNode<T> | null {
for (let point of this.points) {
if (point.data === data) {
return point;
}
}
return null;
}
removePoint(target: GraphNode<T>) {
this.points.filter((point: GraphNode<T>) => point !== target);
}
@@ -32,12 +44,16 @@ export default class Graph<T> {
}
addEdge(start: GraphNode<T>, end: GraphNode<T>, weight?: number) {
if (this.isWeighted && weight) {
start.addEdge(end, weight);
end.addEdge(start, weight);
if (this.isDirected) {
this.isWeighted ? start.addEdge(end, weight) : start.addEdge(end);
} else {
start.addEdge(end);
end.addEdge(start);
if (this.isWeighted) {
start.addEdge(end, weight);
end.addEdge(start, weight);
} else {
start.addEdge(end);
end.addEdge(start);
}
}
}

View File

@@ -1,6 +1,8 @@
import { ChordQuality, EventData } from "./index";
import LinkedList from "../LinkedList/index";
import ListNode from "../LinkedList/ListNode";
import Graph from "../Graph/index";
import GraphNode from "../Graph/GraphNode";
const firstEvent: EventData = {
root: "C",
@@ -43,16 +45,23 @@ const fourthEvent: EventData = {
const eventlist = new LinkedList<EventData>();
eventlist.addManyToTail(firstEvent, secondEvent, thirdEvent, fourthEvent);
const thirdnode = eventlist.findByData(thirdEvent) as ListNode<EventData>;
// console.log(eventlist.findByNode(thirdnode));
// eventlist.removeHead();
// eventlist.removeTail();
// eventlist.removeAtPosition(3);
// eventlist.removeByData(fourthEvent);
// const thirdnode = eventlist.findByData(thirdEvent) as ListNode<EventData>;
// console.log(eventlist.findByNode(thirdnode));
const eventgraph = new Graph<EventData>(false, true);
console.log(eventlist);
eventgraph.addPoints(firstEvent, secondEvent, thirdEvent, fourthEvent);
export default { eventlist }
const secondstop = eventgraph.getPointByData(secondEvent) as GraphNode<EventData>;
const thirdstop = eventgraph.getPointByData(thirdEvent) as GraphNode<EventData>;
const fourthstop = eventgraph.getPointByData(fourthEvent) as GraphNode<EventData>;
eventgraph.addEdge(secondstop, thirdstop);
eventgraph.addEdge(thirdstop, secondstop);
eventgraph.addEdge(secondstop, fourthstop);
export default { eventlist, eventgraph }

View File

@@ -1,7 +1,8 @@
import data from "./lib/helpers/sample";
const { eventlist } = data;
const { eventlist, eventgraph } = data;
eventlist.print();
// eventlist.print();
eventgraph.print();
// export default content;