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 6844065
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T00:19:49+00:00 2026-05-27T00:19:49+00:00

I have a BinarySearchTree that consists of nodes which are both a template class

  • 0

I have a BinarySearchTree that consists of nodes which are both a template class of dataType student, where student is a class with private variables of name and grade.

At the moment I can print the tree, find names and or grades in the tree but I am having trouble deleting nodes from the tree.

I am trying to delete all students who had a grade < 50 (so failed).

Once a node is deleted one of the following needs to occur:

  1. Left child is empty: Replace the node by its right child.
  2. Left child is not empty: Replace the node by the highest element in the left
    branch.

My understanding of this is, if this was the tree:

      1
     /  \
    2    3
   / \   /\
  4  5  6  7

If 2 failed i.e. had a grade < 50

You would end up with

     1
    /  \
  4     3
   \    / \
    5  6  7

4 being the highest element in the left branch.

If this was the tree:

     1
    /  \
   2     3
   \     / \
    5  6   7

and 2 failed

you would end up with

     1
    /  \
  5      3
        /  \
       6   7

If this was the tree:

     1
    /  \
  2     3
 / \    / \
 4  5  6  7

and 1 failed

you would end up with

     5
    /  \
  2     3
 /      / \
4      6  7

I have been having a lot of trouble trying to create functions to do this, at the moment I have:

void checkBranch()  //called from main - used to pass the root node to checkBranch()
{
checkBranch(studentTree.getRoot());
}

bool checkBranch(BTNode<Student>* currentNode)
{
if (currentNode != NULL)
{
    if (checkBranch(currentNode -> getLeft()))
    {
        deleteNode(currentNode, true);
    }  

    if (checkBranch(currentNode -> getRight()))
    {
        deleteNode(currentNode, false);
    }

    return (currentNode -> getData().getGrade() < 50.0);
}

else
{
    return false;
}
}

Now I am trying to add the deleteNode function where I am just stuck on what to do / handle what needs to occur:

void deleteNode(BTNode<Student> *parentNode, bool left)
{
BTNode<Student>* nodeToDelete;

if (left)
{
    nodeToDelete = parentNode->getLeft();
}
}
  • 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-27T00:19:50+00:00Added an answer on May 27, 2026 at 12:19 am

    I managed to achieve it with this:

    template <typename dataType>
    void BTree<dataType>::deleteNode(BTNode<dataType> *parentNode, bool left)   
    {
        BTNode<dataType> *nodeToDelete;
        BTNode<dataType> *currentNode;
    
    if (left)
    {
        nodeToDelete = parentNode->getLeft();
    }
    
    else
    {
        nodeToDelete = parentNode->getRight();
    }
    
    if (nodeToDelete->getLeft() == NULL)
    {
        currentNode = nodeToDelete;
    
        if (left)
        {
            parentNode->setLeft(nodeToDelete->getRight());
        }
        else
        {
            parentNode->setRight(nodeToDelete->getRight());
        }
    
        delete nodeToDelete;
    }
    
    else
    {
        BTNode<dataType>* greatestNode = nodeToDelete->getLeft();
    
        if (greatestNode->getRight() == NULL)
        {
            nodeToDelete->getLeft()->setRight(nodeToDelete->getRight()); 
    
            if (left)
            {
                parentNode->setLeft(nodeToDelete->getLeft());
            }
            else
            {
                parentNode->setRight(nodeToDelete->getLeft());
            }
        }
    
        else
        {
            while (greatestNode->getRight()->getRight())
            {
                greatestNode = greatestNode->getRight();
            }
    
            nodeToDelete->setData(greatestNode->getRight()->getData());
    
            BTNode<dataType> *nodeToDelete2 = greatestNode->getRight();
    
            greatestNode->setRight(greatestNode->getRight()->getLeft());
    
            delete nodeToDelete2;
        }
    }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a BinarySearchTree class which is built using a BinaryTree class. Now I
I have the BST class same as in this thread BST.hpp template<class T> class
I want to add an element to a BinarySearchTree. I have a condition that
have next code: class GameTexture { private: LPDIRECT3DTEXTURE9 texture; unsigned char *alphaLayer; UINT width,
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have a bunch of WCF REST services hosted on Azure that access a SQL
Have a photography site that I want to prevent image copying from. How can
Have created a ATL COM project through which I am inserting Menu Items to
I have a WCF service which exposes a Generic interface (and the service has
I have a generic BST and a dataItem class to act as treeNode's value.

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.