I’m working on a personal project to build a website for managing courses and their prerequisites. I’m using this to learn MERN stack development.
I’ve created a tree-like structure to show how courses depend on each other. The tricky part is that some courses have multiple prerequisites. I’m struggling to turn this into a working data structure.
My main issues are:
Can’t figure out how to have multiple parent nodes
Can’t traverse the tree to find available courses
I tried using a Tree/Node setup in JavaScript, but I’m open to other languages. Here’s a simplified version of what I’ve tried:
class CourseTree {
constructor(rootCourse) {
this.root = rootCourse || null;
}
addCourse(newCourse, parentCourse) {
// Add new course logic here
}
findAvailableCourses() {
// Logic to find courses with completed prerequisites
}
}
const curriculum = new CourseTree();
curriculum.addCourse('MATH101');
curriculum.addCourse('PHYS201', 'MATH101');
Any suggestions on how to approach this? Maybe there’s a better way to structure this data? Thanks for any help!
hey jack, have u tried using a directed acyclic graph (DAG) instead? it’s perfect for prereqs. each course is a node, edges are dependencies. u can use adjacency list to represent it. for finding available courses, try topological sorting algorithm. it’ll give u courses in order. good luck with ur project!
Hey Jack27! Your project sounds super interesting! I’m actually working on something similar for my university’s CS department.
Have you considered using a graph structure instead of a tree? Graphs are perfect for representing complex relationships like course prerequisites. Each course could be a node, and the edges could represent the prerequisites.
I’d recommend using an adjacency list to represent your course structure. It’s efficient for storing relationships and allows for multiple prerequisites easily. Here’s a basic implementation:
This structure allows for multiple prerequisites and efficient traversal. You can easily integrate this with MongoDB by storing course objects with an array of prerequisite IDs.