zoukankan      html  css  js  c++  java
  • 二叉查找树(二叉排序树)(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;
    }
  • 相关阅读:
    bzoj 2878: [Noi2012]迷失游乐园
    端口安全检查shell脚本
    Linux安全基线检查脚本
    kali修改时区
    Elasticsearch+Kibana+Logstash安装
    go基础语法
    windows server 2012泛域名解析配置
    域名解析知识总结
    DDOS防护原理
    kali 2.0下搭建DVWA环境
  • 原文地址:https://www.cnblogs.com/wuchanming/p/3806785.html
Copyright © 2011-2022 走看看