我有 3 种递归实现的方法。是的,这是给学校的,所以请不要简单明了的答案,我会很感激描述性的答案,这样我就可以学习了!我是树结构的新手。
3种方法如下...
public class Zombies{
public static int countPeople(Person p){...}
// counts all the people in the tree structure
// starting with Person p.
public static int countZombies(Person p){...}
// counts all the people in the tree structure
// starting with Person p that are zombies
public static void draw(Person p){...}
// draws a diagram of the people in tree structure
// starting with Person p.
// each person will be denoted by a P and
// person that is a zombie will be denoted by a Z
//
// The diagram should illustrate the family tree
// structure. Each person will be drawn with 3 minus
// signs '-' for each level below p.
我已经开始了我的 Person 课程,我有几个问题。
1)我的个人课程是否在正确的轨道上
2)方法描述中提到的树结构是二叉树吗?
3)能够实现这些方法我缺少什么(如果有的话,或者这个树结构是否需要构建块)
下面是我的 Person 类。
public class Person{
public int id; // some identification number unique to the person
public boolean zombie; // true if the person is a zombie
public char state; // p means human, z means zombie
public ArrayList<Person> friends; // list of friends
public Person(int id, char state, boolean zombie){
this.id = id;
this.state = state;
this.zombie = zombie;
}
public boolean isZombie() {
if (state == 'p'){
return zombie=false;
}
else if (state == 'z'){
return zombie=true;
}
return zombie;
}
}
树类型的示例输出如下..
P (this is Person q)
---P (this is a friend of q, say q1)
------P (this is a friend of q1)
------Z (this is another friend of q1, who is a zombie)
---Z (this is a friend of q, say q2, who is a zombie)
------Z (this is a friend of q1, who is also a zombie)
------P (this is a friend of q1, who is not a zombie)
提前感谢您的耐心和帮助/输入!