Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6319065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T15:44:19+00:00 2026-05-24T15:44:19+00:00

I am a C++ newbie, not quite understand the destruction workflow of this program,

  • 0

I am a C++ newbie, not quite understand the destruction workflow of this program, so I write destructor for both classes. But got error… Do I bother defining the destructor of Node?

#include<iostream>
using namespace std;

// in this case, the BST doesn't have duplicates
class Node
{
public:
    Node* parent;
    Node* left;
    Node* right;
    Node() : key(-1), parent(NULL), left(NULL), right(NULL) {}
    virtual ~Node() {cout << "destructor of Node" << endl; delete parent; delete left; delete right;}
    void setKey(int k)
    {
        key = k;
    }
    int getKey() {return key;}
private:
    int key;
};


class BST
{
public:
    Node* root;
    BST() {root = NULL;}
    virtual ~BST() {freeNode(root);}
    void addNode(int key)
{
    if (!root)
    {
        root = new Node();
        root->setKey(key);
    }
    else 
        addNode(key, root);
}
Node* findNode(int key)
{
    Node* p = root;
    while (p)
    {
        if (p->getKey() == key)
            return p;
        else if (p->getKey()<=key)
        {
            p = p->right;
        }
        else
            p = p->left;
    }
    return p;
}
void walk(Node* node)
{
    if (node)
    {
        walk(node->left);
        cout << node->getKey() << " " << flush;
        walk(node->right);
    }
}
void deleteNode(int key)
{
    Node* p = findNode(key);
    if (p)
    {
        //case 1: p has no children
        if (!p->right && !p->left)
            p->parent->right == p ? p->parent->right = NULL : p->parent->left = NULL;
        //case 2: p has one child
        else if (!p->left && p->right)
        {
            p->parent->right = p->right;
            freeNode(p);
        }
        else if (!p->right && p->left)
        {
            p->parent->left = p->left;
            freeNode(p);
        }
        //case 3: p has two children
        else
        {
            Node *suc = successor(key);
            exchange(suc,p);
            deleteNode(suc->getKey());
        }

    }
}
Node* min(Node* node)
{
    //empty tree
    if (!node)
        return NULL;
    else if (!node->left)
        return node;
    else
        return min(node->left);
}
Node* max(Node* node)
{
    //empty tree
    if(!node)
        return NULL;
    else if (!node->right)
        return node;
    else 
        return max(node->right);
}
Node* successor(int key)
{
    Node *temp = NULL;
    Node *p = findNode(key);
    //case 1: has a right child
    if (p->right)
        return min(p->right);
    //case 2: does not have a right child
    else
    {
        temp = p->parent;
        while(temp->left != p)
        {
            p = temp;
            temp = temp->parent;
        }
        return temp;
    }
}

Node* predecessor(int key)
{
    Node *temp = NULL;
    Node *p = findNode(key);
    //case1: has a left child
    if (p->left)
        return max(p->left);
    //case2: does not have a left child
    else
    {
        temp = p->parent;
        while(temp->right != p)
        {
            p = temp;
            temp = temp->parent;
        }
        return temp;
    }
}

private:
void addNode(int key, Node* node)
{
    if (node->getKey() <= key)
    {
        if (node->right)
            addNode(key, node->right);
        else
        {
            Node* leaf = new Node();
            leaf->setKey(key);
            leaf->parent = node;
            node->right = leaf;
        }
    }
    else
    {
        if (node->left)
            addNode(key, node->left);
        else
        {
            Node* leaf = new Node();
            leaf->setKey(key);
            leaf->parent = node;
            node->left = leaf;
        }
    }

}
void freeNode(Node* leaf)
{
    delete leaf;
}
void exchange(Node *a, Node *b)
{
    int temp = a->getKey();
    a->setKey(b->getKey());
    b->setKey(temp);
}
};

int main(int argc, char** args)
{
int *p = NULL;
delete p;

BST tree;
tree.addNode(8);
tree.addNode(4);
tree.addNode(12);
tree.addNode(2);
tree.addNode(6);
tree.addNode(10);
tree.addNode(14);
tree.addNode(1);
tree.addNode(3);
tree.addNode(5);
tree.addNode(7);
tree.addNode(9);
tree.addNode(11);
tree.addNode(13);
tree.addNode(15);
tree.walk(tree.root);
return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T15:44:21+00:00Added an answer on May 24, 2026 at 3:44 pm
    delete parent; delete left; delete right;
    

    When you delete an object, its destructor is called. Here, both the left and the right node will delete the current one as they delete their parents. To fix this, simply remove parent deletion.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm newbie in Python. I can't understand why this code does not work: reOptions
I know this is a total newbie question, but the answer may not be
I think that this is a newbie type question but I have quite understood
Maybe my question is a newbie one, but I can not really understand the
Bit of a javascript newbie so not sure if this question is easy or
I am a complete newbie to iOS development but not to programming. I am
Don't dismiss this as a newbie question! It's not, I'm not, I've tried everything,
I'm a regular expression newbie and I can't quite figure out how to write
I apologize if this has been asked before, but I haven't quite found the
This is a very standard newbie question, but I am looking for an expert

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.