1. 程式人生 > >C語言樹的建立

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 NULL;
	}
	
	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->childList = NULL;
	tree->head->next      = 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 && 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->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* 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", '-');
	}
	

	printf ("%c\n", node->data);
	
	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);
}

void r_delete(Tree *tree, TreeNode *node)//遞迴刪除孩子節點
{
	if (tree == NULL || node == NULL)
		return;
	

	TreeNode* tmp = tree->head; // 連結串列的頭節點
	while (tmp->next)
	{
		if (tmp->next == node)
		{
			tmp->next = node->next;
			tree->len --;
			break;
		}
		tmp = tmp->next;
	}
	

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

	ChildNode* child = node->childList->next;   // 子結點連結串列中的第一個結點
	while (child)
	{
		ChildNode* pchild = child->next;
		r_delete(tree, child->childNode);
		child  = pchild;
	}

	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 Tree_Get(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;
	return Delete (tree, 0, &x);
}

void Tree_Destroy(Tree* tree)//銷燬樹
{
	if (tree == NULL)
	{
		errno = ERROR;
		return;
	}
	
	Tree_Clear(tree);
	
	free (tree->head);
	free (tree);
}

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_height(TreeNode* node)//遞迴計算節點深度
{
	if (node == NULL)
		return 0;
	
	int subHeight = 0;
	int max = 0;
	ChildNode* child = node->childList->next;
	while (child)
	{
		subHeight = r_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_height(tree->head->next);
	
	return height;
}

int r_degree(TreeNode* node)//遞迴計算節點的度
{
	if (node == NULL)
		return 0;
	
	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 Tree_Degree(Tree* tree)//樹的度
{
	if (tree == NULL)
	{
		errno = ERROR;
		return FALSE;
	}
	
	int degree = r_degree(tree->head->next);
	
	return degree;
}