gpt4 book ai didi

java - 实现二叉树的节点类

转载 作者:太空宇宙 更新时间:2023-11-04 14:01:28 24 4
gpt4 key购买 nike

我必须实现一种二叉树形式,它显示就业层次结构,其中老板是树根,较低的员工是左节点,右节点是同一级别的员工。同级别员工按照员工编号排序。

public class Node {
private Person data;
private Node seniorEmployee;
private Node nextEmployee;
private Node lesserEmployee;

public Node(Member data, Node seniorEmployee) {
this.data = data;
this.seniorEmployee = seniorEmployee;
this.nextEmployee = null;
this.lesserColleague = null;
}

public Member getPerson() {
return this.data;
}

public Node getSeniorEmployee() {
return this.seniorEmployee;
}

public Node getNextEmployee() {
return this.nextEmployee;
}

public Node getLesserEmployee() {
return this.lesserEmployee;
}

public void setEmployee(Person e1) {
if (nextEmployee == null) {
nextEmployee = new Node(e1, this.seniorEmployee);
} else {
if (e1.compareTo(nextEmployee.getPerson()) > 0) {

} else {

}
}

public void setLesserEmployee(Member p1) {
if (lesserEmployee == null) {
lesserEmployee = new Node(e1, this.seniorEmployee);
} else {
if (e1.compareTo(lesserEmployee.getPerson()) > 0) {

} else {

}
}

这是我到目前为止所做的,但我不知道如何实现 set 方法。

最佳答案

我认为对于同级别的员工来说应该没问题:

public void setEmployee(Person e1) {
if (nextEmployee == null) {
nextEmployee = new Node(e1, this.seniorEmployee);
} else {
nextEmployee.setEmployee(e1);
}

}

关于java - 实现二叉树的节点类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29264223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com