Java 比较已实现可比较的类的对象

Java 比较已实现可比较的类的对象,java,compare,Java,Compare,我正在学习二进制搜索树,并试图用Java实现它 public class BinarySearchTree<T> { private class Node { public T data; public Node left; public Node right; } //some code goes here pu

我正在学习二进制搜索树,并试图用Java实现它

    public class BinarySearchTree<T>
    {
        private class Node
        {
            public T data;
            public Node left;
            public Node right;
        }

        //some code goes here

        public void insert(T data)
        {
            //make a new node and add data to that node
            //call to recursive function
        }
        private Node ins(Node root,Node toBeInserted)
        {
            if(root==null) { root = tobeInserted; return root; }

            //problem is here...
            else if(toBeInserted.data<=root.data)// <----How to do this ?????
                root = ins(root.left,toBeInserted);
            else
                root = ins(root.right,toBeInserted);
            return root;
        }
        //some more code
    }
公共类二进制搜索树
{
私有类节点
{
公共数据;
公共节点左;
公共节点权;
}
//这里有一些代码
公共空白插入(T数据)
{
//创建新节点并向该节点添加数据
//对递归函数的调用
}
专用节点插件(节点根、要插入的节点)
{
如果(root==null){root=tobeInserted;返回root;}
//问题就在这里。。。

否则如果(toBeInserted.data如果
T
始终实现
Comparable
,则可以将适当的绑定添加到其定义中:

public class BinarySearchTree<T extends Comparable<T>> { ... }

将类定义为

public class BinarySearchTree<T extends Comparable<T>>
公共类二进制搜索树
然后进行插入

试试这个

  /**
  * This method inserts new node in tree
  * @param value
  */
 public void insert(T value){
  if(isEmpty())
   root = new Node(value);  //root node added
  else
   insert(root, value);  //if not empty then insert based on root
 }

 /**
  * Recursive method is called internally to insert nodes at proper 
    places depending on their vakues.
  * @param node
  * @param value
  */
 private void insert(Node node, T value){
  if(value.compareTo(node.value) < 0){  //if new value less than parent node
   if(node.left == null)  //if left null then add
    node.left = new Node(value);
   else
    insert(node.left,value);  //if left not null then recursive call
  }
  else if(value.compareTo(node.value) >= 0){  //if new value same or greater than parent node
   if(node.right == null)  //if right null then add
    node.right = new Node(value);
   else
    insert(node.right,value);  //if right not null then recursive call
  }
 }
/**
*此方法在树中插入新节点
*@param值
*/
公共空白插入(T值){
if(isEmpty())
root=新节点(值);//添加了根节点
其他的
插入(根,值);//如果不是空的,则基于根插入
}
/**
*在内部调用递归方法以在适当的位置插入节点
取决于他们的信仰的地方。
*@param节点
*@param值
*/
专用空心插入(节点,T值){
if(value.compareTo(node.value)<0{//如果新值小于父节点
if(node.left==null)//如果为null,则添加
node.left=新节点(值);
其他的
insert(node.left,value);//如果left不为null,则递归调用
}
else if(value.compareTo(node.value)>=0){//如果新值与父节点相同或大于父节点
if(node.right==null)//如果right为null,则添加
node.right=新节点(值);
其他的
insert(node.right,value);//如果right不为null,则递归调用
}
}

访问链接获取完整的源代码。

如果T实现了许多接口,并且在像上面这样的其他类中,至少需要来自不同接口的两个函数,那么它是否是某个类?这似乎是不可能的
T扩展接口1和接口2
  /**
  * This method inserts new node in tree
  * @param value
  */
 public void insert(T value){
  if(isEmpty())
   root = new Node(value);  //root node added
  else
   insert(root, value);  //if not empty then insert based on root
 }

 /**
  * Recursive method is called internally to insert nodes at proper 
    places depending on their vakues.
  * @param node
  * @param value
  */
 private void insert(Node node, T value){
  if(value.compareTo(node.value) < 0){  //if new value less than parent node
   if(node.left == null)  //if left null then add
    node.left = new Node(value);
   else
    insert(node.left,value);  //if left not null then recursive call
  }
  else if(value.compareTo(node.value) >= 0){  //if new value same or greater than parent node
   if(node.right == null)  //if right null then add
    node.right = new Node(value);
   else
    insert(node.right,value);  //if right not null then recursive call
  }
 }