2012-04-30 2 views
-3

このコードでは、numitemsを静的変数として宣言し、 のoutsizeを初期化しました。私はそれが(return (numitems==order-1));でのエラーの主な理由だと思っていたので、ここでも(childarrray[0]==NULL)に関連する問題があります。このコードは完全にJavaから取得され、C++に変換されていますので、参照の代わりにポインタを追加しました。私のコードでエラーを修正するのを助けてください。プログラムは子配列を読み取ることができませんでした[0]

#include<iostream> 
    #include<cstring> 
    using namespace std; 
    class data 
    { 
    public: 
     long ddata; 
     data(long dd) 
     { 
      ddata=dd; 
     } 
     void display() 
     { 
      cout<<ddata<<" "; 
     } 
     }; 
    class Node 
    { 
    private: 
    //public: 
      static const int order=4; 
     static int numitems; 
     Node *parent; 
     Node *childarray[order]; 
     data *item[order-1]; 
    public: 
     Node(){} 
    public: 
     void connect(int childnum,Node *child) 
     { 
      childarray[childnum]=child; 
      if(child!=NULL) 
       child->parent=(this); 

     } 
     //disconetc from this node,return it; 

     Node *disconnectchild(int childnum) 
     { 
      Node *tempnode=childarray[childnum]; 
      childarray[childnum]=NULL; 

     return (tempnode); 
     } 
     Node *getchild(int childnum){ 
      return childarray[childnum]; 
    } 
     Node *getparent() 
     { 

      return parent; 
     } 
     bool isleaf() 
     { 
      return ((childarray[0]==NULL)?true:false); 

     } 
     int getnumitems() 
     { 
      return numitems; 
     } 
     data *getitem(int index) 
     { 
      return item[index]; 

     } 

     bool isfull() 
     { 

      return (numitems==(order-1)); 
     } 
     int finditem(long key) 
     { 
      for(int j=0;j<order-1;j++) 
      { 
       if(item[j]==NULL) 
        break; 
       else if(item[j]->ddata==key) 
        return j; 




      } 
      return -1; 

     } 
     int insertitem(data *newitem) 
     { 
      if(numitems==order-1) 
        return 0; 
      numitems++; 
      long newkey=newitem->ddata; 
      for(int j=order-2;j>=0;j--) 
      { 
       if(item[j]==NULL) 
        continue; 
       else 
       { 
        long itskey=item[j]->ddata; 
        if(newkey<itskey) 
         item[j+1]=item[j]; 
        else 
        { 
         item[j+1]=newitem; 
         return j+1; 

        } 
           } 

      } 
      item[0]=newitem; 
      return 0; 



     } 
     data *removeitem() 
     { 
      data *temp=item[numitems-1]; 
      item[numitems-1]=NULL; 
      numitems--; 
      return temp; 


     } 
     void displayNode() 
     { 
      for(int j=0;j<numitems;j++) 
       item[j]->display(); 
      cout<<endl; 

     } 

    }; 

    int Node::numitems=0; 
    class tree234 
    { 

     Node *root; 
    public: 
     tree234() : root(NULL) {} 
     void displaytree() 
     { 

      recursivdisplay(root,0,0); 

     } 
     void recursivdisplay(Node *thisnode,int level,int childnum) 
     { 
      thisnode->displayNode(); 
      int numitems=thisnode->getnumitems(); 
      for(int j=0;j<numitems+1;j++) 
      { 
       Node *nextnode=thisnode->getchild(j); 
       if(nextnode!=NULL) 
        recursivdisplay(nextnode,level+1,j); 
       else 
         return ; 




      } 





     } 
     Node *getnextchild(Node *theNode,long thevalue) 
     { 
      int j; 
      int numItems=theNode->getnumitems(); 
      for(j=0;j<numItems;j++) 
      { 

       if(thevalue<theNode->getitem(j)->ddata) 
        return theNode->getchild(j); 

      } 
      return theNode->getchild(j); 

     } 
      void split(Node * thisnode) 
      { 
       data *itemb,*itemc; 
       Node *parent,*child2,*child3; 
       int itemindex; 
       itemc=thisnode->removeitem(); 
       itemb=thisnode->removeitem(); 
       child2=thisnode->disconnectchild(2); 
       child3=thisnode->disconnectchild(3); 
       Node *newright=new Node(); 
       if(thisnode==root) 
       { 
        root=new Node(); 
        parent=root; 
        root->connect(0,thisnode); 




       } 
       else 
        parent=thisnode->getparent(); 
       //deal with parent 
       itemindex=parent->insertitem(itemb); 
       int n=parent->getnumitems(); 
       for(int j=n-1;j>itemindex;j--) 
       { 
        Node *temp=parent->disconnectchild(j); 
        parent->connect(j+1,temp); 


       } 

       parent->connect(itemindex+1,newright); 
       //deal with newright 
       newright->insertitem(itemc); 
       newright->connect(0,child2); 
       newright->connect(1,child3); 

      } 
    public: 
     void insert(long dvalue) 
     { 
      Node *curnode=root; 
      data *tempitem=new data(dvalue); 
      while(true) 
      { 
       if(curnode->isfull()) 
       { 

        split(curnode); 
        curnode=curnode->getparent(); 

        curnode=getnextchild(curnode,dvalue); 


       } 
       else if(curnode->isleaf()) 
        break; 
       else 
        curnode=getnextchild(curnode,dvalue); 

      } 
      curnode->insertitem(tempitem); 
       } 
    public: 
     int find(long key) 
     { 
      Node *curnode=root; 
    int childnum; 
    while(true) 
    { 
     if((childnum=curnode->finditem(key))!=-1) 
      return childnum; 
     else if(curnode->isleaf()) 
      return -1; 
     else 
      curnode=getnextchild(curnode,key); 
    } 
     } 
    }; 

    char Getchar() 
    { 
     char AB; 
     cin>>AB; 
     return AB; 
    } 
    int main() 
    { 
     int found; 
     long value; 
     tree234 *thetree=new tree234(); 
     thetree->insert(50); 
     thetree->insert(40); 
     thetree->insert(60); 
     thetree->insert(30); 
     thetree->insert(70); 
     while(true) 
     { 
    cout<<" enter first letter "<<endl; 
    cout<<" show ,insert or find : "<<endl; 
    char choice=Getchar(); 
    switch(choice) 
    { 
    case 's': 
     cout<<" display tree "; 
     thetree->displaytree(); 
     break; 

    case 'i': 
     cout<<" enter value to insert "<<endl; 
     cin>>value; 
     thetree->insert(value); 
     break; 
    case 'f' : 
     cout<< "enter value to find "; 
     cin>>value; 
     found=thetree->find(value); 
     if(found!=-1) 
      cout<<" found = "<<value<<endl; 

     else 

      cout<< " not found " <<value <<endl; 

     break; 
    default: 
     cout <<" invalid entry "<<endl;; 
     } 


     } 
     delete thetree; 

     return 0; 
    } 
