2013-01-07 51 views
-2

所以我有一個安裝程序從一個文件中讀取國家記錄,我的目標是根據一個國家(例如美國)的3個字母表示進行索引。我需要使用6個並行陣列。 LeftChPtr,代碼的3個字符數組,DataRecPtr和RightChPtr。二進制搜索樹插入問題(Java)

下面是它在DataRecPtr中發送的方法InsertCode和數組中的3個字符。

插入所有的數據後,它被保存到一個文件,當我看着文件,我可以告訴它是錯誤的,因爲指針是不正確的。

請幫助,我絕不會要求你爲我寫代碼我只是沒有看到問題。 感謝

N = 0 
rootPtr = -1 
parentI = 0; 


    public void InsertCode(short ID, char[] cc) 
      { 
       drp = ID; 
       short i; 
       codeArray1[N] = cc[0]; 
       codeArray2[N] = cc[1]; 
       codeArray3[N] = cc[2]; 
       leftChPtr[N] = -1; 
       rightChPtr[N] = -1; 
       dataRecPtr[N] = drp; 

       if (rootPtr == -1) //special case - no nodes in BST yet 
        rootPtr = N; 
       else //normal case 
       { 
        i = rootPtr; 
        String dataN = ""; 
        dataN += codeArray1[N] + codeArray2[N] + codeArray1[N]; 

        while (i != -1) 
        { parentI = i; 
         String dataI = ""; 
         dataI += codeArray1[i] + codeArray2[i] + codeArray3[i]; 

         if (dataN.compareTo(dataI) < 0) 
         { 
          i = leftChPtr[i]; 
          LorR = true;//L 
         } 
         else 
         { 
          i = rightChPtr[i]; 
          LorR = false;//R 
         } 
        }     
        //i++;????????????????? 

       if (LorR == true) 
        leftChPtr[parentI] = N; 
       else 
        rightChPtr[parentI] = N; 
       } 
       N++; 
      } 

回答

0

看不出什麼毛病insert方法除了LORR應該聲明。也許你應該檢查保存到文件中的代碼。