在二叉树解决方案中删除

Deleting in Binary Tree sulotion

本文关键字:删除 解决方案 二叉树      更新时间:2023-10-16

经过几个问题和一些很好的答案和友好的帮助。我得到了在二叉树中删除问题的解决方案,我得到了建议,我不能只删除树中最大的数字,因为它可能不是最后一个,或者它有孩子1,2或没有,所以我做了下面的代码,我用了很多评论,希望能帮助你们帮助我。我现在实际上不知道的是,我如何在我的公共中调用这个RemoveLargest()函数,然后在main中稍后调用,即使我不知道代码是否会正常运行。

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

编辑的代码,现在它的运行,它的创建树,显示最大的,但之后我调用我的删除函数仍然崩溃…div =/

就像我之前说的,我认为你把事情弄得太复杂了。你必须考虑在二叉搜索树和节点中键之间的关系中,你的节点是最大的节点意味着什么。

如果一个节点是树中最大的节点,它不可能有右子指针,因为右子指针必须有一个更大的键。然后,如果你知道它最多有一个左子节点,你只需将你的节点替换为它的可能为空的左子节点,就完成了

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