Designing a course prerequisite structure using tree data model

Hey everyone! I’m working on a project to learn MERN stack. I’m trying to build a website that shows courses and their prerequisites. The tricky part is that some courses have more than one prerequisite.

I’ve been trying to use a tree structure to represent this, but I’m stuck. Here are the main issues I’m facing:

  1. Can’t figure out how to have multiple parents for a single node
  2. Having trouble traversing the tree to find available courses

I’ve tried using JavaScript, but I’m open to other languages too. Here’s a basic tree structure I started with:

class CourseTree {
  constructor(rootCourse) {
    this.root = rootCourse || null;
  }

  addCourse(courseName, prereqCourse) {
    // Add new course logic here
  }

  findAvailableCourses() {
    // Traverse tree to find courses with completed prerequisites
  }
}

class Course {
  constructor(name) {
    this.name = name;
    this.prerequisites = [];
  }
}

Any ideas on how to improve this or suggestions for a better approach? I’d really appreciate some guidance!

Hey there Liam39! :slightly_smiling_face:

Your project sounds super interesting! Have you thought about using a graph database like Neo4j for this? It could be a cool way to handle those tricky multiple prerequisites.

I’m curious - what made you choose a tree structure initially? Did you consider any alternatives before diving in?

Also, I’m wondering how you’re planning to handle course completion tracking. Are you thinking of integrating user profiles to keep tabs on what courses they’ve finished?

Keep us posted on how it goes! I’d love to hear more about your progress as you build this out. Learning MERN stack is awesome, by the way. What’s been your favorite part of working with it so far?

yo, that DAG thing sounds cool but kinda complicated. what about using a simple object to store courses and their prereqs? like:

let courses = {
  'math101': ['algebra'],
  'physics201': ['math101', 'science101']
}

then u can easily check prereqs and add new ones. just a thought!

I’ve tackled a similar challenge in my work on educational software. Instead of a traditional tree, I’d recommend using a directed acyclic graph (DAG) for your course prerequisite structure. This allows for multiple prerequisites per course.

Here’s a basic implementation in JavaScript:

class Course {
  constructor(name) {
    this.name = name;
    this.prerequisites = new Set();
  }

  addPrerequisite(course) {
    this.prerequisites.add(course);
  }
}

class CourseGraph {
  constructor() {
    this.courses = new Map();
  }

  addCourse(name) {
    if (!this.courses.has(name)) {
      this.courses.set(name, new Course(name));
    }
    return this.courses.get(name);
  }

  findAvailableCourses(completedCourses) {
    return Array.from(this.courses.values()).filter(course => 
      Array.from(course.prerequisites).every(prereq => 
        completedCourses.includes(prereq.name)
      )
    );
  }
}

This approach should solve your multiple prerequisites issue and make traversal easier. Hope this helps!