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

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:

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; } 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 }</int></int></class></cstdlib></string></iostream>

T0mii604

Comments

Ignore me - I'm an idiot...Deleted.

hoped some one found it.

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!

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:

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

Comments

My 5+ for share your Solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject,
503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada
+1 416-849-8900 x 100