zoukankan      html  css  js  c++  java
  • 树和而叉查找树的实现

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




  • 相关阅读:
    MYSQL学习笔记——sql语句优化工具
    SQL优化工具SQLAdvisor使用
    SqlServer性能检测和优化工具使用详细
    Sql优化器究竟帮你做了哪些工作
    通俗易懂的php多线程解决方案
    PHP删除数组中空值的方法介绍
    PHP函数
    python函数回顾:dir()
    面向对象封装思想小结
    python函数回顾:all()
  • 原文地址:https://www.cnblogs.com/james1207/p/3268575.html
Copyright © 2011-2022 走看看