二叉搜索树的插入 查找 删除

//1、定义二叉搜索树类,封装查找、插入、删除操作
删除最为麻烦,其中对于parent的保存用循环来记录 while的条件需多加考虑
#include<queue>
#include<iostream>
using namespace std;

class BinaryTreeNode{
    private:
    int value;
    BinaryTreeNode *leftChild;
    BinaryTreeNode *rightChild;
    public:
    BinaryTreeNode(){};//默认构造函数
    BinaryTreeNode(const int&ele):value(ele),leftChild(NULL),rightChild(NULL){}//给定数值构造
    BinaryTreeNode(const int&ele,BinaryTreeNode*l,BinaryTreeNode*r):value(ele),leftChild(l),rightChild(r){}//构造
    friend class BinarySearchTree;
    void print(){cout<<"值为"<<value<<endl;}
    int getvalue(){return value;};
    void leftChildVal(BinaryTreeNode*node){this->leftChild=node;}//置左孩子的节点为输入节点
    void rightChildVal(BinaryTreeNode*node){this->rightChild=node;}//置右孩子的节点为输入节点

   
};

class BinarySearchTree
{
private:
BinaryTreeNode*root;
public:
BinaryTreeNode* getRoot(){return root;}
BinarySearchTree(){root=NULL;}
BinarySearchTree(BinaryTreeNode*r){root=r;}
BinaryTreeNode* search(BinaryTreeNode*N1,int val);//查找 参数为节点 要找的值 返回的是这个数的节点
void insert(BinaryTreeNode*N1,int value);//插入
void del(BinaryTreeNode*N1,int value);//删除
void levelOrder(BinaryTreeNode*root)//层次遍历
    {
        queue<BinaryTreeNode*> nodeQueue;//需要有头文件 创建以树结点指针为元素的队列
        BinaryTreeNode*temp= root;
        if(temp!=NULL){nodeQueue.push(temp);}//如果当前所指非空,那么入队
        while(nodeQueue.empty()!=true)//当队列不为空 (空的话为true)
        {
         temp=nodeQueue.front();//指向头节点
         cout<<temp->value<<" ";//输出值
         nodeQueue.pop();//删除该节点
         if(temp->leftChild!=NULL)nodeQueue.push(temp->leftChild);
         if(temp->rightChild!=NULL)nodeQueue.push(temp->rightChild);
        }
        cout<<endl;
    }
};



BinaryTreeNode* BinarySearchTree::search(BinaryTreeNode*N1,int val)
{
    BinaryTreeNode*temp=N1;
    if(temp==NULL||temp->value==val)return temp; //如果为空或者值等于,则返回temp;
    BinaryTreeNode*result=NULL;
        if(temp->value<val) result = search(temp->rightChild,val);//如果小于,搜索右子树
        if(temp->value>val) result = search(temp->leftChild,val);//大于,搜索左子树
        return result;
}

void BinarySearchTree::insert(BinaryTreeNode*N1,int val)//两个指针 一个记录当前的,一个记录上一个的
{
    BinaryTreeNode*cur=N1,*parent=N1;
    if(cur==NULL){cur=new BinaryTreeNode(val);}
    while(cur!=NULL)//
    {
        parent=cur;
        if(cur->value>val) cur=cur->leftChild;
        else if(cur->value<val) cur=cur->rightChild;
    }
    if(parent->value<val) parent->rightChild=new BinaryTreeNode(val);
    if(parent->value>val) parent->leftChild=new BinaryTreeNode(val);
}

void BinarySearchTree::del(BinaryTreeNode*N1,int val)
{
    if(N1==NULL) {cout<<"为空树,无法删除"<<endl;}
    BinaryTreeNode*cur=N1;
    BinaryTreeNode*parent=NULL;
    // while(cur!=NULL)//
    // {
    //     parent=cur;
    //     if(cur->value>val) cur=cur->leftChild;
    //     else if(cur->value<val) cur=cur->rightChild;
    //     else if(cur->value==val) break;
    // }
    while (cur != NULL && cur->value != val) {
        parent = cur;
        if (val < cur->value) {
            cur= cur->leftChild;
        } else {
            cur = cur->rightChild;
        }
    }

    //第一种情况 找不到要删除的数
    if(cur==NULL){cout<<"没有这个数,无法删除"<<val<<endl;}
    //第二种情况 待删除节点为叶子节点
    else if(cur->leftChild==NULL&&cur->rightChild==NULL)
    {
        if(parent==NULL) {root=NULL;}
        else{
            if(cur==parent->leftChild) parent->leftChild=NULL;
            else parent->rightChild=NULL;
        }
    }
    //第三种情况 待删除节点只有左子树
    else if(cur->leftChild&&cur->rightChild==NULL)
    {
        if(cur==parent->leftChild) parent->leftChild=cur->leftChild;
        else parent->rightChild=cur->leftChild;
    }
    //第四种情况 待删除节点只有右子树
    else if(cur->rightChild&&cur->leftChild==NULL)
    {
        if(cur==parent->leftChild) parent->leftChild=cur->rightChild;
        else parent->rightChild=cur->rightChild;
    }
    //第五种情况 待删除子树左右子树都有  复制删除 找左子树的最大
    else
    {
        BinaryTreeNode*temp=cur->leftChild;//左子树最大
        while(temp->rightChild!=NULL)
        {
            temp=temp->rightChild;
        }
        cur->value=temp->value;
        del(temp,temp->value);
    }
}

int main()
{
    BinaryTreeNode* node[6];
    node[0]=new BinaryTreeNode(17);
    node[1]=new BinaryTreeNode(15);
    node[2]=new BinaryTreeNode(20);
    node[3]=new BinaryTreeNode(12);
    node[4]=new BinaryTreeNode(16);
    node[5]=new BinaryTreeNode(18);

    BinarySearchTree t1(node[0]);
    node[0]->leftChildVal(node[1]);
    node[0]->rightChildVal(node[2]);
    node[1]->leftChildVal(node[3]);
    node[1]->rightChildVal(node[4]);
    node[2]->leftChildVal(node[5]);
    t1.levelOrder(t1.getRoot());

    //查找部分主函数
    int key1=12,key2=30;
    BinaryTreeNode* N1=t1.search(t1.getRoot(),key1);
    if(N1==NULL){cout<<"没有查找到"<<key1<<endl;}
    else {cout<<"找到了这个数,";N1->print();}
    BinaryTreeNode* N2=t1.search(t1.getRoot(),key2);
    if(N2==NULL){cout<<"没有查找到"<<key2<<endl;}
    else {cout<<"找到了这个数,";N1->print();}

    //插入部分主函数
    t1.insert(t1.getRoot(),22);
    t1.levelOrder(t1.getRoot());
    t1.insert(t1.getRoot(),13);
    t1.levelOrder(t1.getRoot());
    //删除部分主函数
    t1.del(t1.getRoot(),13);
    t1.levelOrder(t1.getRoot());
    return 0;
}
posted @ 2023-11-14 12:55  Fredddy  阅读(10)  评论(0编辑  收藏  举报