• 数据--第31课


    第31课 - 树的存储结构

    小B:线性表可以直接利用内存线性的特性用数组实现树结构是非线性的,肯定不能用数组直接实现吧?

    小A:我觉得链式结构应该可以实现树,但是树中每个结点的孩子又是数目不定的,该如何定义呢?

    小C:我觉得是不是先定义好结点间的关系,再设计结构体呢?

    小D:真的很难哦。

    1. 树的存储结构

    (1)      无法直接用数组表示树的逻辑。

    (2)      但可以设计结构体数组对结点间的关系进行表述。

    (3)      利用链表组织树中的各个结点。

    (4)      链表中的前后关系不代表结点间的逻辑关系。

    (5)      结点的逻辑关系由child数据域描述。

    (6)      child数据域保存其他结点的存储地址。

     

    Index

    Data

    Parent

    Child

    0

    A

    -1

    1,2,3

    1

    B

    0

    4,5

    2

    C

    0

    NULL

    3

    D

    0

    6,7,8

    4

    E

    1

    NULL

    5

    F

    1

    NULL

    6

    H

    3

    NULL

    7

    I

    3

    NULL

    8

    J

    3

    NULL

    思考:

    l  树结构需要添加删除结点,数组存储是否足够灵活,数组存储是否足够灵活?

    l  个结点的子结点可以有多个,如何存储?

    //树结点结构体

    tyedef struct _tag_GTreeNode GTreeNode;

    struct _tag_GTreeNode

    {

             GTreeData* data;

             GTreeNode* parent;

             LinkList* child;

    }   

    //链表结点结构体

    typedef struct _tag_TLNode TLNode;

    struct _tag_TLNode

    {

             LinkListNode header;

             GTreeNode* node;

    }

        

    注意:树结点在链表中的位置不代表树的任何逻辑关系。

     

     

    2. 手把手教你写代码---通用树结构的创建

    main.c

    #include <stdio.h>

    #include "GTree.h"

    /* run this program using the console pauser or add your own getch, system("pause") or input loop */

    void printf_data(GTreeData* data)

    {

        printf("%c", (int)data);

    }

    int main(int argc, char *argv[])

    {

        GTree* tree = GTree_Create();

        int i = 0;   

        GTree_Insert(tree, (GTreeData*)'A', -1);

        GTree_Insert(tree, (GTreeData*)'B', 0);

        GTree_Insert(tree, (GTreeData*)'C', 0);

        GTree_Insert(tree, (GTreeData*)'D', 0);

        GTree_Insert(tree, (GTreeData*)'E', 1);

        GTree_Insert(tree, (GTreeData*)'F', 1);

        GTree_Insert(tree, (GTreeData*)'H', 3);

        GTree_Insert(tree, (GTreeData*)'I', 3);

        GTree_Insert(tree, (GTreeData*)'J', 3);   

        printf("Tree Height: %d ", GTree_Height(tree));

        printf("Tree Degree: %d ", GTree_Degree(tree));

        printf("Full Tree: ");   

        GTree_Display(tree, printf_data, 2, ' ');

        printf("Get Tree Data: ");

        for(i=0; i<GTree_Count(tree); i++)

        {        printf_data(GTree_Get(tree, i));

            printf(" ");

        }

        printf("Get Root Data: ");

        printf_data(GTree_Root(tree));

        printf(" ");

        GTree_Delete(tree, 3); 

        printf("After Deleting D: ");

        GTree_Display(tree, printf_data, 2, '-');

        GTree_Clear(tree);

        printf("After Clearing Tree: ");

        GTree_Display(tree, printf_data, 2, '.');      

        GTree_Destroy(tree);

                 return 0;

    }

    GTree.h

    #ifndef _GTREE_H_

    #define _GTREE_H_

    typedef void GTree;

    typedef void GTreeData;

    typedef void (GTree_Printf)(GTreeData*);

    GTree* GTree_Create();

    void GTree_Destroy(GTree* tree);

    void GTree_Clear(GTree* tree);

    int GTree_Insert(GTree* tree, GTreeData* data, int pPos);

    GTreeData* GTree_Delete(GTree* tree, int pos);

    GTreeData* GTree_Get(GTree* tree, int pos);

    GTreeData* GTree_Root(GTree* tree);

    int GTree_Height(GTree* tree);

    int GTree_Count(GTree* tree);

    int GTree_Degree(GTree* tree);

    void GTree_Display(GTree* tree, GTree_Printf* pFunc, int gap, char div);

    #endif

    Gtree.c

    #include <stdio.h>

    #include <malloc.h>

    #include "GTree.h"

    #include "LinkList.h"

    typedef struct _tag_GTreeNode GTreeNode;

    struct _tag_GTreeNode

    {

        GTreeData* data;

        GTreeNode* parent;

        LinkList* child;

    };

    typedef struct _tag_TLNode TLNode;

    struct _tag_TLNode

    {

        LinkListNode header;

        GTreeNode* node;

    };

    static void recursive_display(GTreeNode* node, GTree_Printf* pFunc, int format, int gap, char div)

    {

        int i = 0;  

        if( (node != NULL) && (pFunc != NULL) )

        {

            for(i=0; i<format; i++)

            {

                printf("%c", div);

            }

            pFunc(node->data);

            printf(" ");

            for(i=0; i<LinkList_Length(node->child); i++)

            {

                TLNode* trNode = (TLNode*)LinkList_Get(node->child, i);

               

                recursive_display(trNode->node, pFunc, format + gap, gap, div);

            }

        }

    }

    static void recursive_delete(LinkList* list, GTreeNode* node)

    {

        if( (list != NULL) && (node != NULL) )

        {

            GTreeNode* parent = node->parent;

            int index = -1;

            int i = 0;

           

            for(i=0; i<LinkList_Length(list); i++)

            {

                TLNode* trNode = (TLNode*)LinkList_Get(list, i);

                

                if( trNode->node == node )

                {

                    LinkList_Delete(list, i);

                   

                    free(trNode);

                   

                    index = i;

                   

                    break;

                }

            }  

            if( index >= 0 )

            { 

                if( parent != NULL )

                {

                     for(i=0; i<LinkList_Length(parent->child); i++)

                     {

                         TLNode* trNode = (TLNode*)LinkList_Get(parent->child, i);

                        

                         if( trNode->node == node )

                         {

                             LinkList_Delete(parent->child, i);

                            

                             free(trNode);

                            

                             break;

                         }

                     }              

                }

               

                while( LinkList_Length(node->child) > 0 )

                {

                    TLNode* trNode = (TLNode*)LinkList_Get(node->child, 0);

                   

                    recursive_delete(list, trNode->node);

                }

                LinkList_Destroy(node->child);

                free(node);

            }

        }

    }

    static int recursive_height(GTreeNode* node)

    {

        int ret = 0;  

        if( node != NULL )

        {

            int subHeight = 0;

            int i = 0;

            for(i=0; i<LinkList_Length(node->child); i++)

            {

                TLNode* trNode = (TLNode*)LinkList_Get(node->child, i);

                subHeight = recursive_height(trNode->node); 

                if( ret < subHeight )

                {

                    ret = subHeight;

                }

            }

           

            ret = ret + 1;

        }

       

        return ret;

    }

    static int recursive_degree(GTreeNode* node)

    {

    int ret = -1;

       

        if( node != NULL )

        {

            int subDegree = 0;

            int i = 0;

           

            ret = LinkList_Length(node->child);

           

            for(i=0; i<LinkList_Length(node->child); i++)

            {

                TLNode* trNode = (TLNode*)LinkList_Get(node->child, i);

               

                subDegree = recursive_degree(trNode->node);

               

                if( ret < subDegree )

                {

                    ret = subDegree;

                }

            }

        }

       

        return ret;

    }

    GTree* GTree_Create()

    {

        return LinkList_Create();

    }

    void GTree_Destroy(GTree* tree)

    {

        GTree_Clear(tree);

        LinkList_Destroy(tree);

    }

    void GTree_Clear(GTree* tree)

    {

         GTree_Delete(tree, 0);

    }

    int GTree_Insert(GTree* tree, GTreeData* data, int pPos)

    {

        LinkList* list = (LinkList*)tree;

        int ret = (list != NULL) && (data != NULL) && (pPos < LinkList_Length(list));

       

        if( ret )

        {

            TLNode* trNode = (TLNode*)malloc(sizeof(TLNode));

            TLNode* cldNode = (TLNode*)malloc(sizeof(TLNode));

            TLNode* pNode = (TLNode*)LinkList_Get(list, pPos);

            GTreeNode* cNode = (GTreeNode*)malloc(sizeof(GTreeNode));

           

            ret = (trNode != NULL) && (cldNode != NULL) && (cNode != NULL);

           

            if( ret )

            {

                cNode->data = data;

                cNode->parent = NULL;

                cNode->child = LinkList_Create();

               

                trNode->node = cNode;

                cldNode->node = cNode;

               

                LinkList_Insert(list, (LinkListNode*)trNode, LinkList_Length(list));

               

                if( pNode != NULL )

                {

                    cNode->parent = pNode->node;

                   

                    LinkList_Insert(pNode->node->child, (LinkListNode*)cldNode, LinkList_Length(pNode->node->child));

                }

            }

            else

            {

                free(trNode);

                free(cldNode);

                free(cNode);

            }

        }

       

        return ret;

    }

    GTreeData* GTree_Delete(GTree* tree, int pos)

    {

        TLNode* trNode = (TLNode*)LinkList_Get(tree, pos);

        GTreeData* ret = NULL;

       

        if( trNode != NULL )

        {

            ret = trNode->node->data;

           

            recursive_delete(tree, trNode->node);

        }

       

        return ret;

    }

    GTreeData* GTree_Get(GTree* tree, int pos)

    {

        TLNode* trNode = (TLNode*)LinkList_Get(tree, pos);

        GTreeData* ret = NULL;

       

        if( trNode != NULL )

        {

            ret = trNode->node->data;

        }

       

        return ret;

    }

    GTreeData* GTree_Root(GTree* tree)

    {

        return GTree_Get(tree, 0);

    }

    int GTree_Height(GTree* tree)

    {

        TLNode* trNode = (TLNode*)LinkList_Get(tree, 0);

        int ret = 0;

       

        if( trNode != NULL )

        {

            ret = recursive_height(trNode->node);

        }

       

        return ret;

    }

    int GTree_Count(GTree* tree)

    {

        return LinkList_Length(tree);

    }

    int GTree_Degree(GTree* tree)

    {

        TLNode* trNode = (TLNode*)LinkList_Get(tree, 0);

        int ret = -1;

       

        if( trNode != NULL )

        {

            ret = recursive_degree(trNode->node);

        }

       

        return ret;

    }

    void GTree_Display(GTree* tree, GTree_Printf* pFunc, int gap, char div)

    {

        TLNode* trNode = (TLNode*)LinkList_Get(tree, 0);

       

        if( (trNode != NULL) && (pFunc != NULL) )

        { 

            recursive_display(trNode->node, pFunc, 0, gap, div);

        }

    }

    LinkList.h

    #ifndef _LINKLIST_H_

    #define _LINKLIST_H_

    typedef void LinkList;

    typedef struct _tag_LinkListNode LinkListNode;

    struct _tag_LinkListNode

    {

        LinkListNode* next;

    };

    LinkList* LinkList_Create();

    void LinkList_Destroy(LinkList* list);

    void LinkList_Clear(LinkList* list);

    int LinkList_Length(LinkList* list);

    int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);

    LinkListNode* LinkList_Get(LinkList* list, int pos);

    LinkListNode* LinkList_Delete(LinkList* list, int pos);

    #endif

    LinkList.c

    #include <stdio.h>

    #include <malloc.h>

    #include "LinkList.h"

    typedef struct _tag_LinkList

    {

        LinkListNode header;

        int length;

    } TLinkList;

    LinkList* LinkList_Create() // O(1)

    {

        TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList));

       

        if( ret != NULL )

        {

            ret->length = 0;

            ret->header.next = NULL;

        }

       

        return ret;

    }

    void LinkList_Destroy(LinkList* list) // O(1)

    {

        free(list);

    }

    void LinkList_Clear(LinkList* list) // O(1)

    {

        TLinkList* sList = (TLinkList*)list;

       

        if( sList != NULL )

        {

            sList->length = 0;

            sList->header.next = NULL;

        }

    }

    int LinkList_Length(LinkList* list) // O(1)

    {

        TLinkList* sList = (TLinkList*)list;

        int ret = -1;

       

        if( sList != NULL )

        {

            ret = sList->length;

        }

       

        return ret;

    }

    int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) // O(n)

    {

        TLinkList* sList = (TLinkList*)list;

        int ret = (sList != NULL) && (pos >= 0) && (node != NULL);

        int i = 0;

       

        if( ret )

        {

            LinkListNode* current = (LinkListNode*)sList;

           

            for(i=0; (i<pos) && (current->next != NULL); i++)

            {

                current = current->next;

            }

           

            node->next = current->next;

            current->next = node;

           

            sList->length++;

        }

       

        return ret;

    }

    LinkListNode* LinkList_Get(LinkList* list, int pos) // O(n)

    {

        TLinkList* sList = (TLinkList*)list;

        LinkListNode* ret = NULL;

        int i = 0;

       

        if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )

        {

            LinkListNode* current = (LinkListNode*)sList;

           

            for(i=0; i<pos; i++)

            {

                current = current->next;

            }

           

            ret = current->next;

        }

       

        return ret;

    }

    LinkListNode* LinkList_Delete(LinkList* list, int pos) // O(n)

    {

        TLinkList* sList = (TLinkList*)list;

        LinkListNode* ret = NULL;

        int i = 0;

       

        if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )

        {

            LinkListNode* current = (LinkListNode*)sList;

           

            for(i=0; i<pos; i++)

            {

                current = current->next;

            }

           

            ret = current->next;

            current->next = ret->next;

           

            sList->length--;

        }

       

        return ret;

    }

    小结:

    l  本节中的树结构是一种通用的数据结构。

    l  利用链表组织树结点:能够便利的存取结点,链表的维护具有一定的复杂性。

    树结构的非线性特性和递归定义的特性是树结构实现难度较大的根本原因。

  • 相关阅读:
    BZOJ 1218: [HNOI2003]激光炸弹( 前缀和 + 枚举 )
    BZOJ 1878: [SDOI2009]HH的项链( BIT )
    BZOJ 1054: [HAOI2008]移动玩具( BFS )
    js-提取行间元素
    vim的三种模式的基本操作
    Linux的高级命令
    Linux的进阶命令
    Linux的基本命令
    Linux常见的文件目录结构
    js-操作属性
  • 原文地址:https://www.cnblogs.com/free-1122/p/11336027.html
Copyright © 2020-2023  润新知