树和二叉查找树的实现

1.树节点的典型声明

typedef struct TreeNode *PtrToNode;

struct TreeNode

{

      ElementType Element;

      PtrToNode     FirstChild;

      PtrToNode     NextSibling;      /* 指向下一个兄弟 */

}

2.而叉查找树的实现

#include <stdio.h>
#include <stdlib.h>

struct TreeNode;
typedef struct TreeNode *SearchTree;
typedef struct TreeNode *Position;

struct TreeNode
{
    int Element;
    SearchTree Left;
    SearchTree Right;
};

/* 清空&初始化树 */
SearchTree MakeEmpty(SearchTree T)
{
    if (T != NULL)
    {   
        MakeEmpty(T->Left);
        MakeEmpty(T->Right);
        free(T);
    }   
    return NULL;
}

/* 在树中查找x */
Position Find(int 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(int x, SearchTree T)
{
    if (T == NULL)
    {
        T = malloc(sizeof(struct TreeNode));
        if (T == NULL)
            printf("Out of space!\n");
        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;
}

/* 在树中查找x,并删除 */
SearchTree Delete(int x, SearchTree T)
{
    Position TmpCell;

    if (T == NULL)
        printf("Element not found!\n");
    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(T->Element, T->Right);
    }
    else
    {
        TmpCell = T;
        if (T->Left == NULL)
            T = T->Right;
        else if (T->Right == NULL)
            T = T->Left;
        free(TmpCell);
    }

    return T;
}

int main()
{
    SearchTree tree = NULL;
    MakeEmpty(tree);
    /* 注意Insert返回值为树 */
    tree = Insert(3, tree);
    tree = Insert(1, tree);
    tree = Insert(2, tree);
    tree = Insert(4, tree);

    Position p;
    p = FindMin(tree);
    printf("Min = %d\n", p->Element);
    p = FindMax(tree);
    printf("Max = %d\n", p->Element);

    p = Find(1, tree);
    if (p != NULL)
    {
        printf("Find %d success!\n", p->Element);
    }
    else
    {
        printf("Do not find in tree!\n");
    }

    tree = Delete(1, tree);

    p = FindMin(tree);
    printf("Min = %d\n", p->Element);

    return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值