【发布时间】:2011-07-19 08:30:19
【问题描述】:
我正在解决一个问题,该问题需要我递归地复制二叉搜索树并返回该树。我在二叉搜索树类中编码,所以它会复制它所调用的任何二叉搜索树。要求说私有方法必须具有Entry<E> 的返回类型和Entry<E> 类型的参数。我遇到的问题是将多个条目添加到树中。
这是我目前拥有的:
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;
}
这里是 Entry 类,所以你知道我有什么可用的:
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 - 回复:您建议的编辑:您可以发布自己问题的答案,而不是编辑其他人的答案。
标签: java recursion copy binary-tree