删除时二进制树崩溃 [英] Binary Tree crash while delete

查看:105
本文介绍了删除时二进制树崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试删除最大数量的电话时,我的程序崩溃了,请问有人可以帮我吗?我真的不知道我会尽力了.

这是代码,请提供一些帮助:

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

推荐答案

修复了该问题,我想得太过分了,因为我只需要删除树中最大的数字,就不会有正确的子对象,并且那么我只需要用左子孙替换它就可以了,然后删除它,它很完美,非常感谢所有帮助我的人,通过这个项目.

解决方法如下:

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;

};


这篇关于删除时二进制树崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