Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 树、节点、树的类型_Java_Binary Tree - Fatal编程技术网

Java 树、节点、树的类型

Java 树、节点、树的类型,java,binary-tree,Java,Binary Tree,我想创建一个树来检测insert是否是字符类型的对象,它将比较每个字符并决定在何处插入[右或左],(我知道它可以通过ascii表中的位置进行检测),如果insert是int类型的对象,它将执行相同的操作。 我的问题是: 1.我需要创建一棵树,同时设置一个比较器(例如,如果它是一棵字符树,那么它将是一个检查字符的字符比较器,并实现(java的)比较器)。? 2.我的代码现在只适用于int。因为我将对象转换为string,然后再转换为int,在所有这些之后,我比较并决定在何处插入,这就是我需要做的


我想创建一个树来检测insert是否是字符类型的对象,它将比较每个字符并决定在何处插入[右或左],(我知道它可以通过ascii表中的位置进行检测),如果insert是int类型的对象,它将执行相同的操作。
我的问题是:
1.我需要创建一棵树,同时设置一个比较器(例如,如果它是一棵字符树,那么它将是一个检查字符的字符比较器,并实现(java的)比较器)。? 2.我的代码现在只适用于int。因为我将对象转换为string,然后再转换为int,在所有这些之后,我比较并决定在何处插入,这就是我需要做的事情?或者有另一种方法可以处理所有类型的对象? 下面是我的代码以及如何创建树,

树类

public class tree {

bNode root;
public tree() {
    this.root = null;
}
public boolean isEmpty(){
    return root==null;
}
public void insert(Object data)
{
    if(isEmpty())
        this.root = new bNode(data);
    else
        this.root.insert(data);

   }
 }
public class bNode {
 protected Object data;
 protected bNode left;
 protected bNode right;


public bNode(Object data) {
    this.data = data;
    this.left = null;
    this.right = null;
}

public void insert(Object data){

    if(Integer.parseInt(data.toString())<Integer.parseInt(this.data.toString())){
        if(this.left==null)
             this.left = new bNode(data);
        else 
            this.left.insert(data);

    }
    else{
        if(this.right==null)
             this.right = new bNode(data);
        else 
            this.right.insert(data);



    }
}
public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    tree x = new tree();
    char a = 'G';
    x.insert(a);
    x.insert(60);
    x.insert(40);
    x.insert(30);
    x.insert(59);
    x.insert(61);
    x.root.printTree(x.root);


}

b节点类

public class tree {

bNode root;
public tree() {
    this.root = null;
}
public boolean isEmpty(){
    return root==null;
}
public void insert(Object data)
{
    if(isEmpty())
        this.root = new bNode(data);
    else
        this.root.insert(data);

   }
 }
public class bNode {
 protected Object data;
 protected bNode left;
 protected bNode right;


public bNode(Object data) {
    this.data = data;
    this.left = null;
    this.right = null;
}

public void insert(Object data){

    if(Integer.parseInt(data.toString())<Integer.parseInt(this.data.toString())){
        if(this.left==null)
             this.left = new bNode(data);
        else 
            this.left.insert(data);

    }
    else{
        if(this.right==null)
             this.right = new bNode(data);
        else 
            this.right.insert(data);



    }
}
public class Main {

/**
 * @param args
 */
public static void main(String[] args) {
    tree x = new tree();
    char a = 'G';
    x.insert(a);
    x.insert(60);
    x.insert(40);
    x.insert(30);
    x.insert(59);
    x.insert(61);
    x.root.printTree(x.root);


}
}

谢谢!

您可以在
insert()
中传递一个
Comparable
,而不是传递一个对象。 整数、字符串等标准类型已经实现了可解析接口。


如果我创建树,例如BinaryTree x=new BinaryTree(comp)和comp is Comparator comp=new IntegerComparator,那么现在我确定树将是一个int树,因此所有检查都将是int,那么我不需要问所有问题,(instanceof…),我的意思是,我真正想要的是创建一个实现comparator的函数,每个函数检查另一件事,就像instanceof,例如,如果我创建了一个IntegerComparator,我知道每个对象都会通过IntegerComparator的compareTo。我认为这样做更有效。不是吗?不,只是新的BinaryTree();否则,您必须阅读更多有关泛型的内容。但是insert(Comparable comp);使用Comparable如果我想将对象存储为字符,则按原样插入?字符是字符的java对象表示形式