2016-08-12 69 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

回答