二叉搜索树克隆-Java

二叉搜索树克隆-Java,java,recursion,clone,binary-search-tree,Java,Recursion,Clone,Binary Search Tree,这是一个家庭作业问题。我们需要用java建立一个方法,通过递归克隆给定的二叉搜索树,我在网上查阅了几个例子,问题是我们的讲师让我们编写的程序是用他称之为现代方法的方法,而不是在每个方法中检查空值,使用接口节点的动态调度构造树,连接到两个子类nil(表示空节点和处理空节点实例的必要方法)和Vertex(填充节点及其附属方法)。我不知道如何构造递归来克隆节点,如何构造节点来保存克隆的信息。这是我的家庭作业,我显然不是在寻找答案,但我真的需要一些帮助 interface Node<T exte

这是一个家庭作业问题。我们需要用java建立一个方法,通过递归克隆给定的二叉搜索树,我在网上查阅了几个例子,问题是我们的讲师让我们编写的程序是用他称之为现代方法的方法,而不是在每个方法中检查空值,使用接口节点的动态调度构造树,连接到两个子类nil(表示空节点和处理空节点实例的必要方法)和Vertex(填充节点及其附属方法)。我不知道如何构造递归来克隆节点,如何构造节点来保存克隆的信息。这是我的家庭作业,我显然不是在寻找答案,但我真的需要一些帮助

 interface Node<T extends Comparable<T>>
{
    public int size();//return the number of values in the tree
    public boolean empty();//true if tree is empty nil
    public Node<T> insert(T x);// insert something into a binary search tree, return the node it was inserted into
    public vertex<T> search(T x);//search for a given value and return the vertex ( filled node ) it exists in
    public int depth();//returns the greatest depth of the tree
    public void inorder();
    //public Node<T> Attack_of_the_clones();
}
//note that insert must be used as in t = t.insert(x)


class nil<T extends Comparable<T>> implements Node<T> //empty tree
{
    public int size() {return 0;}// empty node, therefore of size zero
    public boolean empty() { return true; }//its and empty node, duh
    public Node<T> insert(T x) { return new vertex<T>(x); }// returns a Tpe Node for inserting a given value into a node (thereby creating a 
                                                              //vertex containing said inserted value)
    public vertex<T> search (T x) { return null; }//RETURNS NULL IN SEARCHING FOR A GIVE VALUE BECAUSE NODES OF TPE nIL ARE INHERENTLY empty
    public int depth() { return 0; }
    public void inorder() { System.out.print("0");}
    //public Node<T> Attack_of_the_clones() { return new nil<T>(this); }
}//end nil

class vertex<T extends Comparable<T>> implements Node<T>
{
    protected T head;// the root of the tree
    protected Node<T> left;//creates an instance of Node to serve as the left child of head
    protected Node<T> right;

    //constructor
    public vertex(T h, Node<T> l, Node<T> r) { head = h; left = l; right = r; }// a constructed instance
    //leaf instructor
    public vertex(T h) { head = h; left = new nil<T>(); right = new nil<T>(); }//a constructed leaf
    // a leaf is Tpically a node with no or null children, some consider the null nodes themselves to be leaves

    //accesors so that the protected variables can be displayed
    public T acHead() {return head;}
    public Node<T> acLeft() {return left;}
    public Node<T> acRight() {return right;}

    public int size()
    {
        return left.size() + right.size() + 1;//recursively call size down the left and right trees to get all the nodes,
                                              // and combine them ( +1 for the root) to get the size of the tree
    }

    public int depth()
    {
        return Math.max((left.depth()+1),(right.depth()+1));
    }

    public boolean empty() {return false; }//because its of class vertex and therefore not empty

    public Node<T> insert(T x)
    {
        if (x.compareTo(head) <= 0)// go down left tree
            left = left.insert(x);
        else right = right.insert(x);// go right
        return this;//root vertex has not changed
    }//end insert

    public vertex<T> search(T x)
    {
        int r = x.compareTo(head);
        if(r==0)//go left
        {
            return left.search(x);//recursively call search using said node to move down tree
        }
        else //go right
        {
            return right.search(x);
        }
    }// end binary search
    public void inorder()
    {
        Node<T> current_root = this;
        if(current_root == null)
            return;
        left.inorder();
        System.out.println(current_root + ", ");
        right.inorder();
    }//end inorder print

    /*public Node<T> Attack_of_the_clones()
    {

        left_copy = curr_node.left.copy();          
        right_copy = curr_node.right.copy();
        return new vertex(curr_node, left1, right1);
    }*/

    public vertex<T> largest(Node<T> x)
    {

        int left1 = largest(x.left);
        int right1 = right.largest();
        if(this.head > left1 && this.head > right1)
            return this.root;
        else
            return Math.max(left1,right1);
    }

}// end vertex

