12,065,060 members (44,943 online)
Rate this:
See more:
My program is crashing when i try to delete the largest number, can some one help me please? I really dont know any further i tried my best.

Here is the code please some one help:

```#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;
}

{
Insert(newData, root);
}

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

};

int main()
{
BinaryTree<int> *myBT = new BinaryTree<int>();

//for(int i = 0; i < 10; i++)			//randommal tolti fel/fill with random
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
}</int></int></class></cstdlib></string></iostream>```
Posted 19-Nov-11 5:33am
T0mii604
OriginalGriff 19-Nov-11 10:47am

Ignore me - I'm an idiot...Deleted.
G0geta 19-Nov-11 10:48am

hoped some one found it.
OriginalGriff 19-Nov-11 11:05am

Have you tried running it through the debugger? Easier to work out what is going on then - at the moment it could be that the tree is badly formed or anything!

Rate this:

## Solution 2

fixed it, i was over thinking it, cause i just need to delete the largest number in my tree and that wont have a right childe, and then i only have to replace it with the left childe if he got one and delete it, it works perfect, ty everyone who helped me, trough this project.

Here is the fix:

```    T LargeRemove(Node*& n){

if (!n)
return -1;

if (n->rChildptr)
return LargeRemove(n->rChildptr);

T result = n->data;

Node *d = n;
n = n->lChildptr;
delete d;

return result;

};
```
RaviRanjankr 19-Nov-11 14:55pm

My 5+ for share your Solution.

Top Experts
Last 24hrsThis month
 OriginalGriff 190 RDBurmon 100 ppolymorphe 50 CPallini 50 nv3 40
 Dave Kreskowiak 1,857 Richard MacCutchan 1,250 d@nish 980 OriginalGriff 974 CPallini 790