-4

在这段代码中,我将 numitems 声明为静态变量,并初始化了类的超大尺寸。因为我认为这是错误的主要原因,(return (numitems==order-1));但这里也是与相关的问题(childarrray[0]==NULL),完全这段代码取自 Java 并转换为 C++,所以我添加了指针而不是引用。请帮助修复我的代码中的错误。

    #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;
    }
4

1 回答 1

2

问题可能是你没有初始化childarray,所以它充满了垃圾,任何比较NULL都会失败:

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

至少,您需要初始化该数组,使其充满NULLs。您可以通过添加构造函数并在初始化列表中进行初始化来做到这一点:

class Node {
public:
  Foo() : childarray() {}
private:
  // as before
};
于 2012-04-30T11:45:30.107 回答