#ifndef __TREE_H__
#define __TREE_H__
#include
#include
#include
#define FALSE 0
#define TRUE 1
struct _treenode;
typedef struct _childnode
{
struct _childnode *Next;
struct _treenode *ChildNode;
}CHILD_NODE;
typedef char TREE_DATA;
typedef struct _treenode
{
TREE_DATA data;
struct _treenode *Parent;
struct _childnode *ChildList;
struct _treenode *Next;
int degree;
}TREE_NODE;
typedef struct _tree
{
struct _treenode *head;
int length;
}TREE;
extern TREE* CreateTree();
extern int InsertTreeNode(TREE* tree, TREE_DATA data, int pos);
extern void Display(TREE* tree);
extern DeleteTreeeNode(TREE *tree, int pos, TREE_DATA* e);
extern int GetTreeNode(TREE* tree, int pos, TREE_DATA *e);
extern int ClearTree(TREE* tree);
extern int DestoryTree(TREE* tree);
extern TREE_NODE* GetTreeRoot(TREE* tree);
extern int TreeLength(TREE* tree);
extern int TreeHeight(TREE* tree);
extern int TreeDegree(TREE *tree);
#endif
#include "tree.h"
TREE* CreateTree()
{
TREE* tree = (TREE*)malloc(sizeof(TREE));
if(tree == NULL)
{
printf("create tree:malloc error!\n");
return NULL;
}
tree->length = 0;
tree->head = (TREE_NODE*)malloc(sizeof(TREE_NODE));
if(tree->head == NULL)
{
printf("create tree head:malloc error!\n");
free(tree);
return NULL;
}
tree->head->degree = 0;
tree->head->data = 0;
tree->head->Next = NULL;
tree->head->Parent = NULL;
tree->head->ChildList = NULL;
return tree;
}
int InsertTreeNode(TREE* tree, TREE_DATA data, int pos)
{
if(tree == NULL || pos < 0 || pos > tree->length)
{
printf("insert tree node[1]:invalid input\n");
return FALSE;
}
if(pos != 0 && tree->length == pos)
{
printf("insert tree node[2]:invalid input\n");
return FALSE;
}
TREE_NODE* node = (TREE_NODE*)malloc(sizeof(TREE_NODE));
if(node == NULL)
{
printf("insert tree node: node malloc error\n");
return FALSE;
}
node->data = data;
node->Next = NULL;
node->ChildList = (CHILD_NODE*)malloc(sizeof(CHILD_NODE));
if(node->ChildList == NULL)
{
printf("insert tree node:childlist malloc error\n");
free(node);
return FALSE;
}
node->ChildList->Next = NULL;
node->ChildList->ChildNode = NULL;
int i = 0;
TREE_NODE* _parent = tree->head->Next;
for (i = 0; i < pos; i++)
{
_parent = _parent->Next;
}
node->Parent = _parent;
if(_parent != NULL)
{
CHILD_NODE* child_node = (CHILD_NODE*)malloc(sizeof(CHILD_NODE));
if(child_node == NULL)
{
printf("insert tree node:child node malloc error!\n");
free(node->ChildList);
free(node);
return FALSE;
}
child_node->Next = NULL;
child_node->ChildNode = node;
CHILD_NODE* tmp = _parent->ChildList;
while (tmp->Next != NULL)
{
tmp = tmp->Next;
}
tmp->Next = child_node;
_parent->degree++;
}
TREE_NODE* temp = tree->head;
while(temp->Next != NULL)
{
temp = temp->Next;
}
temp->Next = node;
tree->length++;
return TRUE;
}
void RDisplay(TREE_NODE* node, int gap)
{
if(node == NULL)
{
printf("R display:no node\n");
return ;
}
int i = 0;
for(i = 0; i < gap; i++)
{
printf("%c", '-');
}
printf("%c\n", node->data);
CHILD_NODE* child = node->ChildList->Next;
while(child != NULL)
{
RDisplay(child->ChildNode, gap + 4);
child = child->Next;
}
}
void Display(TREE* tree)
{
if(tree == NULL)
{
printf("display:invalid input\n");
return ;
}
RDisplay(tree->head->Next, 0);
}
void RDelete(TREE *tree, TREE_NODE* node)
{
if(tree == NULL || node == NULL)
{
printf("R delete:unexpected error\n");
return ;
}
TREE_NODE* tmp = tree->head;
while(tmp->Next != NULL)
{
if(tmp->Next == node)
{
tmp->Next = node->Next;
tree->length--;
break;
}
tmp = tmp->Next;
}
TREE_NODE* parent = node->Parent;
if(parent != NULL)
{
CHILD_NODE* temp = parent->ChildList;
while(temp->Next != NULL)
{
if(temp->Next->ChildNode == node)
{
CHILD_NODE* p = temp->Next;
temp->Next = p->Next;
free(p);
parent->degree--;
break;
}
temp = temp->Next;
}
}
CHILD_NODE* child = node->ChildList->Next;
while (child != NULL)
{
CHILD_NODE* pchild = child->Next;
RDelete(tree, child->ChildNode);
child = pchild;
}
free (node->ChildList);
free (node);
}
int DeleteTreeNode(TREE* tree, int pos, TREE_DATA *e)
{
if(tree == NULL || pos < 0 || pos > tree->length)
{
printf("delete tree node:input invalid\n");
return FALSE;
}
if(pos != 0 && pos == tree->length)
{
printf("delete tree node:input invalid\n");
return FALSE;
}
int i = 0;
TREE_NODE* node = tree->head->Next;
for (i = 0; i < pos; i++)
{
node = node->Next;
}
*e = node->data;
RDelete(tree, node);
return TRUE;
}
int GetTreeNode(TREE* tree, int pos, TREE_DATA *e)
{
if(tree == NULL || pos < 0 || pos > tree->length)
{
printf("get tree node:input invalid\n");
return FALSE;
}
if(pos != 0 && pos == tree->length)
{
printf("get tree node:input invalid\n");
return FALSE;
}
int i = 0;
TREE_NODE* tmp = tree->head->Next;
for(i = 0; i < pos; i++)
{
tmp = tmp->Next;
}
*e = tmp->data;
return TRUE;
}
int ClearTree(TREE* tree)
{
if(tree == NULL)
{
printf("clear tree:no tree\n");
return FALSE;
}
TREE_DATA e;
return DeleteTreeNode(tree, 0, &e);
}
int DestoryTree(TREE *tree)
{
if(tree == NULL)
{
printf("destory tree:no tree\n");
return FALSE;
}
ClearTree(tree);
free(tree->head);
free(tree);
return TRUE;
}
TREE_NODE* GetTreeRoot(TREE* tree)
{
if(tree == NULL)
{
printf("get tree root:no tree\n");
return FALSE;
}
return tree->head->Next;
}
int TreeLength(TREE* tree)
{
if(tree == NULL)
{
printf("get tree length:no tree\n");
return FALSE;
}
return tree->length;
}
int RHeight(TREE_NODE* node)
{
if(node == NULL)
{
printf("r height:no tree head\n");
return FALSE;
}
int SubHeight = 0;
int max = 0;
CHILD_NODE* child = node->ChildList->Next;
while(child != NULL)
{
SubHeight = RHeight(child->ChildNode);
if(SubHeight > max)
{
max = SubHeight;
}
child = child->Next;
}
return max + 1;
}
int TreeHeight(TREE* tree)
{
if(tree == NULL)
{
printf("get tree height:no tree\n");
return FALSE;
}
int height = RHeight(tree->head->Next);
return height;
}
int RDegree(TREE_NODE* node)
{
if(node == NULL)
{
printf("get tree degree:no tree head\n");
return FALSE;
}
int SubDegree = 0;
int max = node->degree;
CHILD_NODE* child = node->ChildList->Next;
while(child != NULL)
{
SubDegree = RDegree(child->ChildNode);向下递归找最大度
if(SubDegree > max)
{
max = SubDegree;
}
child = child->Next;
}
return max;
}
int TreeDegree(TREE* tree)
{
if(tree == NULL)
{
printf("get tree degree:no tree\n");
return FALSE;
}
int degree = RDegree(tree->head->Next);
return degree;
}
//main.c 基本测试
int main()
{
TREE* tree = CreateTree();
if(tree == NULL)
{
return -1;
}
InsertTreeNode(tree, 'A', 0);
InsertTreeNode(tree, 'B', 0);
InsertTreeNode(tree, 'C', 0);
InsertTreeNode(tree, 'D', 0);
InsertTreeNode(tree, 'E', 1);
InsertTreeNode(tree, 'F', 1);
InsertTreeNode(tree, 'G', 3);
InsertTreeNode(tree, 'H', 3);
InsertTreeNode(tree, 'I', 3);
InsertTreeNode(tree, 'J', 8);
Display(tree);
printf("删除结点D\n");
TREE_DATA e;
DeleteTreeNode(tree, 3, &e);
Display(tree);
printf("获取结点F的值\n");
TREE_DATA e1;
GetTreeNode(tree, 4, &e1);
printf("F data is %c\n", e1);
printf("获取树的高度\n");
int height = TreeHeight(tree);
printf("树的高度为 %d\n", height);
printf("获取树的度\n");
int degree = TreeDegree(tree);
printf("树的度为 %d\n", degree);
return 0;
}