博客
关于我
C语言实现二叉搜索树
阅读量:243 次
发布时间:2019-03-01

本文共 4872 字,大约阅读时间需要 16 分钟。

二叉搜索树是常见的数据结构,主要用于快速查找数据。以下是对二叉搜索树操作集的实现,包括插入、删除、查找、找最小值和找最大值的函数。

节点结构定义

typedef struct TNode *Position;typedef Position BinTree;struct TNode {    ElementType Data;    BinTree Left;    BinTree Right;};

插入函数

插入函数将一个元素插入二叉搜索树中,并返回根节点指针。

BinTree Insert(BinTree BST, ElementType X) {    if (!BST) {        BST = (BinTree)malloc(sizeof(struct TNode));        BST->Data = X;        BST->Left = NULL;        BST->Right = NULL;    } else {        if (X < BST->Data) {            BST->Left = Insert(BST->Left, X);        } else if (X > BST->Data) {            BST->Right = Insert(BST->Right, X);        }    }    return BST;}

删除函数

删除函数将一个元素从二叉搜索树中删除,并返回根节点指针。如果元素不存在,输出“Not Found”并返回原树根节点。

BinTree Delete(BinTree BST, ElementType X) {    Position Tmp;    if (!BST) {        printf("Not Found\n");        return BST;    } else if (X < BST->Data) {        BST->Left = Delete(BST->Left, X);    } else if (X > BST->Data) {        BST->Right = Delete(BST->Right, X);    } else {        if (BST->Left && BST->Right) {            Tmp = FindMin(BST->Right);            BST->Data = Tmp->Data;            BST->Right = Delete(BST->Right, BST->Data);        } else {            Tmp = BST;            if (!BST->Left) {                BST = BST->Right;            } else if (!BST->Right) {                BST = BST->Left;            }            free(Tmp);        }    }    return BST;}

查找函数

查找函数返回目标值的节点指针,如果不存在则返回空指针。

Position Find(BinTree BST, ElementType X) {    while (BST) {        if (X > BST->Data) {            BST = BST->Right;        } else if (X < BST->Data) {            BST = BST->Left;        } else {            return BST;        }    }    return NULL;}

找最小值函数

找最小值函数返回二叉搜索树中最小值的节点指针。

Position FindMin(BinTree BST) {    while (BST) {        if (!BST->Left) {            return BST;        } else {            BST = BST->Left;        }    }    return NULL;}

找最大值函数

找最大值函数返回二叉搜索树中最大值的节点指针。

Position FindMax(BinTree BST) {    while (BST) {        if (!BST->Right) {            return BST;        } else {            BST = BST->Right;        }    }    return NULL;}

使用示例

#include 
#include
typedef int ElementType;typedef struct TNode *Position;typedef Position BinTree;struct TNode { ElementType Data; BinTree Left; BinTree Right;};BinTree Insert(BinTree BST, ElementType X) { if (!BST) { BST = (BinTree)malloc(sizeof(struct TNode)); BST->Data = X; BST->Left = NULL; BST->Right = NULL; } else { if (X < BST->Data) { BST->Left = Insert(BST->Left, X); } else if (X > BST->Data) { BST->Right = Insert(BST->Right, X); } } return BST;}BinTree Delete(BinTree BST, ElementType X) { Position Tmp; if (!BST) { printf("Not Found\n"); return BST; } else if (X < BST->Data) { BST->Left = Delete(BST->Left, X); } else if (X > BST->Data) { BST->Right = Delete(BST->Right, X); } else { if (BST->Left && BST->Right) { Tmp = FindMin(BST->Right); BST->Data = Tmp->Data; BST->Right = Delete(BST->Right, BST->Data); } else { Tmp = BST; if (!BST->Left) { BST = BST->Right; } else if (!BST->Right) { BST = BST->Left; } free(Tmp); } } return BST;}Position Find(BinTree BST, ElementType X) { while (BST) { if (X > BST->Data) { BST = BST->Right; } else if (X < BST->Data) { BST = BST->Left; } else { return BST; } } return NULL;}Position FindMin(BinTree BST) { while (BST) { if (!BST->Left) { return BST; } else { BST = BST->Left; } } return NULL;}Position FindMax(BinTree BST) { while (BST) { if (!BST->Right) { return BST; } else { BST = BST->Right; } } return NULL;}int main() { BinTree BST, MinP, MaxP, Tmp; ElementType X; int N, i; BST = NULL; scanf("%d", &N); for (i = 0; i < N; i++) { scanf("%d", &X); BST = Insert(BST, X); } printf("preorder: "); preorderTraversal(BST); printf("\n"); MinP = FindMin(BST); MaxP = FindMax(BST); for (i = 0; i < N; i++) { X = ...; Tmp = Find(BST, X); if (Tmp == NULL) { printf("%d is not found\n", X); } else { if (Tmp == MinP) { printf("%d is the smallest key\n", Tmp->Data); } if (Tmp == MaxP) { printf("%d is the largest key\n", Tmp->Data); } } } scanf("%d", &N); for (i = 0; i < N; i++) { X = ...; Tmp = Insert(BST, X); ... } ...}

功能说明

  • 插入函数:递归地将元素插入到正确的位置,确保树的结构。
  • 删除函数:处理三种删除情况,确保树的结构正确性。
  • 查找函数:通过比较节点值,找到目标节点或返回空指针。
  • 找最小值和最大值函数:分别从左下方和右下方遍历,找到叶节点。
  • 这些函数按照二叉搜索树的性质实现,确保插入、删除和查找的效率。

    转载地址:http://gxhv.baihongyu.com/

    你可能感兴趣的文章
    nmon_x86_64_centos7工具如何使用
    查看>>
    NN&DL4.1 Deep L-layer neural network简介
    查看>>
    NN&DL4.3 Getting your matrix dimensions right
    查看>>
    NN&DL4.8 What does this have to do with the brain?
    查看>>
    No 'Access-Control-Allow-Origin' header is present on the requested resource.
    查看>>
    NO 157 去掉禅道访问地址中的zentao
    查看>>
    no available service ‘default‘ found, please make sure registry config corre seata
    查看>>
    no connection could be made because the target machine actively refused it.问题解决
    查看>>
    No Datastore Session bound to thread, and configuration does not allow creation of non-transactional
    查看>>
    No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
    查看>>
    No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
    查看>>
    No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
    查看>>
    No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
    查看>>
    No module named 'crispy_forms'等使用pycharm开发
    查看>>
    No module named cv2
    查看>>
    No module named tensorboard.main在安装tensorboardX的时候遇到的问题
    查看>>
    No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
    查看>>
    No new migrations found. Your system is up-to-date.
    查看>>
    No qualifying bean of type XXX found for dependency XXX.
    查看>>
    No resource identifier found for attribute 'srcCompat' in package的解决办法
    查看>>