2014-04-23 58 views
0

BSTree插入節點:誤差BSTree插入

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++; 

    } 

誰能幫我解決這個錯誤嗎? 我是BSTree的新手,因此我不確定哪裏出了問題。但是我的Node類沒有問題。

謝謝:)

+0

什麼是實際錯誤?另外,爲什麼你的Node構造函數將其實例化爲一個參數? – Mike

+0

「Node」類定義在哪裏? –

回答

0
  1. 你必須確保root.setLeft()root.hasLeft()的定義及其返回類型爲數值,這樣你就可以將其與像><運營商進行比較。

  2. next.compareTo(s)因爲你想與Node比較String,而該方法的合同要求通過String失敗。