1. 程式人生 > >二叉排序樹 基礎操作 增刪改查

二叉排序樹 基礎操作 增刪改查

//本人比較懶,寫不寫註釋看心情

#include<iostream>
#include<malloc.h>
#include<cstdio>
#include<cstdlib>
using namespace std;
typedef struct node
{
    int key;
    char data;
    struct node *lchild,*rchild;
}BSTNode;
void DispBST(BSTNode *bt);
bool InsertBSTNOde(BSTNode *&bt,int k)
{
    if(bt==NULL)
    {
        bt=(BSTNode *)malloc(sizeof(BSTNode));
        bt->key=k;
        bt->lchild=NULL;
        bt->rchild=NULL;
        return true;
    }
    else if(bt->key==k)
    {
        return false;
    }
    else if(k<bt->key)
    {
        return InsertBSTNOde(bt->lchild,k);
    }
    else
    {
        return InsertBSTNOde(bt->rchild,k);
    }
}

BSTNode *CreateBST(int A[],int n)
{
    BSTNode *bt=NULL;
    int i=0;
    while(i<n)
    {
        if(InsertBSTNOde(bt,A[i]))
        {
            printf("第%d步,插入%d:",i+1,A[i]);
            DispBST(bt);
            printf("\n");
            i++;
        }
    }
    return bt;
}

void DispBST(BSTNode *bt)
{
    if(bt!=NULL)
    {
        printf("%d",bt->key);
        if(bt->lchild!=NULL||bt->rchild!=NULL)
        {
            printf("(");
            DispBST(bt->lchild);
            if(bt->rchild!=NULL)
            {
                printf(",");
            }
            DispBST(bt->rchild);
            printf(")");
        }
    }
}

BSTNode *SearchBST(BSTNode *bt,int k,BSTNode *f1,BSTNode *&f)
{
    if(bt->key==k)
    {
        f=f1;
        return bt;
    }
    else if(k<bt->key)
    {
        return SearchBST(bt->lchild,k,bt,f);
    }
    else
    {
        return SearchBST(bt->rchild,k,bt,f);
    }
}
void Delete1(BSTNode *p,BSTNode *&r)
{
    BSTNode *q;
    if(r->rchild!=NULL)
    {
        Delete1(p,r->rchild);
    }
    else
    {
        p->key=r->key;
        q=r;
        r=r->lchild;
        free(q);
    }
}
void Delete(BSTNode *&p)
{
    BSTNode *q;
    if(p->lchild==NULL)
    {
        q=p;
        p=p->rchild;
        free(q);
    }
    else if(p->rchild==NULL)
    {
        q=p;
        p=p->lchild;
        free(q);
    }
    else
    {
        //printf("%d\n",p->key);
        Delete1(p,p->lchild);
    }
}

bool deleteBST(BSTNode *&bt,int k)
{
    if(bt==NULL)
    {
        return false;
    }
    else
    {
        if(k<bt->key)
        {
            return deleteBST(bt->lchild,k);
        }
        else if(k>bt->key)
        {
            return deleteBST(bt->rchild,k);
        }
        else
        {
            //printf("%d",bt->key);
            Delete(bt);
            return true;
        }
    }
}

int main()
{
    BSTNode *bt;
    int a[]={4,9,0,1,8,6,3,5,2,7};
    int n=10;
    bt=CreateBST(a,n);
    DispBST(bt);
    printf("\n");
    printf("輸入需要查詢的值");
    int k;
    BSTNode *f,*f1,*ser;
    scanf("%d",&k);
    ser=SearchBST(bt,k,f1,f);
    printf("要找的人是他:%d\n",ser->key);
    printf("他的Parent:%d\n",f->key);
    printf("輸入需要刪除的值");
    int s;
    scanf("%d",&s);
    deleteBST(bt,s);
    printf("刪除之後的二叉排序樹:\n");
    DispBST(bt);
    return 0;
}