C语言树的编程程序

main主函数:

#include <stdio.h>
#include "tree.h"

int main()
{
	Tree *tree = Tree_Create();
	if (tree == NULL)
		return -1;
	
	Tree_Insert(tree, 'A', -1);
	Tree_Insert(tree, 'B', 0);
	Tree_Insert(tree, 'C', 0);
	Tree_Insert(tree, 'D', 0);
	Tree_Insert(tree, 'E', 1);
	Tree_Insert(tree, 'F', 1);
	Tree_Insert(tree, 'G', 2);
	Tree_Insert(tree, 'H', 3);
	Tree_Insert(tree, 'I', 3);
	Tree_Insert(tree, 'J', 3);
	Display(tree);
	return 0;
}
树的.c文件
#include "tree.h"
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>

Tree* Tree_Create()
{
	// 创建树的结点
	Tree* tree = (Tree*)malloc(sizeof(Tree)/sizeof(char));
	assert(tree);
	
	// 对树的结点参数进行初始化
	tree->len = 0;
	
	// 创建树的链表的头节点
	tree->head = (TreeNode*)malloc(sizeof(TreeNode)/sizeof(char));
	assert(tree->head);
	tree->head->next      = NULL;
	tree->head->parent    = NULL;
	tree->head->childList = NULL;
	
	return tree;
}
/*
初始化新结点参数:
data = data
len = 0;
next  = NULL
childList:新建子节点链表的头节点,初始化相应参数

parent: 
找到双亲结点,如果找到:在双亲结点的子节点链表中新建子节点
子节点参数:next = null  childNode = node; 
将新的子结点尾插进双亲结点的子结点链表当中
双亲结点的度 要 +1
*/
int Tree_Insert(Tree* tree, TreeData data, int pos)
{
	assert(tree != NULL && pos < tree->len);
	
	// 新建树结点
	TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)/sizeof(char));
	assert(node);
	node->data = data;
	node->len  = 0;
	node->next = NULL;
	node->childList = (ChildNode*)malloc(sizeof(ChildNode)/sizeof(char));
	assert(node->childList);
	node->childList->next      = NULL;
	node->childList->childNode = NULL;
	
	
	// 找双亲结点
	TreeNode* parent = tree->head->next;  // 树链表的第0个结点
	int i;
	for (i = 0; i < pos; i++)
	{
		parent = parent->next;
	}
	// 如果双亲结点存在,就加入到双亲结点的子结点链表中
	if (parent != NULL)
	{
		// 创建一个子结点
		ChildNode* childNode = (ChildNode*)malloc(sizeof(ChildNode)/sizeof(char));
		assert(childNode);
		childNode->next      = NULL;
		childNode->childNode = node;
		
		// 将子结点插入到parent的childList中
		ChildNode* tmp = parent->childList;  // 子结点链表的头节点
		while(tmp->next)
			tmp = tmp->next;
		tmp->next = childNode;
		
		parent->len++;
	}
	
	// 将新结点插入到树的链表中
	TreeNode* tmp = tree->head;
	while(tmp->next)
		tmp = tmp->next;
	tmp->next = node;
	
	tree->len++;
	
	return 1;
}
void r_display(TreeNode *node ,int gap)
{
	if(node == NULL)
		return ;
	int i = 0;
	for(i = 0;i < gap;i++)
	{
		printf("%c",'-');
	}
	printf("%c\n",node->data);
	ChildNode *child = node->childList->next;
	while(child)
	{
		r_display(child->childNode,gap+2);
		child = child -> next;
	}
}
void Display(Tree *tree)
{
	assert(tree);
	r_display(tree->head->next,0);
}

树的.h文件
#ifndef __TREE_H__
#define __TREE_H__

struct _treeNode;   // 类型声明
typedef char TreeData;
// 孩子结点类型
typedef struct _childNode
{
	struct _treeNode *childNode;  // 指向孩子结点的指针
	struct _childNode *next;
}ChildNode;

typedef struct _treeNode
{
	TreeData data;
	struct _treeNode *parent;       // 指向双亲结点
	struct _treeNode *next;         // 指向链表的下一个结点
	struct _childNode* childList;   // 指向孩子结点链表头节点的指针
	int len;                        // 孩子结点的个数:度
}TreeNode;

typedef struct _tree
{
	TreeNode *head; // 指向树的链表的头节点
	int len;        // 树中结点的总个数
}Tree;

// 创建通用树
Tree* Tree_Create();

// 树节点插入:pos指双亲结点在链表中的位置,链接中的结点是从0开始
// 也就是说 根节点的位置 是 0
int Tree_Insert(Tree* tree, TreeData data, int pos);
void Display(Tree *tree);
void r_display(TreeNode *node ,int gap);
#endif // __TREE_H__

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值