DC -19 :树树树02(接DC - 17)

本文是DC系列的第二部分,主要聚焦于树这种数据结构的详细解析,涵盖树的基本概念、常见操作以及在计算机科学中的应用。通过本文,读者将能深入理解树的内在工作机制,并提升在实际问题中应用树的能力。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


// 递归打印结点
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);
	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);
}

void r_delete(Tree *tree, TreeNode *node)
{
	if (tree == NULL || node == NULL)
		return;
	
	// 从树链表中移除这个结点,找node的前一个结点
	TreeNode* tmp = tree->head; // 链表的头节点
	while (tmp->next)
	{
		if (tmp->next == node)
		{
			tmp->next = node->next;
			tree->len --;
			break;
		}
		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;
		}
	}
	
	// 将该结点的孩子结点删掉
	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;
}
打印运用了递归,因为对于一棵树,你不知道他的深度,节点个数,但打印每一步都是相同的:打印自己,打印子节点,这就可以运用递归,边界条件为子节点为NULL,同样,删除一个节点,首先在parent双亲节点中删除指向本身的那个节点,再删除自己子节点,但删除子节点又要先删除孙节点的,这就是递归。求高度与深度都是把每个父节点看成根节点,找出子节点最长的深度或者高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值