该代码实现了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数据结构的更多相关文章

  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. Java基础知识总结(二)

    &和&&的区别: 按位与:a&b是把a和b都转换成二进制数后逐位进行与的运算.若两数字的某位都为1,则该位的运算结果才为1.运算的最终结果是数字. 逻辑与:a& ...

  2. codinglife主题小修改和有意思的博客挂件

    这个主题很漂亮,不过为了迎合自己的喜好ヽ(•̀ω•́ )ゝ,修改了字号.阴影之类的小细节.同时下面还有我博客里面的两个有意思的小挂件,请向右边看(๑و•̀ω•́)و 1.主题修改:复制下面的css代码 ...

  3. phpstorm 同步远程服务器代码

    1.打开view —Toolbar,点击红框中的小工具 2.单击Deployment,在connection中填写对应选项, 其中:type勾选sftp root path :点击后面的Autodet ...

  4. web编程速度大比拼(nodejs go python)(非专业对比)

    C10K问题的解决,涌现出一大批新框架,或者新语言,那么问题来了:到底谁最快呢?非专业程序猿来个非专业对比. 比较程序:输出Hello World! 测试程序:siege –c 100 –r 100 ...

  5. android-SQLite 和 Content

    SQLite 游标(Cursor)相当于指向底层数据中结果集的指针,而不是提取和返回结果值的副本,是在结果集中对位置(行)进行控制的管理方式. moveToFirst:把游标移动到查询结果的第一行 m ...

  6. .net mvc笔记2_Essential C# Features

    Essential C# Features 1.Using Automatically Implemented Properties public class Product { private st ...

  7. python进阶3--文件系统

    文件系统 python的标准库中包括大量工具,可以处理文件系统中的文件,构造和解析文件名,也可以检查文件内容. pyhton表文件名表示为简单的字符串,另外还提供了一些工具,用来由os.path中平台 ...

  8. linux服务器在运行210天左右宕机

    减小字体 增大字体 作者:错新网  来源:www.cuoxin.com  发布时间:2014-2-25 19:21:32 错新网讯   最近几天,一批linux线上的服务器接连宕机,当时以为是硬件问题 ...

  9. Android使用XML全攻略(2)

    Android使用XML全攻略(2)   Android 是针对移动设备的一种新兴的开源操作系统和 SDK.借助它,您可以创建功能强大的移动应用程序.当您的应用程序可以访问 Web 服务时,其吸引力会 ...

  10. ADF 项目创建流程

    ADF 项目创建流程: 1.首先建好应用 2.创建model,UI 3.创建EO,VO,AO, VL 4.设置EO的属性 5.新建lov 6.设置VO的View Accessors,并设置Attrib ...