该代码实现了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]; }
c语言实现tree数据结构
时间: 2024-10-09 21:48:23