1. 程式人生 > >c語言實現tree資料結構

c語言實現tree資料結構

該程式碼實現了tree的結構,依賴dyArray資料結構。有first一級目錄,second二級目錄。

dyArray的c實現參考這裡點選開啟連結  hashTable的c實現參考這裡點選開啟連結

下面是跨平臺的資料型別定義

//
//  cpPlatform.h
//  dataStruct
//
//  Created by hherima on 14-7-29.
//  Copyright (c) 2014年 . All rights reserved.
//

#ifndef dataStruct_cpPlatform_h
#define dataStruct_cpPlatform_h



enum
{
    CP_FALSE  =   0,
    CP_TRUE    =  !CP_FALSE
};


#define  F_MALLOC_TYPE(s) (s*)f_malloc(sizeof(s))
#define  FREEFUN free
#define  MIN_PRE_ALLOCATE_SIZE 10  //The initial size of the dynamic array.
#define  MEMSETFUN      memset
#define  REALLOCFUN     realloc
#define  MALLOCFUN      malloc
#define  MEMCMPFUN      memcmp
#define  MEMCPYFUN      memcpy



typedef unsigned char cp_bool;
typedef signed int cp_int32;
typedef char cp_int8;
typedef unsigned int cp_uint32;


#endif


//
//  treeStruct.h
//  dataStruct
//
//  Created by hherima on 14-8-1.
//  Copyright (c) 2014年 . All rights reserved.
//

#ifndef dataStruct_treeStruct_h
#define dataStruct_treeStruct_h

#include <stdlib.h>
#include "cpPlatform.h"
#include "dyArray.h"

struct firstnode;
struct secondnode;
struct tree;

enum nodetype	//tree節點型別
{
	second_type_node,
	first_type_node
};

struct firstnode
{
    void**  pfirst;
    struct DynamicArray *second_array;
    void *puiData;
    cp_bool flag_expand;    //標誌該組是否展開
};


struct TreeNode
{
    enum nodetype pnode_t;         //用來記錄該節點為first或者是second
    void *nodedata;        //該指標實際應該為firstnode或者secondnode,應根據nodetype強轉加以使用
};

    
    struct tree
    {
        /*struct Iterator_trees_fromcur_skipmode */void *piterator_fromcur_skipmode;
        /*struct Iterator_trees_fromhead_skipmode*/void *piterator_fromhead_skipmode;
        /*struct Iterator_trees_fromhead_holemode*/void *piterator_fromhead_holemode;
        
#ifdef FET_ITERATOR_EXTEND
        /*struct Iterator_trees_fromcur_holemode*/void *piterator_fromcur_holemode;
        /*struct Iterator_trees_fromcur_first*/void *piterator_fromcur_first;
        /*struct Iterator_trees_fromhead_first*/void *piterator_fromhead_first;
        /*struct Iterator_trees_fromcur_skipmode_wm*/void *piterator_fromcur_skipmode_wm;
        /*struct Iterator_trees_fromcur_holdmode_wm*/void *piterator_fromcur_holemode_wm;
        /*struct Iterator_trees_fromcur_first_wm*/void *piterator_fromcur_first_wm;
#endif
        struct DynamicArray *first_array;
        cp_int32 firstIndex;  //該second所在組在整個組動態陣列中的index
        cp_int32 secondIndex; //該second所在second動態陣列中的index
        void * pCursorfirstNode;
        void * pCursorsecondNode;
    };
    
    enum travmode	//遍歷模式
    {
        skipModeFlag,	//跳躍閉合組模式
        wholeModeFlag,	//全節點遍歷模式(不區分閉合組)
    };
    
    //為樹上的節點申請空間
    cp_bool  create_first_array(struct tree *c_tree,DataDestroyFunc data_destroy);
    
    cp_bool  create_second_array(struct firstnode *first_node,DataDestroyFunc data_destroy);
    
    cp_bool	append_first_ele(struct tree *c_tree, struct firstnode *first_node);
    
    cp_bool	append_second_ele(struct firstnode *first_node, struct secondnode *second_node);
    
    cp_bool	insert_first_ele(struct tree *c_tree, struct firstnode *first_node, cp_int32 insert_pos);
    
    cp_bool	insert_second_ele(struct firstnode *first_node, struct secondnode *second_node, cp_int32 insert_pos);
    
    cp_bool ThisIsSelectedNode(struct tree *theTree, void *pNode);
    
    void *get_focus_first(struct tree *theTree);


