2015-10-29 46 views
1

所以我真的可以得到這個我想知道是否有人可以幫助我,這個程序的目標是使用訪客模式來生成一個字符串列表從一個給定的人的名字二叉樹,如果不確定如何去使用附加函數來做到這一點。我如何將這個人追加到父母身上?Java二叉樹/訪問者模式

import tester.Tester; 

//Representation for an ancestor tree 
interface IAT { 
    <R> R accept(IATVisitor<R> visitor); 

    //Append two lists 
    IList<String> append(IList<String> l); 
} 
//------------------------------------------------------------------------------------------------- 
//Unknown person 
class Unknown implements IAT { 
    Unknown() { 
    } 
    public <R> R accept(IATVisitor<R> visitor) { 
     return visitor.visitUnknown(this); 
    } 
    //append two an unknown 
    public IList<String> append(IList<String> l) { 
     return l; 
    } 

}  
//------------------------------------------------------------------------------------------------- 
//Representation for a person 
class Person implements IAT { 
    String name; 
    int yob; 
    boolean isMale; 
    IAT mom; 
    IAT dad; 
    //Constructor 
    Person(String name, int yob, boolean isMale, IAT mom, IAT dad) { 
     this.name = name; 
     this.yob = yob; 
     this.isMale = isMale; 
     this.mom = mom; 
     this.dad = dad; 
    } 
    public <R> R accept(IATVisitor<R> visitor) { 
     return visitor.visitPerson(this); 
    } 
    //append parent and children of tree 
    public IList<String> append(IList<String> l) { 
     // 
    } 

} 
//------------------------------------------------------------------------------------------------- 
interface IATVisitor<R> { 
    R visitUnknown(Unknown u); 
    R visitPerson(Person p); 
} 
//------------------------------------------------------------------------------------------------- 
//IAT Visitor that returns a list of the names of all people 
class IATVisitGetNames implements IATVisitor<IList<String>> { 
    public IList<String> visitUnknown(Unknown u) { 
     return new MT<String>(); 
    } 
    public IList<String> visitPerson(Person p) { 
     return new Cons<String>(p.name, new MT<String>()); 
    } 
} 

//Examples 
class ExamplesIATV { 
    //persons 
    Unknown a = new Unknown(); 
    Person ralph = new Person("Ralph", 1995, true, a, a); 
    Person kevin = new Person("Kevin", 1994, true, a , a); 
    Person julia = new Person("Julia", 1991, false, ralph, a); 
    Person lily = new Person("Lily", 1990, false, kevin, julia); 
    Person who = new Person("?", 1738, false, lily, a); 

    //Visitor 
    IATVisitor<IList<String>> byName = new IATVisitGetNames(); 


    //test Vistior 
    boolean testGetNames(Tester t) { 
     return 
       t.checkExpect(who.accept(byName), new MT<String>()); 
    } 
} 
+0

我你的意思是「將這個人與其父母相提並論」。在你的例子中給出測試數據,你想要輸出什麼,比如說'who.append(MT ())'是什麼? – sprinter

回答

1

首先 - 你想從樹上收集所有名字。您需要遍歷功能,例如:

public void traverse(Node root) { 
    //do somesing with node 
    System.out.println(root.value); 
    if (root.left != null) { 
     traverse(root.left); 
    } 
    if (root.right != null) { 
     traverse(root.right); 
    } 
} 

第二個 - 您想要使用訪問者模式。維基百科如何說:

訪問者 - 是一種將算法與其操作的對象結構 分離的方法。

因此訪問者不適合travers/itereate邏輯。隨着遊客我們只能incapsulate節點某種邏輯:

public void traverseWithVisitor(Node root, IVisitor v) { 
    root.accept(v); 
    if (root.left != null) { 
    traverseWithVisitor(root.left, v); 
    } 
    if (root.right != null) { 
    traverseWithVisitor(root.right, v); 
    } 
} 

現在incapsulate我們收集名稱邏輯遊客:

class AggregateNamesVisitor implements IVisitor { 

    public List<Integer> names = new ArrayList<>(); 

    @Override 
    public void visit(Node node) { 
     names.add(node.value); 
    } 
} 

我們可以用這樣的:

AggregateNamesVisitor aggregateVisitor = new AggregateNamesVisitor(); 
traverseWithVisitor(root, aggregateVisitor); 
aggregateVisitor.names.forEach(name -> System.out.print(" " + name));