实现删除二叉查找树中的结点,使其还是二叉查找树,并用层序遍历查看结果

输入10//结点数量
5 1 0 3 2 4 8 6 7 9//插入序列
输出
5 1 8 0 3 6 9 2 4 7//原来树的层序遍历序列
4 1 8 0 3 6 9 2 7//删除根结点后的树的层序遍历序列

#include<cstdio>
#define maxn 102
#include<algorithm>
#include<vector>
#include<cmath>
#include<iostream>
#include<queue>
using namespace std;
//实现给一个原始序列构建其二叉查找树,然后对该树进行删除操作
//删除操作如下,对于给定的树root,删除结点值为x的结点,删除后的树仍然为BST
//先查找到对应结点,然后对于该根结点,删除其等价于,找到其前驱(比结点值小的结点中最大的结点)或后继
//用前驱或后继的值替换该结点的值,并删除前驱或后继,这样得到的树还是BST(不难证明也不难理解)
//该算法还可以优化,不过需要在结点结构体中增加父亲指针域
//找前驱(后继)可以用一个算法来实现,该算法输入根结点,找到该树中值最大的结点该结点一定没有右子树
vector<int>origin,pre,post;
struct node
{
    
    
    int data;
    node* lchild;
    node* rchild;
};
node* newnode(int x)
{
    
    
    node* Node=new node;
    Node->data=x;
    Node->lchild=NULL;
    Node->rchild=NULL;
    return Node;
}
void Insert(node*& root,int x)
{
    
    
    if(root==NULL){
    
    
        root=newnode(x);
        return;
    }
    if(x<root->data){
    
    
        Insert(root->lchild,x);
    }
    else{
    
    
        Insert(root->rchild,x);
    }
}
void preorder(node* root)
{
    
    
    if(root==NULL){
    
    
        return;
    }
    pre.push_back(root->data);
    preorder(root->lchild);
    preorder(root->rchild);
}
void postorder(node* root)
{
    
    
    if(root==NULL){
    
    
        return;
    }
    postorder(root->lchild);
    postorder(root->rchild);
    post.push_back(root->data);
}
node* buildtree()
{
    
    
    node* root=NULL;//这里要注意要赋值为NULL,否则会死循环出错
    for(int i=0;i<origin.size();i++){
    
    
        Insert(root,origin[i]);
    }
    return root;
}
void Layerorder(node* root)
{
    
    
    if(root==NULL){
    
    
        return;
    }
    queue<node*>q1;
    q1.push(root);
    node* temp;
    while(!q1.empty()){
    
    
        temp=q1.front();
        q1.pop();
        printf("%d ",temp->data);
        if(temp->lchild!=NULL){
    
    
            q1.push(temp->lchild);
        }
        if(temp->rchild!=NULL){
    
    
            q1.push(temp->rchild);
        }
    }
    printf("\n");
}
int findmax(node* root)
{
    
    
    while(root->rchild!=NULL){
    
    
        root=root->rchild;
    }
    return root->data;
}
int findmin(node* root)
{
    
    
    while(root->lchild!=NULL){
    
    
        root=root->lchild;
    }
    return root->data;
}
void Delete(node*& root,int x)
{
    
    
    if(root==NULL)return ;
    if(root->data==x){
    
    
        if(root->lchild!=NULL){
    
    
            root->data=findmax(root->lchild);
            Delete(root->lchild,root->data);
            return ;
        }
        else if(root->rchild!=NULL){
    
    
            root->data=findmin(root->rchild);
            Delete(root->rchild,root->data);
            return ;
        }
        else{
    
    
            root=NULL;
            return ;
        }
    }
    if(x<=root->data){
    
    
        Delete(root->lchild,x);
    }
    else{
    
    
        Delete(root->rchild,x);
    }
}
int main()
{
    
    
    int n;
    scanf("%d",&n);
    for(int i=0;i<n;i++){
    
    
        int temp;
        scanf("%d",&temp);
        origin.push_back(temp);
    }
    node* root;
    root=buildtree();
    //preorder(root);
    //postorder(root);
    //for(int i=0;i<post.size();i++){
    
    
            //if(i!=post.size()-1){
    
    
              //  printf("%d ",post[i]);
           // }
            //else{
    
    
          //      printf("%d",post[i]);
         //   }
       // }
     Layerorder(root);
     Delete(root,origin[0]);
     Layerorder(root);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_45890608/article/details/111349968
今日推荐