You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在BST中插入学生数据对象?求策略模式实现参考资源

实现带策略模式的学生数据二叉搜索树(BST)插入方案

核心思路

策略模式的核心是把比较逻辑从BST中抽离,让BST可以动态切换不同的排序规则(比如按学号、GPA、姓名排序)来插入学生对象。这样BST的核心结构不用修改,只需要替换比较策略就能适配不同的插入需求。


步骤1:定义学生数据类

先封装学生的核心数据,提供必要的访问方法:

public class Student {
    private String firstName;
    private String lastName;
    private String studentId;
    private double gpa;

    public Student(String firstName, String lastName, String studentId, double gpa) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.studentId = studentId;
        this.gpa = gpa;
    }

    // Getter方法,供策略类访问字段
    public String getFirstName() { return firstName; }
    public String getLastName() { return lastName; }
    public String getStudentId() { return studentId; }
    public double getGpa() { return gpa; }
}

步骤2:定义比较策略接口

创建一个统一的策略接口,所有具体的比较规则都要实现这个接口:

public interface StudentComparisonStrategy {
    // 返回值规则:<0则a排在b前面,>0则b排在a前面,=0则相等
    int compare(Student a, Student b);
}

步骤3:实现具体的比较策略

根据不同的排序需求,实现对应的策略类:

  • 按学号排序的策略:
public class StudentIdStrategy implements StudentComparisonStrategy {
    @Override
    public int compare(Student a, Student b) {
        return a.getStudentId().compareTo(b.getStudentId());
    }
}
  • 按GPA降序排序的策略:
public class GpaDescStrategy implements StudentComparisonStrategy {
    @Override
    public int compare(Student a, Student b) {
        // 降序:b的GPA减a的GPA,返回值反转
        return Double.compare(b.getGpa(), a.getGpa());
    }
}
  • 按姓氏+名字排序的策略:
public class FullNameStrategy implements StudentComparisonStrategy {
    @Override
    public int compare(Student a, Student b) {
        int lastNameCompare = a.getLastName().compareTo(b.getLastName());
        if (lastNameCompare != 0) {
            return lastNameCompare;
        }
        // 姓氏相同则比较名字
        return a.getFirstName().compareTo(b.getFirstName());
    }
}

步骤4:改造BST以支持策略模式

让BST持有一个比较策略实例,插入时使用该策略判断节点位置:

public class StudentBST {
    private class Node {
        Student data;
        Node left;
        Node right;

        Node(Student data) {
            this.data = data;
            left = right = null;
        }
    }

    private Node root;
    private StudentComparisonStrategy strategy;

    // 构造时传入指定的比较策略
    public StudentBST(StudentComparisonStrategy strategy) {
        this.root = null;
        this.strategy = strategy;
    }

    // 对外暴露的插入方法
    public void insert(Student student) {
        root = insertRecursive(root, student);
    }

    // 递归实现插入逻辑
    private Node insertRecursive(Node current, Student student) {
        if (current == null) {
            return new Node(student);
        }

        // 使用策略比较当前节点和待插入学生
        int comparison = strategy.compare(student, current.data);
        if (comparison < 0) {
            current.left = insertRecursive(current.left, student);
        } else if (comparison > 0) {
            current.right = insertRecursive(current.right, student);
        } else {
            // 相等时可选择忽略或更新,这里默认忽略重复
            return current;
        }
        return current;
    }
}

步骤5:使用示例

创建不同策略的BST并插入学生:

public class Main {
    public static void main(String[] args) {
        // 创建按学号排序的BST
        StudentBST idSortedBST = new StudentBST(new StudentIdStrategy());
        idSortedBST.insert(new Student("Alice", "Smith", "S1001", 3.8));
        idSortedBST.insert(new Student("Bob", "Jones", "S1003", 3.5));
        idSortedBST.insert(new Student("Charlie", "Brown", "S1002", 3.9));

        // 切换为按GPA降序的BST
        StudentBST gpaSortedBST = new StudentBST(new GpaDescStrategy());
        gpaSortedBST.insert(new Student("Alice", "Smith", "S1001", 3.8));
        gpaSortedBST.insert(new Student("Bob", "Jones", "S1003", 3.5));
        gpaSortedBST.insert(new Student("Charlie", "Brown", "S1002", 3.9));
    }
}

内容的提问来源于stack exchange,提问作者Pooja

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 15:15:47