#ifndef _TREE_H_ #define _TREE_H_ #include "error.h" #define false 0 #define true 1 typedef _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 要插入父结点 int Insert_Tree(Tree *tree,TreeData data,int pos); //显示树 void Display (Tree *tree,TreePrint pFunc); //移除结点 //int R_Delete (Tree *tree,TreeNode *node); //删除指定位置结点 int Delete(Tree *tree,int pos,TreeData *x); //获取树的结点 int Get_Tree(Tree *tree,int pos,TreeData *x); //清楚所有结点 int Tree_Clear(Tree *tree); //销毁树 void Destroy(Tree *tree); //获取根节点 TreeNode *Tree_root(Tree *tree); //树链表的长度 int Tree_Count(Tree *tree); //结点的高度 //int R_Tree_Height(TreeNode *node); //树的高度 int Tree_Height(Tree *tree); //结点的度(后继数量) //int R_Degree(TreeNode *tree); //树的度 int Degree(Tree *tree); #endif //_TREE_H_ //tree.c函数源代码 #include "tree.h" #include <stdlib.h> Tree *Create_Tree() { //创建树结点 Tree *tree = (Tree *)malloc(sizeof(Tree) / sizeof(char)); if(tree == NULL) { errno = MALLOC_ERROR; return false; } //创建树结点链表的头结点 tree->head = (TreeNode *)malloc(sizeof(TreeNode) / sizeof(char)); if(tree->head == NULL) { errno = MALLOC_ERROR; free(tree); return NULL; } //给元素赋值 tree->head->parent = NULL; tree->head->next = NULL; tree->head->childlist = NULL;//置空,树中没有结点 tree->len = 0; return tree; } // pos 代表要插入结点父亲结点的位置 int Insert_Tree(Tree *tree,TreeData data,int pos) { if(tree == NULL || pos < 0 || pos > tree->len) { errno = ERROR; return false; } if(pos != 0 && tree->len == pos) { 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结点的孩子结点链表的头结点 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->childNode = node; childnode->next = NULL; //把childnode加入到父结点node的孩子链表中 ChildNode *tmp = parent->childlist;//孩子链表的头结点 while(tmp->next) tmp = tmp->next; tmp->next = childnode; parent->degree += 1;//后继(度)加一 } TreeNode *tmp = tree->head;//树结点链表的头结点 while(tmp->next) tmp = tmp->next; tmp->next = node; tree->len += 1; return true; } void R_Display(TreeNode *node,int gap,TreePrint pFunc) //递归打印结点 { if(node == NULL) return; //打印距离前一个结点的距离 int i; for(i = 0;i < gap;i++) { printf("%c",'-'); } //打印结点自己 pFunc(node); ChildNode *child = node->childlist->next;//该节点的第一个孩子结点 //用递归打印结点孩子 while(child) { R_Display (child->childNode,gap+4,pFunc); child = child->next; } } void Display(Tree *tree,TreePrint pFunc)//打印树 { if(tree == NULL) return; R_Display(tree->head->next,0,pFunc);//先打印根节点 } int R_Delete (Tree *tree,TreeNode *node)//移除结点 { if(tree == NULL || node == NULL) { return; } //先从树链表中移除结点,找node的前一个结点 TreeNode *tmp = tree->head->next; while(tmp) { if(tmp == node) { tmp = node->next; (tree->len)--; } tmp = tmp->next; } //将父结点中子节点链表中的node的结点移除 TreeNode *parent = node->parent; if(parent != NULL) { ChildNode *tmp = parent->childlist;//子结点链表的头结点 while(tmp->next) { if(tmp->next->childNode == node) { ChildNode *p = tmp->next; tmp->next = p->next; free(p); parent->degree--; break; } tmp = tmp->next; } } //将node结点的孩子结点删掉 ChildNode *child = node->childlist->next; //孩子链表的第一个结点 while(child) { ChildNode *tmp = child->next; R_Delete(tree,child->childNode); child = tmp; } free(node->childlist); free(node); } int Delete(Tree *tree,int pos,TreeData *x)//删除指定位置结点 { if(tree == NULL || pos < 0 || pos > tree->len) { errno = ERROR; return false; } if(pos != 0 && tree->len == pos) { errno = ERROR; return false; } //找结点 int i; TreeNode *current = tree->head->next;//当前树链表第一个结点 for(i = 0; i < pos;i++) { current = current->next; } *x = current->data; R_Delete(tree,current); return true; } int Get_Tree(Tree *tree,int pos,TreeData *x) //获取树的结点 { if(tree == NULL || pos < 0 || pos > tree->len) { errno = ERROR; return false; } if(pos != 0 && tree->len == pos) { errno = ERROR; return false; } int i; // 找结点 TreeNode* current = tree->head->next; for (i = 0; i < pos; i++) { current = current->next; } *x = current->data;//获取结点数据 return true; } int Tree_Clear(Tree *tree)//清楚所有结点 { if(tree = NULL) { errno = ERROR; return false; } TreeData x; Delete(tree,0,&x); return true; } void Destroy(Tree *tree)//销毁树 { if(tree = NULL) { errno = ERROR; return ; } Tree_Clear(tree);//清除所有结点在释放掉 free(tree->head); free(tree); return; } TreeNode *Tree_root(Tree *tree) //获取根节点 { if(tree == NULL) { errno = ERROR; return NULL; } return tree->head->next; } int Tree_Count(Tree *tree) // 树链表的长度 { if(tree = NULL) { errno = ERROR; return false; } return tree->len; } int R_Tree_Height(TreeNode *node) //结点的高度 { if(node == NULL) { return false; } int subheight = 0; int max = 0; ChildNode *child = node->childlist->next; while(child) { subheight = R_Tree_Height(child->childNode); if(subheight > max) max = subheight; child = child->next; } return max + 1; } int Tree_Height(Tree *tree) //树的高度 { if (tree == NULL) { errno = ERROR; return FALSE; } int height = R_Tree_Height(tree->head->next); return height; } int R_Degree(TreeNode *node) //结点的度(后继数量) { if(node == NULL) { return false; } int max = node->degree; int subdegree = 0; ChildNode *child = node->childlist->next; while(child) { subdegree = R_Degree(child->childNode); if(subdegree > max) { max = subdegree; } child = child->next; } return max; } int Degree(Tree *tree) //树的度 { if (tree == NULL) { errno = ERROR; return FALSE; } int degree = R_Degree(tree->head->next); return degree; } //main.c #include <stdio.h> #include "tree.h" void printA(TreeNode *node) { printf("%c\n",node->data); } 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); Display(tree,printA); TreeData x; //Delete(tree,i,&x); printf("height = %d\n",Tree_Height(tree)); printf("degree = %d\n",Degree(tree)); return 0; }
数据结构之树的实现
最新推荐文章于 2023-08-07 14:02:44 发布