2012-03-05 47 views
-1

我在为我的树连接节点时遇到问题。在我的程序中,函数read()将先执行,然后执行load()函数。在read()函数中,我的外部文件中的所有数据将被存储在数组中,然后在load()函数中重新生成。全局声明变量dati。我的问题在于load()函数。你的帮助是非常赞赏的。顺便说一下,我的外部文件数据是按顺序排列的,我使用“#”作为NULL节点。文件重建树

void read() 
{ 
    string dat1; 
    fstream file; 
    file.open("data.txt", ios::in); 
    do 
    { 
     getline(file,dat1); 
     dat[i]=dat1; 
     i++; 
    }while(!file.eof()); 
    file.close(); 
} 

void load(node *root,int index) 
{ 

    node *nNode; 
    nNode=(node*)malloc(sizeof(node)); 
    nNode->yes=NULL; 
    nNode->no=NULL; 

    nNode->data=dat[index]; 

    if(index<i) 
    { 
     if(nNode->data!="#") 
     { 
      root=nNode; 
      load(root->yes,index+1); 
      load(root->no,index+1); 
     } 
     else 
     { 
      root=NULL; 
      return; 
     } 
    } 
} 
+0

你是什么意思; “我的问题是在加载函数”是否被窃听?如果是这样的话;它编译?如果不是有什么错误。 – 111111 2012-03-05 16:09:57

+0

它编译但节点没有连接。抱歉我的问题。 – user1242812 2012-03-05 16:12:04

回答

1

那么,一个问题是,你从来没有真正在load()增加index。另一个问题是,你永远不会为任何东西分配root-> yes或root-> no。相反,您可以指定传递值参数root。也许你希望这也是参考。

你可能想要更多的东西一样:

// note that root and index are now pass-by-reference 
void load(node &*root,int &index) 
{ 
    ... 
     index++; 
     load(root->yes,index); 
     index++; 
     load(root->no,index); 
    ... 
} 
+0

谢谢先生..我会努力的。 – user1242812 2012-03-05 16:18:03