• 二叉查找树(二叉排序树)(C语言)


    #include<stdio.h>
    #include "fatal.h"
    struct TreeNode;
    typedef struct TreeNode *Position;
    typedef struct TreeNode *SearchTree;
    typedef int ElementType;
    SearchTree MakeEmpty(SearchTree T);
    Position Find(ElementType X,SearchTree T);
    Position FindMin(SearchTree T);
    Position FindMax(SearchTree T);
    SearchTree Insert(ElementType X,SearchTree T);
    SearchTree Delete(ElementType X,SearchTree T);
    ElementType Retrieve(Position P);
    struct TreeNode 
    {
        ElementType Element;
        SearchTree left;
        SearchTree right;
    };
    
    SearchTree MakeEmpty(SearchTree T)
    {
        if(T!=NULL)
        {
            MakeEmpty(T->left);
            MakeEmpty(T->right);
            free(T);
        }
        return NULL;
    }
    
    Position Find(ElementType X,SearchTree T)
    {
        if(T==NULL)
            return NULL;
        if(X<T->Element)
            return Find(X,T->left);
        else if(X>T->Element)
            return Find(X,T->right);
        else 
            return T;
    }
    
    Position FindMin(SearchTree T)
    {
        if(T==NULL)
            return NULL;
        if(T->left==NULL)
            return T;
        else
            return FindMin(T->left);
    }
    
    Position FindMax(SearchTree T)
    {
        if(T==NULL)
            return NULL;
        else if(T->right==NULL)
            return T;
        else
            return FindMax(T->right);
    }
    
    SearchTree Insert(ElementType X,SearchTree T)
    {
        if(T==NULL)
        {
            T=malloc(sizeof(struct TreeNode));
            if(T==NULL)
                FatalError("Out of space!!!");
            else
            {
                T->Element=X;
                T->left=T->right=NULL;
            }
        }
        else if(X<T->Element)
            T->left=Insert(X,T->left);
        else if(X>T->Element)
            T->right=Insert(X,T->right);
        return T;
    }
    
    SearchTree Delete(ElementType X,SearchTree T)
    {
        Position TmpCell;
        if(T==NULL)
            Error("Error not found");
        else if(X<T->Element)
            T->left=Delete(X,T->left);
        else if(X>T->Element)
            T->right=Delete(X,T->right);
        else if(T->left&&T->right)
        {
            TmpCell=FindMin(T->right);
            T->Element=TmpCell->Element;
            T->right=Delete(X,T->right);
        }
        else
        {
            TmpCell=T;
            if(T->left==NULL)
                T=T->right;
            else if(T->right=NULL)
                T=T->left;
            free(TmpCell);
        }
        return T;
    }
    ElementType Retrieve(Position P)
    {
        if(P==NULL)
            return -1;
        else
            return P->Element;
    }
  • 相关阅读:
    如何添加和删除本地存储中的数据?
    本地存储和cookies之间的区别是什么?
    那么如何使用WebSQL?
    WebSQL是HTML 5规范的一部分吗?
    WebSQL是什么?
    什么是本地存储的生命周期?
    本地存储和cookies之间的区别是什么?
    什么是多线程中的上下文切换?
    web workers是什么,为什么我们需要web workers?
    连接点?
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3806785.html
Copyright © 2020-2023  润新知