数据结构学习第7篇 - 二叉搜索树

二叉搜索树

二叉搜索树是二叉树的重要应用。请编写实现二叉搜索树的基本操作,主要包括:初始化(得到空二叉搜索树)、销毁(释放二叉搜索树的所有节点)、插入、查找(迭代和递归实现)、删除和遍历(前序和中序,分别迭代和递归实现)。

注意:1.树中的元素可以是简单类型,如int类型

      2.通过不断插入元素可以得到二叉搜索树


#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/**********************类型定义 ********************/
#define MAXNODE 200

typedef int elemtype;
 
typedef struct node
{
    elemtype data;  
    struct node *lchild,*rchild;
} BSNode, *BSTree;

BSTree init();                                                //初始化
void destroy(BSTree bst);                                    //注销
BSTree search(BSTree bst,elemtype x);                        //递归搜索
int insert(BSTree *bst,elemtype x);                            //插入
BSTree del(BSTree bst,elemtype x);                            //删除
void NRPreOrder(BSTree bst);                                //迭代先序遍历
void PreOrder(BSTree bst);                                    //递归先序遍历
void InOrder(BSTree bst);                                    //递归中序遍历
void NRInOrder(BSTree bst);                                    //迭代中序遍历
BSTree NRsearch(BSTree bst,elemtype x);                     //迭代搜索
void Menu(void);                                            //主菜单

elemtype x[MAXNODE];                                        //存放各节点数据的数组

int main()
{
    BSTree bst = NULL;
    int i,n,op;
    elemtype X;
    while(1)
    {
        Menu();
        scanf("%d",&op);
        switch(op)
        {
            case 1:
                printf("请输入你要输入的数字个数:");
                scanf("%d",&n);
                for(i = 0; i < n; i++)
                {
                    scanf("%d",&x[i]);
                    if(i == 0)
                    {
                        bst = init();
                        bst->data = x[i];
                    }
                    else
                    {
                        insert(&bst, x[i]);
                    }
                }
                break;
                
            case 2:
                printf("请输入你要删除的数字:\n");
                scanf("%d",&X);
                del(bst,X);
                break;
                
            case 3:
                printf("请输入你想查找的数字:\n");
                scanf("%d",&X);
                printf("递归查找:\n");
                search(bst, X);
                printf("迭代查找:\n");
                NRsearch(bst, X);
                break;
                
            case 4:
                printf("递归前序遍历: \n");
                PreOrder(bst);
                printf("迭代前序遍历:\n");
                NRPreOrder(bst);
                printf("递归中序遍历:\n");
                InOrder(bst);
                printf("迭代中序遍历:\n");
                NRInOrder(bst);
                break;
                
            case 5:
                printf("菜单已退出!\n");
                return 0;
        }
    }
}

//主菜单
void Menu(void)
{
    printf("功能菜单:\n");
    printf("\t1.创建二叉搜索树\n");
    printf("\t2.删除二叉搜索树\n");
    printf("\t3.搜索二叉搜索树\n");
    printf("\t4.遍历二叉搜索树\n");
    printf("\t5.注销并推出菜单\n");
    printf("请输入你的选择:\n");
}


//初始化搜索二叉树
BSTree init(void)
{
    BSTree root = (BSTree)malloc(sizeof(BSNode));
    root->lchild = NULL;
    root->rchild = NULL;
    return root;
}

//插入
int insert(BSTree* bst,elemtype x)
{
    int i;
    
    if(*bst == NULL)
    {
        *bst =(BSTree)malloc(sizeof(BSNode));
        if(*bst == NULL)
        {
            printf("动态内存没有分配!\n");    
        }    
        else
        {
            (*bst)->data = x;
            //printf("%d\n",p->data);
            (*bst)->lchild = NULL;
            (*bst)->rchild = NULL;
        }
    }
    else
    {
        if(x < (*bst)->data)
        {
            printf("左\n");
            insert(&(*bst)->lchild, x);    
        }
        else
        {
            if(x > (*bst)->data)
            {
                printf("右\n");
                insert(&(*bst)->rchild, x);    
            }    
        }    
    }
}