+7

このコードについては、何度も繰り返し投稿しています。 SOはデバッグサービスではありません。ある時点では、使用している言語を実際に理解し、これを自分でデバッグするために必要な時間を費やさなければならないでしょう - 小冊子を1インチ単位で取得すると、コードが最終的に実行されるかもしれませんが、多くのプロセスで。 – Mat

+0

私はたくさんのことを学んだことがありますが、この状況で助けが必要な人は誰でも私を助けることができますか? –

+0

[tree234クラスクラッシュ]の重複可能性があります(http://stackoverflow.com/questions/10381541/tree234-class-crashes) – Johnsyweb

答えて

2

問題は、それがゴミでいっぱいであるとNULLへの比較は失敗しますので、あなたは、childarrayを初期化していないことが考えられます。

class Node 
{ 
private: 
    static const int order=4; 
    Node *childarray[order]; // this is full of, well, anything. 
}; 

非常に少なくとも、あなたはそれを初期化する必要があります配列はNULLで満杯です。これを行うには、コンストラクタを追加し、初期化リストで初期化することができます。

class Node { 
public: 
    Foo() : childarray() {} 
private: 
    // as before 
}; 
+0

どうすれば初期化できますか? –

+2

これは、コードを変換しようとする前にC++を学ぶ時間がかかっているので、本当に便利です。コンストラクタを参照してください。 – chris

+0

@datoをコンストラクタで編集した回答を参照してください。配列データメンバの初期化に関する多くの質問があります。そして、私はC++を学ぶのに少し時間を費やすことが本当に効果的だと思います。長期的には時間を節約できます。 – juanchopanza

関連する問題