• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
james1207

博客园    首页    新随笔    联系   管理    订阅  订阅

树和而叉查找树的实现

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;
}




 

 

posted @ 2013-08-19 19:24  Class Xman  阅读(234)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3