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];
}

c语言实现tree数据结构

时间: 2024-08-02 01:13:47

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

java程序代码代写、代写tree数据结构作业

java程序代码代写.代写tree数据结构作业实验三:java面向对象编程一.实验目的及要求1.理解 Java 语言是如何体现面向对象编程基本思想的:2.掌握类的声明以及对象的创建:3.了解类的成员变量和成员方法的特性以及类的构造方法的使用. 4.掌握类变量与实例变量以及类方法和实例方法的区别.二.实验内容1. 编写程序模拟两个村庄共同拥有一片森林.编写一个Village类,该类有一个静态的int型成员变量treeAmount用于模拟森林中树木的数量.在主类MainClass的方法中创建两个村庄

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

table是Lua中唯一的数据结构,其他语言所提供的数据结构,如:arrays.records.lists.queues.sets等,Lua都是通过table来实现,并且在lua中table很好的实现了这些数据结构.--摘自:<Programming in Lua> 看以下代码,可以很清晰的明白Lua中表的使用: -- Lua中的表,table Config = {hello="Hello Lua",world="World"} -- 赋值方式1,以键=

【C语言】【数据结构】菜鸟学习日志(四) 用二叉树实现非递归排序

唉,由于要备战考研,这篇博文可能是我这一年最后一次更新啦! 其实断断续续的也没有写很多,而且大多都是很初级.很简单的东西,没有和大家分享什么高阶的东西.这也正应了我们<菜鸟学习日志>的标题嘛! 不过说回来我还是很喜欢写博文的.一方面总结学到的知识,以后也可以自己看看别做了就忘了:另一方面,写博文也让我在学习的过程中更加认真,以免分享了错误的知识. 写的东西好不好呢是一说,好像有一些点击量,不过看的人估计也不多.只是我还算乐在其中吧! 大学生活说到底过得有点浪了,导致我苦逼地走向了考研的不归路-

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

最近在阅读一个开源的C++代码,里面用到了大量的STL里面的东西.也许是自己一直用C而很少用C++来实现算法的原因,STL里面大量的模板令人心烦.一直对STL的效率表示怀疑,但在网上搜到这样一个帖子,说C的标准库里面快速排序比STL的标准排序要慢!于是,便认真的看了下二者的源码.其实两个使用的算法是完全类似的,而C标准库里面的qsort之所以比std::sort慢,是因为C语言中为了适配所有的数据结构使用了空指针.下面以更为简单的插入排序为例说明这个问题. 插入排序的算法实现代码: void i

R语言学习笔记——数据结构

参考书籍:R语言实战 数据结构: 1. 向量 : 用于存储数值型.字符型或逻辑型数据的一维数组 1.1 创建 : a <- c(1, 2, 3, 4) 1.2 访问 : a[1] : 1 a[c(2, 4)] : 2 4 (向量a中的第二个和第四个元素) a[1:4] : 1 2 3 4 (向量a中的第一个直到第四个元素) 1.3 注意 : 1)  单个向量中的数据必须拥有相同的类型或模式(数值型.字符型或逻辑型) 2) 标量是只含一个元素的向量,例如f <- 3 . g <- &quo

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

#include<stdio.h> #include<stdlib.h> typedef struct Node { int data; struct Node *next; } node; /*初始化链表: 1.首先给头指针分配空间,将头指针赋给temp 2.其次给每一个元素分配空间 3.将内容赋给当前节点的data,NULL赋给当前节点的next指针 4.把当前节点赋给(头指针)上一个节点的next指针 5.上一节点指针后移,准备初始化下个元素 6.最后返回当前链表的头指针*/

C语言动态链表数据结构

链表的操作增删改查 typedef int DATA; struct SNode { DATA data; SNode* pNext; }; SNode* g_head=NULL;//全局变量 //从头部添加 void AddHead(DATA nNum) { SNode* p = (SNode*)malloc(sizeof(SNode));//C语言的方式 //SNode* p = new SNode;//C++ 方式 p->data = nNum; p->pNext = g_pHead;

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

博主最近在考成都大学皇家计算机科学与技术专业,复习专业课数据结构,正好学习到线性结构中的线性表用链表这种存储结构来实现. 首先,数据结构包括1.数据的操作2.逻辑结构3.存储结构(数据结构三要素. 直接上代码,现阶段代码实现功能有:链表初始化.遍历.增.删.返回链表长度,后续功能陆续发布.其中肯定有很多问题,希望各位码哥留言. Linklist* InitList(int i)//i为链表大小 { Linklist *head; head = (Linklist*)malloc(sizeof(L

C语言 排序算法 - 数据结构学习笔记

/**  功能:     排序   *日期:     2017年9月24日   *作者:     yzh   *开发环境:  QT   **/ #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_STACK_LENGTH 100     //非递归快速排序调用栈 #define MAX_LENGTH 20            //数组最大值 //对于数值型 keyType #de