BSTree插入错误

时间:2014-04-23 16:00:34

标签: java binary-search-tree

B树插入节点:

import java.util.*;
import java.io.*;

public class BSTree {
    private Node root;
    private int size;

    public BSTree(){root = null; size = 0;}

    public Node root(){return root;}
    public boolean isEmpty(){return root == null;}
    public int size(){return size;}

    public void insert(String s){

        root = new Node (s, root, null, null);
        size++;

    }

    private void insert(String s,Node node){


        if (root.setLeft() < 0 || root.hasLeft() > 0) // Currrently I have error on this line of code
            root = new Node (s, root, node, node);
        else {
            Node cursor = root;
            Node next = cursor.getRight();
            while (next != null && next.compareTo(s) <= 0 ) { // I also having error on the next.compareTo(s).
                cursor = next;
                next = next.getRight();
            }
            cursor.setRight(new Node(s, next, next, next));
        }
        size++;

    }

任何人都可以帮我解决错误吗? 我是B Tree的新手,因此我不确定哪里出了问题。但是我的Node类很好。

谢谢:)

1 个答案:

答案 0 :(得分:0)

  1. 您必须确保root.setLeft()root.hasLeft()已定义且其返回类型为数字,以便您可以将它们与>和{{}等运算符进行比较{1}}。

  2. <失败是因为您尝试将next.compareTo(s)String进行比较,而该方法的合同要求通过Node。< / p>