2016-08-12 42 views
-3

我想創建一個二叉樹,但對我而言,我甚至無法在樹中插入一個整數值。問題是我創建了一個類datatype,其中插入了三個屬性。它們是:static datatype left,static datatype right,static int value引用類型或類類型變量不能接受給定值

我創建了一個類作爲一個用戶定義的數據類型:

public class datatype 
{ 
static datatype left,right; 
static int value=0; 
} 

我二叉樹類是:

datatype root,parent,node; 

public int insert(int data) 
{  
    node.value=data; //using debugger found, node.value remain null even 
    node.left=null; // after insertion of data into it.....That's my 
    node.right=null; // problem  
    try{ 
     if(root==null) 
      root=node;       
     else   
     { 
      parent=root; 
      insert(node);    
     } 
     parent=root; 
     return 1; 
    } 
    catch(Exception e) 
    { 
     return 0; 
    }  
} 

private void insert(datatype node) 
{  
    if(node.value<=parent.value) 
    { 
     if(parent.left==null) 
     { 
      parent.left=node; 
      return; 
     } 
     else 
     { 
      parent=parent.left; 
      insert(parent); 
     } 
    } 
    else 
    { 
     if(parent.right==null) 
     { 
      parent.right=node; 
      return; 
     } 
     else 
     { 
      parent=parent.right; 
      insert(parent); 
     } 
    } 
} 
+0

爲什麼'left'和''right' static':

另外,不要對你會如何以及爲什麼使用靜態字段讀了?請寫'Datatype'而不是'datatype'! – luk2302

回答

相關問題