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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:28:04+00:00 2026-05-26T21:28:04+00:00

After a few questions and some nice answers and friendly helpers here. I got

  • 0

After a few questions and some nice answers and friendly helpers here. I got the sulotion to my porblem with the deleting in the binary tree, i got suggested that, i can not just delet the largest number in the tree cause its may not the last or it has childrens 1 ,2 or none, so i made the code down below, i used a lot commenting hope that can help you people help me. What i actually dont know now, is how do i call this RemoveLargest() function in my public and then later in main, even though i dont know if the code will run properly.

#include <iostream>
#include <string>
#include <cstdlib> 

using namespace std;

template<class T>
class BinaryTree
{
struct Node
    {
        T data;
        Node* lChildptr;
        Node* rChildptr;

        Node(T dataNew)
        {
            data = dataNew;
            lChildptr = NULL;
            rChildptr = NULL;
        }
    };
private:
    Node* root; 

        void Insert(T newData, Node* &theRoot) //Insert elements into the tree start.
        {
            if(theRoot == NULL) 
            {
                theRoot = new Node(newData);
                return;
            }
            if(newData < theRoot->data)  
                Insert(newData, theRoot->lChildptr);
            else
                Insert(newData, theRoot->rChildptr);
        }                               //end.

        void PrintTree(Node* theRoot) //print me the tree /start
        {
            if(theRoot != NULL)
            {
                PrintTree(theRoot->lChildptr);
                cout<< theRoot->data<<" \n";
                PrintTree(theRoot->rChildptr);
            }
        }                           //end.

        T Largest( Node* theRoot) // show me largest number /start.
            {
        if ( root == NULL )
            {
                 cout<<"There is no tree";
                 return -1;
            }
            if (theRoot->rChildptr != NULL)
            {
                 return Largest(theRoot->rChildptr);
            }
            T value = theRoot->data;
            return value;

        }                   //end.
        void RemoveLargest(Node* theRoot)  //remove the largest priority number from tree /start.
        {
            Node* current;  //the current tree?
            Node* parent;   //the parent of the current node?
            current=theRoot;

                // 3 cases :
                // 1. We're removing a leaf node
                // 2. We're removing a node with a single child
                // 3. we're removing a node with 2 children
            //Node with single child.
            if((current->lChildptr == NULL && current->rChildptr != NULL)||(current->lChildptr != NULL && current->rChildptr == NULL))
            {
                if(current->lChildptr == NULL && current->rChildptr != NULL)
                {
                    if(parent->lChildptr==current)
                    {
                        parent->lChildptr = current->rChildptr;
                        delete current;
                    }
                    else
                    {
                        parent->rChildptr = current->rChildptr;
                        delete current;
                    }
                }
                else //left child ok, no right child
                {
                    if(parent->lChildptr==current)
                    {
                        parent->lChildptr = current->lChildptr;
                        delete current;
                    }
                    else
                    {
                        parent->rChildptr = current->lChildptr;
                        delete current;
                    }
                }
            return;
            }
            //We found a leaf(a node with not a single child)
            if(current->lChildptr == NULL && current->rChildptr == NULL)
            {
                if (parent->lChildptr == current)
                    parent->lChildptr = NULL;
                else
                    parent->rChildptr = NULL;
                delete current;
                return;
            }
            //Node with 2 children
            // replace node with smallest value in right subtree
            if (current->lChildptr != NULL && current->rChildptr != NULL)
            {
                Node* checkr;
                checkr = current->rChildptr;
                if((checkr->lChildptr == NULL)&&(checkr->rChildptr == NULL))
                {
                    current=checkr;
                    delete checkr;
                    current->rChildptr = NULL;
                }
                else //right child has children
                {
                //if the node's right child has a left child
                //Move all the way down left to locate smallest element
                    if ((current->rChildptr)->lChildptr != NULL)
                    {
                    Node* lcurr;
                    Node* lcurrp;
                    lcurrp = current->rChildptr;
                    lcurr = (current->rChildptr)->lChildptr;
                    while(lcurr->lChildptr != NULL)
                        {
                            lcurrp = lcurr;
                            lcurr = lcurr->lChildptr;
                        }
                    current->data = lcurr->data;
                    delete lcurr;
                    lcurrp->lChildptr = NULL;
                    }
                    else 
                    {
                        Node* temp;
                        temp = current->rChildptr;
                        current->data = temp ->data;
                        current->rChildptr = temp->rChildptr;
                        delete temp;
                    }

                }
                return;
            }

        };

    public:
        BinaryTree()
        {
            root = NULL;
        }

        void AddItem(T newData)
        {
            Insert(newData, root);
        }

        void PrintTree()
        {
            PrintTree(root);
        }
        T Largest()
        {
            return Largest(root);
        }
        void RemoveLargest()
        {
            RemoveLargest();
        }

    };

    int main()
    {
        BinaryTree<int> *myBT = new BinaryTree<int>();
        myBT->AddItem(5);
        myBT->AddItem(1);
        myBT->AddItem(4);
        myBT->AddItem(2);
        myBT->AddItem(3);

            //for(int i = 0; i < 10; i++)           //randommal tolti fel/fill with random
                //myBT->AddItem(rand() % 100);
        cout << "BinaryTree:" << endl;              //kilistazaa a fat/ list my tree
        myBT->PrintTree();
        cout << "Largest element: " << myBT->Largest() << endl;  //visszaadja a legnagyobb elemet/shows the largest number
        myBT->RemoveLargest();  //suposed to delet the largest number
        myBT->PrintTree(); //shows list again
  }

edited the code, now its running, its creating the tree, shows the largest, but after i call my remove function still crashing… =/

  • 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-26T21:28:04+00:00Added an answer on May 26, 2026 at 9:28 pm

    Like I said before, I think you’re making things overly complicated. You have to think of what it means that your node is the largest one, in the context of a binary search tree and the relationship between the keys in its nodes.

    If a node is the largest one in the tree it cannot possibly have a right child pointer, because the right child would have to have a larger key. And then, if you know that it has at most a left child, you just replace your node with its possibly null left child, and you’re done.

    T ExtractLargest(Node*& n){
    
        if (!n)
            return -1;
    
        if (n->rChildptr)
            return ExtractLargest(n->rChildptr);
    
        T result = n->data;
    
        Node *d = n;
        n = n->lChildptr;
        delete d;
    
        return result;
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After cobbling together a few questions I've managed to get this far to showing
I have a few questions about this after reading the iPhone documentation on it:
I just have a few questions about programmatically adding controls in aspx . After
I've seen a few questions here related to determining the similarity of files, but
Right, i'm new to PHP and mySQL, but i've got a few questions that
I am aware that there were similar questions in past few years, but after
We have done some basic TCP communication, but have a few questions. We are
I know there are a few questions floating around here on the subject, but
just a few questions that I thought I could use some light on. So
Hey so after reading this article I've been left with a few questions I

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.