資料結構-二分尋找樹(C描述)

來源:互聯網
上載者:User

tree.h
typedef int ElementType;
#ifndef TREE_H_INCLUDED
#define TREE_H_INCLUDED
struct TreeNode;
typedef struct TreeNode *Position;
typedef struct TreeNode *SearchTree;
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);
#endif // TREE_H_INCLUDED

fatal.h
#ifndef FATAL_H_INCLUDED
#define FATAL_H_INCLUDED
#include <stdio.h>
#include <stdlib.h>
#define Error(Str)  FatalError(Str)
#define FatalError(Str)  fprintf(stderr, "%s\n", Str), exit(1)
#endif // FATAL_H_INCLUDED

tree.c
#include "tree.h"
#include "fatal.h"
struct TreeNode
{
    ElementType Element;
    SearchTree  Left;
    SearchTree  Right;
};
/* 相當於disponse */
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;
    else if(T->Left == NULL)
        return T;
    else
        return FindMin(T->Left);
}
/* 非遞迴實現 */
Position FindMax(SearchTree T)
{
    if(T != NULL)
        while(T->Right != NULL)
            T = T->Right;
    return T;
}
SearchTree Insert(ElementType X, SearchTree T)
{
    if(T == NULL)
    {
        /* Create and return a one-node tree */
        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);
    /* Else X is in the tree already; we'll do nothing */
    return T;
}
/* 刪除節點時須注意:始終保持節點在水平線上投影的有序性 */
SearchTree Delete(ElementType X, SearchTree T)
{
    Position TmpCell;
    if(T == NULL)
        Error("Element not found");
    else if(X < T->Element)  /* Go left */
        T->Left = Delete(X, T->Left);
    else if(X > T->Element)  /* Go right */
        T->Right = Delete(X, T->Right);
    else  /* Found element to be deleted */
    {
        if(T->Left && T->Right)  /* Two children */
        {
            /* Replace with smallest in right subtree */
            TmpCell = FindMin(T->Right);
            T->Element = TmpCell->Element;
            T->Right = Delete( T->Element, T->Right );
        }
        else  /* One or zero children */
        {
            TmpCell = T;
            if(T->Left == NULL) /* Also handles 0 children */
                T = T->Right;
            else if(T->Right == NULL)
                T = T->Left;
            free( TmpCell );
        }
    }
    return T;
}
ElementType Retrieve(Position P)
{
    return P->Element;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.