Binary Search Tree Destructor

前端 未结 6 1384
太阳男子
太阳男子 2021-02-04 13:51

Working on implementing my own BST in C++ for the experience of dealing with such structures.

I\'ve had trouble implementing a destructor. I found in my studies, that on

6条回答
  •  面向向阳花
    2021-02-04 14:31

    You can have a recursive destructor; what you can't do is delete the same object twice.

    A typical way to delete a tree in C++ might be something like this:

    BinSearchTree::~BinSearchTree()
    {
       delete _rootNode;  // will recursively delete all nodes below it as well
    }
    
    tNode::~tNode()
    {
       delete left;
       delete right;
    }
    

    Regarding the unresolved external error -- is that error thrown when you try to compile/link the program? If so, it's probably because the code for the tNode class (and in particular the tNode destructor, if you declared one) doesn't exist or isn't getting compiled into your project.

提交回复
热议问题