该代码实现了tree的结构。依赖dyArray数据结构。有first一级文件夹。second二级文件夹。

dyArray的c实现參考这里点击打开链接  hashTable的c实现參考这里点击打开链接

以下是跨平台的数据类型定义

  1. //
  2. // cpPlatform.h
  3. // dataStruct
  4. //
  5. // Created by hherima on 14-7-29.
  6. // Copyright (c) 2014年 . All rights reserved.
  7. //
  8.  
  9. #ifndef dataStruct_cpPlatform_h
  10. #define dataStruct_cpPlatform_h
  11.  
  12. enum
  13. {
  14. CP_FALSE = 0,
  15. CP_TRUE = !CP_FALSE
  16. };
  17.  
  18. #define F_MALLOC_TYPE(s) (s*)f_malloc(sizeof(s))
  19. #define FREEFUN free
  20. #define MIN_PRE_ALLOCATE_SIZE 10 //The initial size of the dynamic array.
  21. #define MEMSETFUN memset
  22. #define REALLOCFUN realloc
  23. #define MALLOCFUN malloc
  24. #define MEMCMPFUN memcmp
  25. #define MEMCPYFUN memcpy
  26.  
  27. typedef unsigned char cp_bool;
  28. typedef signed int cp_int32;
  29. typedef char cp_int8;
  30. typedef unsigned int cp_uint32;
  31.  
  32. #endif
  1. //
  2. // treeStruct.h
  3. // dataStruct
  4. //
  5. // Created by hherima on 14-8-1.
  6. // Copyright (c) 2014年 . All rights reserved.
  7. //
  8.  
  9. #ifndef dataStruct_treeStruct_h
  10. #define dataStruct_treeStruct_h
  11.  
  12. #include <stdlib.h>
  13. #include "cpPlatform.h"
  14. #include "dyArray.h"
  15.  
  16. struct firstnode;
  17. struct secondnode;
  18. struct tree;
  19.  
  20. enum nodetype //tree节点类型
  21. {
  22. second_type_node,
  23. first_type_node
  24. };
  25.  
  26. struct firstnode
  27. {
  28. void** pfirst;
  29. struct DynamicArray *second_array;
  30. void *puiData;
  31. cp_bool flag_expand; //标志该组是否展开
  32. };
  33.  
  34. struct TreeNode
  35. {
  36. enum nodetype pnode_t; //用来记录该节点为first或者是second
  37. void *nodedata; //该指针实际应该为firstnode或者secondnode,应依据nodetype强转加以使用
  38. };
  39.  
  40. struct tree
  41. {
  42. /*struct Iterator_trees_fromcur_skipmode */void *piterator_fromcur_skipmode;
  43. /*struct Iterator_trees_fromhead_skipmode*/void *piterator_fromhead_skipmode;
  44. /*struct Iterator_trees_fromhead_holemode*/void *piterator_fromhead_holemode;
  45.  
  46. #ifdef FET_ITERATOR_EXTEND
  47. /*struct Iterator_trees_fromcur_holemode*/void *piterator_fromcur_holemode;
  48. /*struct Iterator_trees_fromcur_first*/void *piterator_fromcur_first;
  49. /*struct Iterator_trees_fromhead_first*/void *piterator_fromhead_first;
  50. /*struct Iterator_trees_fromcur_skipmode_wm*/void *piterator_fromcur_skipmode_wm;
  51. /*struct Iterator_trees_fromcur_holdmode_wm*/void *piterator_fromcur_holemode_wm;
  52. /*struct Iterator_trees_fromcur_first_wm*/void *piterator_fromcur_first_wm;
  53. #endif
  54. struct DynamicArray *first_array;
  55. cp_int32 firstIndex; //该second所在组在整个组动态数组中的index
  56. cp_int32 secondIndex; //该second所在second动态数组中的index
  57. void * pCursorfirstNode;
  58. void * pCursorsecondNode;
  59. };
  60.  
  61. enum travmode //遍历模式
  62. {
  63. skipModeFlag, //跳跃闭合组模式
  64. wholeModeFlag, //全节点遍历模式(不区分闭合组)
  65. };
  66.  
  67. //为树上的节点申请空间
  68. cp_bool create_first_array(struct tree *c_tree,DataDestroyFunc data_destroy);
  69.  
  70. cp_bool create_second_array(struct firstnode *first_node,DataDestroyFunc data_destroy);
  71.  
  72. cp_bool append_first_ele(struct tree *c_tree, struct firstnode *first_node);
  73.  
  74. cp_bool append_second_ele(struct firstnode *first_node, struct secondnode *second_node);
  75.  
  76. cp_bool insert_first_ele(struct tree *c_tree, struct firstnode *first_node, cp_int32 insert_pos);
  77.  
  78. cp_bool insert_second_ele(struct firstnode *first_node, struct secondnode *second_node, cp_int32 insert_pos);
  79.  
  80. cp_bool ThisIsSelectedNode(struct tree *theTree, void *pNode);
  81.  
  82. void *get_focus_first(struct tree *theTree);
  83.  
  84. #endif
  1. //
  2. // treeStruct.c
  3. // dataStruct
  4. //
  5. // Created by hherima on 14-8-1.
  6. // Copyright (c) 2014年 . All rights reserved.
  7. //
  8. #include "treeStruct.h"
  9.  
  10. cp_bool create_first_array(struct tree *c_tree,DataDestroyFunc data_destroy)
  11. {
  12. if( c_tree == NULL )
  13. return CP_FALSE;
  14. c_tree->first_array = DyArrayCreate(data_destroy);
  15. if(c_tree->first_array == NULL)
  16. return CP_FALSE;
  17. else
  18. return CP_TRUE;
  19. }
  20.  
  21. cp_bool create_second_array(struct firstnode *first_node,DataDestroyFunc data_destroy)
  22. {
  23. if(first_node == NULL)
  24. return CP_FALSE;
  25. first_node->second_array = DyArrayCreate(data_destroy);
  26. if(first_node->second_array == NULL)
  27. return CP_FALSE;
  28. else
  29. return CP_TRUE;
  30. }
  31.  
  32. cp_bool append_first_ele( struct tree *c_tree, struct firstnode *first_node )
  33. {
  34. if( c_tree == NULL || first_node == NULL)
  35. {
  36. return CP_FALSE;
  37. }
  38.  
  39. if( !DyArrayAppend(c_tree->first_array, first_node) )
  40. return CP_FALSE;
  41. else
  42. {
  43. return CP_TRUE;
  44. }
  45. }
  46.  
  47. cp_bool append_second_ele(struct firstnode *first_node, struct secondnode *second_node)
  48. {
  49. if( first_node == NULL || second_node == NULL)
  50. {
  51. return CP_FALSE;
  52. }
  53. if( !DyArrayAppend(first_node->second_array, second_node))
  54. return CP_FALSE;
  55. else
  56. {
  57. return CP_TRUE;
  58. }
  59. }
  60.  
  61. cp_bool insert_first_ele(struct tree *c_tree, struct firstnode *first_node, cp_int32 insert_pos)
  62. {
  63. if( first_node == NULL || c_tree == NULL)
  64. {
  65. return CP_FALSE;
  66. }
  67. if(!DyArrayInsert(c_tree->first_array, insert_pos, first_node))
  68. return CP_FALSE;
  69. else
  70. return CP_TRUE;
  71. }
  72.  
  73. cp_bool insert_second_ele(struct firstnode *first_node, struct secondnode *second_node, cp_int32 insert_pos)
  74. {
  75. if( first_node == NULL || second_node == NULL)
  76. {
  77. return CP_FALSE;
  78. }
  79. if(!DyArrayInsert(first_node->second_array, insert_pos, second_node))
  80. return CP_FALSE;
  81. else
  82. return CP_TRUE;
  83. }
  84.  
  85. void traversal_tree(struct tree *theTree)
  86. {
  87. cp_int32 i = 0, j = 0;
  88. cp_int32 first_num = 0, second_num = 0;
  89. struct firstnode *pcurfirst;
  90. struct secondnode *pcursecond;
  91. first_num = theTree->first_array->m_nSize;
  92. while(i < first_num)
  93. {
  94. pcurfirst = (struct firstnode*)(theTree->first_array->m_ppData[i++]);
  95. // visit(pcurfirst);
  96. j = 0;
  97. second_num = pcurfirst->second_array->m_nSize;
  98. while(j < second_num)
  99. {
  100. pcursecond = (struct secondnode*)(pcurfirst->second_array->m_ppData[j++]);
  101. // visit(pcursecond);
  102. }
  103. }
  104. //遍历结束的回调
  105. }
  106.  
  107. void traversal_firstnode(struct tree *theTree)
  108. {
  109. cp_int32 i = 0;
  110. cp_int32 first_num = 0;
  111. struct firstnode *pcurfirst;
  112. first_num = theTree->first_array->m_nSize;
  113. while(i < first_num)
  114. {
  115. pcurfirst = (struct firstnode*)(theTree->first_array->m_ppData[i++]);
  116. // visit(pcurfirst);
  117. }
  118. //遍历结束的回调
  119. }
  120.  
  121. cp_bool ThisIsSelectedNode(struct tree *theTree, void *pNode)
  122. {
  123. if(theTree->secondIndex == -1)
  124. {
  125. if(theTree->first_array->m_ppData[theTree->firstIndex] == pNode)
  126. {
  127. return CP_TRUE;
  128. }
  129. else
  130. {
  131. return CP_FALSE;
  132. }
  133. }
  134. else
  135. {
  136. struct firstnode *first_node = NULL;
  137. first_node = theTree->first_array->m_ppData[theTree->firstIndex];
  138. if(first_node->second_array->m_ppData[theTree->secondIndex] == pNode)
  139. {
  140. return CP_TRUE;
  141. }
  142. else
  143. {
  144. return CP_FALSE;
  145. }
  146. }
  147. }
  148.  
  149. void *get_focus_first(struct tree *theTree)
  150. {
  151. if(theTree == NULL)
  152. return NULL;
  153. if(theTree->first_array == NULL)
  154. return NULL;
  155. return theTree->first_array->m_ppData[theTree->firstIndex];
  156. }

