1、树的创建,用三个结构体:
(1)btree主要用来代表一颗树;
(2)treenode主要用来代表一个一个的树节点;
(3)childnode主要用来代表树节点的子链表(这里把每个树节点的孩子都存放在相印的链表中);
2、输的插入:
创建新的树节点,同时给这个新建的树节点初始化:
(1)初始化自己本身(在树节点链表中通过尾部插入,初始化数据域,初始化指针域);
(2)初始化他的子链表;
(3)找到新建树节点的父亲节点进行插入;
***!!具体插入在什么位置是通过你在主函数传参来实现的;
#include "error.h" //这个是自己写的报错函数头文件名
#include "stdio.h"
#include "stdlib.h"
//**!!此题需要给树链表和孩子链表,创建头节点;
struct _treeNode; //事先申明;
//孩子节点链表的类型;
typedef struct _childNode
{
struct _treeNode *childNode;
struct _childNode *next;
}ChildNode;
//树节点类型
typedef char TreeData;
typedef struct _treeNode
{
TreeData data;
struct _treeNode *parent; //指向父节点的指针
struct _treeNode *next; //指向链表下一个节点的指针
struct _childNode *childList; //孩子链表的头结点;
int degree; //节点的度;
}TreeNode;
typedef struct _tree
{
struct _treeNode *head; //树链表的头结点;(我理解为指向整个树头结点的指针/指向树链表头结点的指针)
int len; //树节点个数;
}Tree;
//定义函数指针类型
typedef void (*TreePrint)(TreeNode* node);
//创建树;
Tree *Create_Tree();
//插入节点
//pos 代表要插入节点父亲节点的位置
//约定:
//1、新插入的节点插入在当前父亲节点所有孩子的右边;
//2、根节点的位置是0;
int Insert_Tree(Tree* tree,TreeData data,int pos);
目前为止都是申明函数,结构体什么;
---------------------------------------------
Tree *Create_Tree()
{
//创建树;
Tree* tree = (Tree*)malloc(sizeof(Tree)/sizeof(char));
if(tree == NULL)
{
errno = MALLOC_ERROR;
return NULL;
}
//给树节点链表创建头结点(给头结点赋值,不是第一个元素)
tree->head = (TreeNode*)malloc(sizeof(TreeNode)/sizeof(char));
if(tree->head == NULL)
{
errno = MALLOC_ERROR;
free(tree);
return NULL;
}
//给树初始化;
tree->head->next = NULL; //代表树中是没有节点的
tree->head->parent = NULL;
tree->head->childList = NULL;
tree->len = 0;
return tree;
}
int Insert_Tree(Tree *tree,TreeData data,int pos)
{
if(tree == NULL || pos < 0|| pos> tree->len)
{
errno = ERROR;
return FALSE;
}
if(pos != 0&& pos == tree->len)
{
errno = ERROR;
return FALSE;
}
//新建节点
TreeNode* node = (TreeNode*)malloc(sizeof(TreeNode)/sizeof(char));
if(node == NULL)
{
errno = MALLOC_ERROR;
return FALSE;
}
node->data = data;
node->next = NULL;
//创建,这个新节点的,孩子链表的头结点
node->childList = (ChildNode*)malloc(sizeof(ChildNode)/sizeof(char));
if(node->childList == NULL)
{
errno = MALLOC_ERROR;
free (node);
return FALSE;
}
node->childList->next = NULL;
node->childList->childNode = NULL;
node->degree = 0;
int i;
//找父亲节点;
TreeNode* parent = tree->head->next;//初始化第一个节点做父节点
for(i = 0;i < pos;i++)
{
parent = parent->next;
}
node->parent = parent;
//在找到的父亲节点的子节点链表中插入子节点;
if(parent != NULL)
{
//创建一个子节点;
ChildNode* childnode = (ChildNode*)malloc(sizeof(ChildNode)/sizeof(char));
if(childnode == NULL)
{
errno = MALLOC_ERROR;
free(node->childList);
free(node);
return FALSE;
}
childnode->next = NULL;
childnode->childNode = node;
//不能从子节点链表第一个元素开始遍历到最后面,第一个元素可能为空;
ChildNode* tmp = parent->childList;
while(tmp->next)
{
tmp = tmp->next;
}
tmp->next = childnode;
//childnode->next = NULL;
parent->degree += 1;
}
TreeNode* tmp = tree->head;
while(tmp->next)
{
tmp = tmp->next;
}
tmp->next = node;
tree->len += 1;
return TRUE;
}
---------------------------------------------------
int main()
{
Tree *tree = Create_Tree();
if(tree == NULL)
{
myError("Create_Tree");
return -1;
}
//插入
Insert_Tree(tree, 'A', 0);
Insert_Tree(tree, 'B', 0);
Insert_Tree(tree, 'C', 0);
Insert_Tree(tree, 'D', 0);
Insert_Tree(tree, 'E', 1);
Insert_Tree(tree, 'F', 1);
Insert_Tree(tree, 'H', 3);
Insert_Tree(tree, 'I', 3);
Insert_Tree(tree, 'J', 3);
Insert_Tree(tree, 'X', 3);
Insert_Tree(tree, 'Z', 8);
}