public class BinaryTree
{

    public static void main(String[] args)
    {
    Node<Integer> n = new vertex<Integer>(3);
    n = n.insert(4);
    for(int i = 0; i < 10; i++)
        {
        n.insert((int)Math.random*8);
        }
    n.size();
    n.depth();
    n.inorder();
    }//end main
}//end Binary Tree
接口节点
{
public int size();//返回树中的值数
public boolean empty();//如果树为空,则为true
public Node insert(T x);//在二叉搜索树中插入内容,返回插入内容的节点
公共顶点搜索(T x);//搜索给定值并返回其所在的顶点(填充节点)
public int depth();//返回树的最大深度
公共无效顺序();
//_克隆()的公共节点攻击_;
}
//请注意,插入必须像在t=t中一样使用。插入(x)
类nil实现节点//空树
{
public int size(){return 0;}//空节点,因此大小为零
public boolean empty(){return true;}//its和空节点,duh
公共节点插入(tx){return new vertex(x);}//返回一个Tpe节点,用于将给定值插入到节点中(从而创建
//包含所述插入值的顶点)
公共顶点搜索(Tx){return null;}//在搜索给定值时返回null,因为TPE nIL的节点本质上是空的
public int depth(){return 0;}
public void inoorder(){System.out.print(“0”);}
//_克隆()的公共节点攻击_{return new nil(this);}
}//零结束
类顶点实现节点
{
受保护的T头;//树的根
受保护的节点left;//创建一个节点实例作为head的左子节点
受保护的节点权限;
//建造师
公共顶点(th,节点l,节点r){head=h;left=l;right=r;}//构造的实例
//叶子指导员
公共顶点(th){head=h;left=new nil();right=new nil();}//构造的叶
//叶子是一个没有或没有子节点的节点,有些人认为零节点本身是叶子。
//以显示受保护的变量
public T acHead(){return head;}
公共节点acLeft(){return left;}
公共节点acRight(){return right;}
公共整数大小()
{
返回left.size()+right.size()+1;//递归调用左树和右树的size以获取所有节点,
//然后将它们组合起来(+1表示根),得到树的大小
}
公共整数深度()
{
返回Math.max((left.depth()+1),(right.depth()+1));
}
public boolean empty(){return false;}//因为它的类是vertex,因此不是空的
公共节点插入(T x)
{
如果(x.compareTo(head)left1&&this.head>right1)
返回这个.root;
其他的
返回Math.max(left1,right1);
}
}//端点
公共类二叉树
{
公共静态void main(字符串[]args)
{
节点n=新顶点(3);
n=n.插入(4);
对于(int i=0;i<10;i++)
{
n、 插入((int)数学随机*8);
}
n、 大小();
n、 深度();
n、 顺序();
}//端干管
}//结束二叉树
BinaryTree.java:87:警告:[rawtypes]找到原始类型:vertex
公共节点攻击_克隆的_(){返回新顶点(头部,左侧。攻击_克隆的_(),右侧。攻击_克隆的_());}
^
缺少泛型类vertex的类型参数
其中T是一个类型变量:
T扩展了类vertex中声明的可比性
java:87:警告:[未选中]未选中调用vertex(T,Node,Node)作为原始类型vertex的成员
公共节点攻击_克隆的_(){返回新顶点(头部,左侧。攻击_克隆的_(),右侧。攻击_克隆的_());}
^
其中T是一个类型变量:
T扩展了类vertex中声明的可比性
java:87:警告:[未选中]未选中的转换
公共节点攻击_克隆的_(){返回新顶点(头部,左侧。攻击_克隆的_(),右侧。攻击_克隆的_());}
^
必需:节点
发现:顶点
其中T是一个类型变量:
T扩展了类vertex中声明的可比性

3警告

由于
Nil
没有字段(是不可变的),那么它的克隆就可以返回它自己

//On Nil
public Node<T> myClone(){
   return this;
}

我们需要更多的代码&更少的冗长等待,为什么我会因此得到负面评价?我想这是一个有道理的问题。哇。不要让别人随意点击鼠标困扰你
Nil.clone()
Vertex.clone()
返回
新顶点(头部,左.clone(),右.clone())
。这样行吗?你似乎已经把这段代码注释掉了,它不起作用吗?我曾经有过类似的东西,但我认为我想得太多了,出现了一些奇怪的错误。实际上,我尝试了你建议的方法,得到了以下错误,以前从未见过<代码>C:\Users\Jarngrimr\Desktop\Java>javac BinaryTree.Java注意:BinaryTree.Java使用未经检查或不安全的操作。注意:使用-Xlint重新编译:未选中以获取详细信息。
//On Vertex
public Node<T> myClone(){
    return new Vertex<T>(head,left.myClone(),right.myClone())
}