0
#ifndef BINARYTREE_H
#define BINARYTREE_H
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>

using namespace std;

class binarytree
{
      private:
      struct tree
      {
             int val;
             tree *left;
             tree *right;
      };
      tree *root;
      void insert(tree *&, tree *&);
      void display(tree *) const;
      void destroy(tree *);
    public:

        binarytree()
        {root = NULL;}
        ~binarytree()
        {destroy(root);}

        void insertnode(int);
        bool search(int);
        void display() const
        { display(root); }
};

#endif // BINARYTREE_H

这是我的.h

这是我的.cpp

#include "binarytree.h" // class's header file
#include <iomanip>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cmath>

using namespace std;

void binarytree :: insertnode( int num)
{
     tree *newnode;

     newnode = new tree;
     newnode->val = num;
     newnode->left = newnode->right = NULL;
     insert(root, newnode);
     }

     void binarytree :: insert(tree *&Ptr, tree *&newnode)
     {
          if(Ptr == NULL)
                 Ptr = newnode;
                 else if( newnode->val < Ptr->val)
                 insert(Ptr->left, newnode);
                 else
                 insert(Ptr->right, newnode);
                 }

void binarytree :: destroy(tree *Ptr)
{
     if(Ptr)
     {
            if(Ptr->left)
            destroy(Ptr->left);

             if(Ptr->right)
            destroy(Ptr->right);

            delete Ptr;
            }
     }

我认为这是一个析构函数错误,但我不明白为什么。如果有语法错误或什么我会觉得很傻,但我刚刚从一本书中复制了这个,所以我似乎没有遗漏任何东西。任何建议都会有所帮助。如果您需要任何澄清,请询问!

4

0 回答 0