我正在處理一個問題,它需要我遞歸複製二叉搜索樹並返回樹。我在二叉搜索樹類中編碼,所以它會複製它所調用的任何二叉搜索樹。要求說私有方法必須具有返回類型Entry<E>
和類型Entry<E>
的參數。我遇到的問題是將多個條目添加到樹中。Java二叉搜索樹遞歸複製樹
這是我目前有:
public BinarySearchTree<E> rcopy(){
BinarySearchTree newTree = new BinarySearchTree();
newTree.add(rcopy(root).element);
return newTree;
}
private Entry <E> rcopy(Entry <E> current){
if(current.left!=null) return rcopy(current.left);
if(current.right!=null) return rcopy(current.right);
return current;
}
這裏是入門級的,所以你知道我有什麼可以對我說:
protected static class Entry<E> {
protected E element;
protected Entry<E> left = null,
right = null,
parent;
protected int pos;
protected Entry<E> link = null;
public Entry() { }
public Entry (E element, Entry<E> parent)
{
this.element = element;
this.parent = parent;
}
}
n00b - re:你提出的編輯:你可以發佈自己問題的答案,而不是編輯別人的答案。 – 2011-03-22 16:49:13