//递归搜素
BSTree search(BSTree bst,elemtype x)
{
    if(bst == NULL)
    {
        printf("查无此人!\n");
        return NULL;
    }
    else
    {
        if(x < bst->data)
        {
            search(bst->lchild, x);
        }    
        else
        {
            if(x > bst->data)
            {
                search(bst->rchild, x);
            }
            else
            {    
                printf("查有此人!\n");
                printf("%d\n",bst->data);
                return bst;    
            }
        }
    }
}

//迭代搜索
BSTree NRsearch(BSTree bst,elemtype x)
{
    while(bst!=NULL)
    {
        if(x < bst->data)
        {
            bst = bst->lchild;
        }
        else if(x > bst->data)
        {
            bst = bst->rchild;
        }
        else if(x == bst->data)
        {
            printf("查有此人!\n");
            printf("%d\n",bst->data);
            return bst;
        }
    }
    if(bst == NULL)
    {
        printf("查无此人!\n");
        return NULL;
    
    }    
}

//找该子树最小权重节点
BSTree FindMin(BSTree bst)
{
    if(bst == NULL)
    {
        return NULL;
    }
    else
    {
        if(bst->lchild == NULL)
        {
            return bst;
        }
        else
        {
            return FindMin(bst->lchild);    
        }    
    }
}

BSTree TmpCell;                                                //删除节点的指针

//递归删除
BSTree del(BSTree bst,elemtype x)
{
    if(bst == NULL)
    {
        printf("查无此人!删除失败!\n");
    }
    else
    {
        if(x < bst->data)
        {
            bst->lchild = del(bst->lchild, x);
        }
        else
        {
            if(x > bst->data)
            {
                bst->rchild = del(bst->rchild, x);
            }    
            else
            {
                if(bst->lchild != NULL && bst->rchild != NULL)
                {
                    TmpCell = FindMin(bst->rchild);
                    bst->data = TmpCell->data;
                    bst->rchild = del(bst->rchild, bst->data);
                }    
                else
                {
                    TmpCell = bst;
                    if(bst->lchild == NULL)
                    {
                        bst = bst->rchild;
                    }    
                    else
                    {
                        if(bst->rchild == NULL)
                        {
                            bst = bst->lchild;
                        }
                    }
                    free(TmpCell);
                }
            }
        }
    }
    return bst;
}

//注销
void destory(BSTree bst)
{
    if(bst != NULL)
    {
        destory(bst->lchild);
        destory(bst->rchild);
        free(bst);    
    }
    return;
}

//查看
void Visite(elemtype p)
{
    printf("%d\n",p);
    
    return;
}

//递归先序遍历
void PreOrder(BSTree bst)
{

    BSTree p = bst;
    if(p == NULL)
    {
        return;
    }
    Visite(p->data);
    
    PreOrder(p->lchild);
    PreOrder(p->rchild);
}

//迭代先序遍历
void NRPreOrder(BSTree bst)
{

    BSTree p = bst;
    if(p == NULL)
    {
        return;
    }

    BSTree stack[MAXNODE];
    
    int top = 0;

    while(!(p == NULL && top == 0))
    {
        
        while(p !=    NULL)
        {
            
            Visite(p->data);
            
            if(top < MAXNODE - 1)
            {
                stack[top] = p;
                top++;
            }
            else
            {
                printf("栈溢出!\n");
                return;
            }
            p = p->lchild;
        }
        top--;
        p = stack[top];
        p = p->rchild;
    
    }
    return;
}

//递归中序遍历
void InOrder(BSTree bst)
{
    if(bst == NULL)
    {
        return;
    }
    else
    {
        InOrder(bst->lchild);
        Visite(bst->data);
        InOrder(bst->rchild);
    }
}

//迭代中序遍历
void NRInOrder(BSTree bst)
{

    BSTree p = bst;
    if(p == NULL)
    {
        return;
    }

    BSTree stack[MAXNODE];
    
    int top = 0;

    while(!(p == NULL && top == 0))
    {
        
        while(p !=    NULL)
        {
            
            
            
            if(top < MAXNODE - 1)
            {
                stack[top] = p;
                top++;
            }
            else
            {
                printf("栈溢出!\n");
                return;
            }
            p = p->lchild;
        }
        top--;
        p = stack[top];
        Visite(p->data);
        p = p->rchild;
    
    }
    return;
}

猜你喜欢

转载自blog.csdn.net/guanshanyue96/article/details/89007964
今日推荐