#endif

//
//  treeStruct.c
//  dataStruct
//
//  Created by hherima on 14-8-1.
//  Copyright (c) 2014年 . All rights reserved.
//
#include "treeStruct.h"

cp_bool  create_first_array(struct tree *c_tree,DataDestroyFunc data_destroy)
{
    if( c_tree == NULL )
        return CP_FALSE;
    c_tree->first_array = DyArrayCreate(data_destroy);
    if(c_tree->first_array == NULL)
        return CP_FALSE;
    else
        return CP_TRUE;
}

cp_bool create_second_array(struct firstnode *first_node,DataDestroyFunc data_destroy)
{
    if(first_node == NULL)
        return CP_FALSE;
    first_node->second_array = DyArrayCreate(data_destroy);
    if(first_node->second_array == NULL)
        return CP_FALSE;
    else
        return CP_TRUE;
}

cp_bool    append_first_ele( struct tree *c_tree, struct firstnode *first_node )
{
    if( c_tree == NULL || first_node == NULL)
    {
        return CP_FALSE;
    }
    
    if( !DyArrayAppend(c_tree->first_array, first_node) )
        return CP_FALSE;
    else
    {
        return CP_TRUE;
    }
}

cp_bool    append_second_ele(struct firstnode *first_node, struct secondnode *second_node)
{
    if( first_node == NULL || second_node == NULL)
    {
        return CP_FALSE;
    }
    if( !DyArrayAppend(first_node->second_array, second_node))
        return CP_FALSE;
    else
    {
        return CP_TRUE;
    }
}

cp_bool    insert_first_ele(struct tree *c_tree, struct firstnode *first_node, cp_int32 insert_pos)
{
    if( first_node == NULL || c_tree == NULL)
    {
        return CP_FALSE;
    }
    if(!DyArrayInsert(c_tree->first_array, insert_pos, first_node))
        return CP_FALSE;
    else
        return CP_TRUE;
}


cp_bool    insert_second_ele(struct firstnode *first_node, struct secondnode *second_node, cp_int32 insert_pos)
{
    if( first_node == NULL || second_node == NULL)
    {
        return CP_FALSE;
    }
    if(!DyArrayInsert(first_node->second_array, insert_pos, second_node))
        return CP_FALSE;
    else
        return CP_TRUE;
}



void traversal_tree(struct tree *theTree)
{
    cp_int32 i = 0, j = 0;
    cp_int32 first_num = 0, second_num = 0;
    struct firstnode *pcurfirst;
    struct secondnode *pcursecond;
    first_num = theTree->first_array->m_nSize;
    while(i < first_num)
    {
        pcurfirst = (struct firstnode*)(theTree->first_array->m_ppData[i++]);
        // visit(pcurfirst);
        j = 0;
        second_num = pcurfirst->second_array->m_nSize;
        while(j < second_num)
        {
            pcursecond = (struct secondnode*)(pcurfirst->second_array->m_ppData[j++]);
            // visit(pcursecond);
        }
    }
    //遍歷結束的回撥
}

void traversal_firstnode(struct tree *theTree)
{
    cp_int32 i = 0;
    cp_int32 first_num = 0;
    struct firstnode *pcurfirst;
    first_num = theTree->first_array->m_nSize;
    while(i < first_num)
    {
        pcurfirst = (struct firstnode*)(theTree->first_array->m_ppData[i++]);
        // visit(pcurfirst);
    }
    //遍歷結束的回撥
}

cp_bool ThisIsSelectedNode(struct tree *theTree, void *pNode)
{
	if(theTree->secondIndex == -1)
	{
		if(theTree->first_array->m_ppData[theTree->firstIndex] == pNode)
		{
			return CP_TRUE;
		}
		else
		{
			return CP_FALSE;
		}
	}
	else
	{
		struct firstnode *first_node = NULL;
		first_node = theTree->first_array->m_ppData[theTree->firstIndex];
		if(first_node->second_array->m_ppData[theTree->secondIndex] == pNode)
		{
			return CP_TRUE;
		}
		else
		{
			return CP_FALSE;
		}
	}
}

void *get_focus_first(struct tree *theTree)
{
	if(theTree == NULL)
		return NULL;
	if(theTree->first_array == NULL)
		return NULL;
	return theTree->first_array->m_ppData[theTree->firstIndex];
}