使用前序遍历在Java中复制二叉树

使用前序遍历在Java中复制二叉树,java,copy,binary-tree,preorder,Java,Copy,Binary Tree,Preorder,我试图使用预顺序遍历复制二叉树,但我被卡住了。 由于我没有将任何值放入新树中,它们显然没有正确复制 public class Node{ int key; String name; Node leftChild; Node rightChild; Node(int key, String name){ this.key = key; this.name = name; } public class BinaryTree{ public Node root; pu

我试图使用预顺序遍历复制二叉树,但我被卡住了。 由于我没有将任何值放入新树中,它们显然没有正确复制

public class Node{

int key;
String name;

Node leftChild;
Node rightChild;

Node(int key, String name){
    this.key = key;
    this.name = name;

}



public class BinaryTree{

public Node root;

public void copyTree(Node focusNode){

    if(focusNode != null){

        Node copyNode = new Node(focusNode.key, focusNode.name);

        //System.out.println(copyNode);

        copyTree(focusNode.leftChild);
        copyTree(focusNode.rightChild);
    }
}

}这里有一个解决方案。为了演示的目的,我在
节点
类中添加了一个
toString()
方法

class Node {

    int key;
    String name;

    Node leftChild;
    Node rightChild;

    Node(int key, String name) {
        this.key = key;
        this.name = name;
    }

    public String toString() {
        return "[" + key + "," + name + "]";
    }
}
BinaryTree
也进行了轻微修改:

class BinaryTree {

    public Node root;

    public BinaryTree copyTree(Node focusNode) {
        BinaryTree bt = new BinaryTree();
        bt.root = preOrderCopy(focusNode);
        return bt;
    }

    public static void preOrderPrint(BinaryTree t) {
        preOrderPrint(t.root);
    }

    public static void preOrderPrint(Node n) {
        if (n == null) {
            // base case
            return;
        }
        System.out.println(n);
        preOrderPrint(n.leftChild);
        preOrderPrint(n.rightChild);

    }

    private Node preOrderCopy(Node focusNode) {
        if (focusNode == null) {
            // base case
            return null;
        }
        Node copy = new Node(focusNode.key, focusNode.name);
        copy.leftChild = preOrderCopy(focusNode.leftChild);
        copy.rightChild = preOrderCopy(focusNode.rightChild);
        return copy;
    }

}
为了测试代码,我根据屏幕上显示的代码创建了一个
BinaryTree
。下面是本例中使用的树的图片:

本例中正确的预顺序遍历是:
F,B,A,D,C,E,G,I,H
。您可以使用以下代码来测试此实现:

public class NodeTest {

    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        Node a = new Node(1, "A");
        Node b = new Node(2, "B");
        Node c = new Node(3, "C");
        Node d = new Node(4, "D");
        Node e = new Node(5, "E");
        Node f = new Node(6, "F");
        Node g = new Node(7, "G");
        Node h = new Node(8, "H");
        Node i = new Node(9, "I");
        f.leftChild = b;
        b.leftChild = a;
        b.rightChild = d;
        d.leftChild = c;
        d.rightChild = e;
        f.rightChild = g;
        g.rightChild = i;
        i.leftChild = h;
        bt.root = f;

        System.out.println("Print full tree:");
        BinaryTree.preOrderPrint(bt.copyTree(f));

        System.out.println("Only print f's left sub-tree:");
        BinaryTree.preOrderPrint(bt.copyTree(f.leftChild));

    }

}
运行上述代码将生成以下输出:

Print full tree:
[6,F]
[2,B]
[1,A]
[4,D]
[3,C]
[5,E]
[7,G]
[9,I]
[8,H]
Only print f's left sub-tree:
[2,B]
[1,A]
[4,D]
[3,C]
[5,E]

从树a复制到树b。你可以像我一样使用两个静态方法。我的想法来自添加元素方法和删除元素方法。它们是相似的

public static Node copyRec(Node a, Node b)//copy from b to a
{
    if(b!=null)
    {
        a=new Node(b.data);
        a.leftChild=copyRec(a.leftChild,b.leftChild);
        a.rightChild=copyRec(a.rightChild,b.rightChild);
        return a;
    }
    return null;
}
public static void copy(BST a, BST b)
{
    a.root=copyRec(a.root,b.root);
}

你所做的对我来说很好。问题到底出在哪里?你的问题是什么?什么东西没有像你期望的那样工作?根据您的
copyTree
方法,看起来您正在创建本地副本(在方法内),但由于副本仅存在于方法内,因此只要
copyTree
方法返回,它就会超出范围(并有资格进行垃圾收集)。也许您想更改
copyTree
的签名以返回
copyNode
(而不是当前的
void
返回值),以便返回您正在构建的副本?感谢您的回复,我实际上并没有将它们复制到新的“副本”中树,这就是我想要的,但你的回答仍然为我澄清了一些事情。。