Cây nhị phân C++


Từ: 23:14 23/10/2013
Bài: 1
Cảm ơn: 0
Thích: 0
Cao thủ nào tìm cho e chỗ sai ở phần void tìm kiếm cái e sửa mãi mà k dk
 
 
#include<iostream>
#include<fstream>
 
using namespace std;
 
struct Node{
       int data;
       Node * parent;
       Node * firtChild;
       Node * nextSibling;
       
       Node(int v) //ham khoi tao cay gia tri v
       {
                data = v;
                parent = NULL;
                firtChild = NULL;
                nextSibling = NULL;
       }
       
};
void noicha(Node *&f,Node *&j)
{
     j->parent=f;
     f->firtChild=j;
}
void noicon(Node *&f,Node *&j)
{
     j->parent = f->parent;
     f->nextSibling=j;
     
}
Node* tim(Node*& root,int x)
 {//Dieu kien dung
  Node* p;
  if(root==NULL) return NULL;
  if(root->data==x) return root;
  //Buoc de quy
  if(root->firtChild!=NULL) p=tim(root->firtChild,x);
  else 
  {
       if(root->nextSibling!=NULL) p=tim(root->nextSibling,x);
       else {
            p=root->parent;
       }
  }
  return p;
}
 
void doc(Node *&root, char* filename)
{
    ifstream fin(filename);
    if(!fin.good()){
        cout << "Loi doc file " << filename << endl;
        return;
    }
    int v,u;
    
    Node *P,*Q;
        fin >> u;
        fin >> v;
    root=new Node(u);
    P=new Node(v);
    noicha(root,P);
    while((fin >> u)&&(fin >>v))
    {     
       Q=tim(root,u);
       if(Q->firtChild==NULL)
       {
           Node* R=new Node(v);
           noicha(Q,R);
       }
       else
       {
           Node* R=new Node(v);
           Q=Q->firtChild;
 
           if(Q->nextSibling==NULL) noicon(Q,R);
           else
           {
                Q=Q->nextSibling;
                for(Q;Q!=NULL;Q=Q->nextSibling)
                {
                    if(Q->nextSibling==NULL)
                    {
                        noicon(Q,R);
                        break;
                    }
                }
           }
       }
    }
}
 
void print(int& a)
{
     cout <<a<<" ";
}
void  DuyetCay(Node*   root, void  f(int&))
{
        if (root != NULL)
        {
               f(root->data);
               Node* P;
               P=root->firtChild;
               while (P != NULL)
              {
                    DuyetCay(P, f);
                    P = P->nextSibling; 
             }
        }
}
int main()
{
    Node * root;
    root = NULL; 
    doc(root,"cay.txt");
    
    DuyetCay(root,print);
    system("pause");
    return 0;
}