• 树和而叉查找树的实现


    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!
    ");
            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!
    ");
        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
    ", p->Element);
        p = FindMax(tree);
        printf("Max = %d
    ", p->Element);
    
        p = Find(1, tree);
        if (p != NULL)
        {
            printf("Find %d success!
    ", p->Element);
        }
        else
        {
            printf("Do not find in tree!
    ");
        }
    
        tree = Delete(1, tree);
    
        p = FindMin(tree);
        printf("Min = %d
    ", p->Element);
    
        return 0;
    }




  • 相关阅读:
    Windows更改默认RDP端口
    npm
    virtbuilder、oz
    brctl 详细使用
    Linux 精心设计的操作
    Oracle 11.2.0.1.0 CRS4639: Could not contact Oracle High Availability Services
    Oracle 11g 数据库启动时实例恢复的背后
    RHEL Debian Repository Configuration
    C#编码规范2
    .net二维码图片生成,并在中间添加LOGO,附base64图片下载功能
  • 原文地址:https://www.cnblogs.com/james1207/p/3268575.html
Copyright © 2020-2023  润新知