c语言实现tree数据结构的更多相关文章

  1. 1. C语言中的数据结构.md

    C语言内建数据结构类型 整型 整型数据是最基本的数据类型,不过从整形出发衍生出好几种integer-like数据结构,譬如字符型,短整型,整型,长整型.他们都是最基本的方式来组织的数据结构,一般是几位 ...

  2. Cocos2d-x 脚本语言Lua基本数据结构-表(table)

    Cocos2d-x 脚本语言Lua基本数据结构-表(table) table是Lua中唯一的数据结构.其它语言所提供的数据结构,如:arrays.records.lists.queues.sets等. ...

  3. C语言实现通用数据结构的高效设计

    近期在阅读一个开源的C++代码.里面用到了大量的STL里面的东西.或许是自己一直用C而非常少用C++来实现算法的原因.STL里面大量的模板令人心烦.一直对STL的效率表示怀疑,但在网上搜到这样一个帖子 ...

  4. C语言运行时数据结构

    段(Segment): 对象文件/可执行文件: SVr4 UNIX上被称为ELF(起初"Extensible Linker Format", 现在"Executable ...

  5. C语言实现常用数据结构——链表

    #include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; ...

  6. R语言数据类型与数据结构

    一.数据类型 5种 1.character 字符 2.numeric 数值 3.integer 整数 一般数字的存储会默认为数值类型,如果要强调是整数,需要在变量值后面加上 L. x <- 5L ...

  7. C语言动态链表数据结构

    链表的操作增删改查 typedef int DATA; struct SNode { DATA data; SNode* pNext; }; SNode* g_head=NULL;//全局变量 //从 ...

  8. C语言实现常用数据结构——二叉树

    #include<stdio.h> #include<stdlib.h> #define SIZE 10 typedef struct Tree { int data; str ...

  9. C语言 严蔚敏数据结构 线性表之链表实现

    博主最近在考成都大学皇家计算机科学与技术专业,复习专业课数据结构,正好学习到线性结构中的线性表用链表这种存储结构来实现. 首先,数据结构包括1.数据的操作2.逻辑结构3.存储结构(数据结构三要素. 直 ...

随机推荐

  1. Struts学习之自定义拦截器

    * 所有的拦截器都需要实现Interceptor接口或者继承Interceptor接口的扩展实现类    * 要重写init().intercept().destroy()方法        * in ...

  2. .Net页面缓存OutPutCachexian详解

    一 它在Web.Config中的位置 <system.web> <!--页面缓存--> <caching> <outputCacheSettings> ...

  3. Gow工具

    一 Gow 是什么 Gow (Gnu On Windows) is the lightweight alternative to Cygwin. It uses a convenient NSIS i ...

  4. shell实现死循环

    参考自http://codingstandards.iteye.com/blog/780524 .while true do command; done .while : do command; do ...

  5. Spring-AOP实践

    Spring-AOP实践 公司的项目有的页面超级慢,20s以上,不知道用户会不会疯掉,于是老大说这个页面要性能优化.于是,首先就要搞清楚究竟是哪一步耗时太多. 我采用spring aop来统计各个阶段 ...

  6. PCB成型製程介紹

    PCB成型製程在電子構裝中所扮演的角色 下圖是電腦主機的內部組成 我們將以插在主機板上的一片 USB擴充卡來說明PCB成型製 程在電子構裝中所扮演的角色 PCB成型製程的子製程 USB擴充卡要插入主機 ...

  7. ubuntu openStack icehouse dashboard theme自定义

    1,ubuntu openStack 语言包locate

  8. <td style="word-break:break-all"> 在html中控制自动换行

    在html中控制自动换行   其实只要在表格控制中添加一句 <td style="word-break:break-all">就搞定了. 其中可能对英文换行可能会分开一 ...

  9. Cocos2d-x:环境配置小节

    一.准备 须要下载下面内容. 1. vs2010 下载地址:http://download.microsoft.com/download/1/4/3/143B7583-6225-474F-88D5-5 ...

  10. python中变量命名

    一 综述:  二 全局变量(包含函数和类): (1)正常变量x: *通过module.x能够使用. *通过from module import *能够使用. (2)以"_"开头变量 ...