java에서 "first child next sibling"트리에 대한 클래스를 구현했습니다.첫 번째 자식 다음 형제 트리 복사
는 다음과 같은 트리를 나타내는 링크입니다
나는 다음과 같은 기능을 구현 한 http://www.cs.utexas.edu/~novak/cs315116.html
:
addChild();
getLabel();
setLabel(T v);
getParent();
getNextSibling();
getFirstChild();
내하는 AddChild 기능은 다음과 같은 순서로 아이들을 추가합니다.
public void addChild(Tree<T> c) {
c.parent = this;
if (firstChild == null)
firstChild = c;
else {
c.nextSibling = firstChild;
firstChild = c;
}
}
That is, if we have a tree node 1 and we add tree node 2 and then tree node 3 to it then the final tree would be,
1.addChild(2);
1.addChild(3);
1 1
/\ which is internally stored as /
3 2 3 - 2
The most recent child added would be the first child
이러한 트리를 인수로 제공하면 트리 복사본을 만들어 반환하는 CopyTree 기능을 구현하고 싶습니다. 몇 가지 초기 코드가 있지만 올바른 재귀를 얻을 수 없습니다.
private Tree<String> CopyTree(Tree<String> tr){
if (tr == null)
return null;
Tree<String> t = new Tree<String>();
t.setLabel(tr.getLabel());
if (tr.getFirstChild() != null) {
t.addChild(CopyTree(tr.getFirstChild()));
}
Tree<String> temp = tr.left();
if (temp != null) {
while (temp.getNextSibling() != null) {
t.addChild(CopyTree(temp.getNextSibling()));
temp = temp.getNextSibling();
}
}
return t;
}
재귀 작업을 수행하기 위해해야 할 일은 무엇입니까 ?? 사전에 모든