二叉树(BST树)内结点的删除(3种情况全解) 代码优化

#include <stdio.h>
//#include <stdlib.h>
#include "stdafx.h"
#include <iostream>

struct tree                    //树结构的声明
{
    int data;                  //结点数据
    struct tree *left;         //指向左子树的指针
    struct tree *right;        //指向右子树的指针
};

typedef struct tree treenode;  //树的结构新类型
typedef treenode *btree;       //声明树结点指针类型

/*
 递归创建二叉树
 */
btree createbtree(int *data, int pos)
{
    btree newnode;

    //递归终止条件
    if (data[pos] == 0 || pos > 15)
    {
        return NULL;
    }
    else
    {
        //给新节点分配内存
        newnode = (btree)malloc(sizeof(treenode));
        //创建新节点内容
        newnode->data = data[pos];
        //创建左子树的递归调用
        newnode->left = createbtree(data, 2*pos);
        //创建右子树的递归调用
        newnode->right = createbtree(data, 2*pos+1);
        return newnode;
    }
}

/* 
 二叉树的查找
 */
btree btreefind(btree ptr, int value, int *pos)
{
    btree backfather;

    backfather = ptr;          //设置父节点指针初值
    *pos = 0;                  //设置位置初值

    while (ptr != NULL)
    {
        if (ptr->data == value)
        {
            return backfather;  //找到了返回父节点
        }
        else
        {
            backfather = ptr;
            if (ptr->data > value)
            {
                ptr = ptr->left;    //左子树
                *pos = -1;
            }
            else
            {
                ptr = ptr->right;   //右子树
                *pos = 1;
            }
        }
    }
    return NULL;
}

/*
 二叉树节点的删除
 */
btree deletenode (btree root, int value)
{
    btree backfather;         //父结点指针
    btree ptr = NULL;                //删除结点指针
    btree next;               //子结点指针
    int pos;                  //删除位置

    backfather = btreefind(root, value, &pos);
    if (backfather == NULL)  //没有找到
    {
        return root;
    }
    //删除位置
    switch (pos)
    {
        case -1:
        {
            //左子节点
            ptr = backfather->left;
            break;
        }
        case 1:
        {
            //右子节点
            ptr = backfather->right;
            break;
        }
        case 0:
        {
            //跟节点
            ptr = backfather;
            break;
        } 
    }
    /* 第一种情况,没有左子树 */
    if (ptr->left == NULL)
    {
        if (backfather != ptr) //判断是否是根节点
        {
            if( backfather->left->data ==  ptr->data) //这里做了优化
            {
                backfather->left = ptr->right;
            }
            else
            {
                backfather->right = ptr->right;
            }
        }
        else
        {
            //是根节点,那么根节点直接指向右节点
            root = root->right;
        }
        free(ptr);
        ptr = NULL;
        return root;
    }
    /* 第二种情况,没有右子树 */
    if (ptr->left == NULL || ptr->right == NULL)
    {
        if (backfather != ptr) //判断是否是根节点
        {
            if( backfather->left->data ==  ptr->data)//这里做了优化
            {
                backfather->left = ptr->left;
            }
            else
            {
                backfather->right = ptr->left;
            }
        }
        else
        {
            //是根节点,那么根节点直接指向右节点
            root = root->left;
        }
        free(ptr);
        ptr = NULL;
        return root;
    }
    /* 第三种情况,有左子树也有右子树 */
    backfather = ptr;  //父节点指向当前节点
    next = ptr->left;   //设置子节点
    while (next->right != NULL)
    {
        backfather = next;
        next = next->right;
    }
   
    int data = next->data;

    root = deletenode(root, next->data);

    ptr->data = data;  //替换数据
    return root;
}

/* 
 二叉树的前序遍历输出
 */
void preolder(btree ptr)
{
    if (ptr)
    {
        printf("%2d - ", ptr->data);
        preolder(ptr->left);
        preolder(ptr->right);
    }
}

/*
 二叉树的中序遍历输出
 */
void inolder(btree ptr)
{
    if (ptr)
    {
        inolder(ptr->left);
        printf("%2d - ", ptr->data);
        inolder(ptr->right);
    }
}

/*
 二叉树的后序遍历输出
 */
void postolder(btree ptr)
{
    if (ptr)
    {
        postolder(ptr->left);
        postolder(ptr->right);
        printf("%2d - ", ptr->data);
    }
}

int main()
{
    btree root = NULL;
    int value;

    int data[16] = {00,50,40,60,20,45,0,80,10,30,41,46,0,0,70,90};
    //递归创建二叉树
    root = createbtree(data, 1);
    printf("\n-------前序遍历-------\n");
    preolder(root);
    printf("\n-------中序遍历-------\n");
    inolder(root);
    printf("\n-------后序遍历-------\n");
    postolder(root);

    printf("\n请输入想要删除的节点(1-90):");
    scanf_s("%d", &value);

    root = deletenode(root, value);
    printf("删除后树的节点内容为\n");
    printf("\n-------前序遍历-------\n");
    preolder(root);
    printf("\n-------中序遍历-------\n");
    inolder(root);
    printf("\n-------后序遍历-------\n");
    postolder(root);
    printf("\n");
    return 0;
}
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值