数据结构与算法分析-C语言描述--第四章题目答案

部分自己证明 部分翻自该书答案

1-3 不作详细证明

4.4 证明在N个节点的二叉树中,存在N+1个NULL指针代表N+1个儿子。

证: 对于N个节点的二叉树 共有2N个指针 而每个节点(除去根节点)都会占用一个指针,所以有N-1个指针不为空

    所以共有: 2N-(N-1) = N+1 个NULL指针

4.5 证明在高度为H的二叉树中,节点最大个数是2^{H+1}-1

证: 对于高度为H的二叉树,其最大为满二叉树,对于满二叉树,在高度为0的节点数目为2^{H}, 高度为H的点为根节点  节点数目为1,所以节点总数为2^{0}+2^{1}+... + 2^{H} = 2^{H+1}-1

4.6 满节点(full Node)是具有两个儿子的节点。证明满节点的个数加1等于非空二叉树的树叶个数

证: 已知N个节点,满节点个数为x,树叶节点为y,  半节点(只有一个孩子的节点)为z, 可得 x+y+z = N 

由指针数可得   2x+z=N-1   可得 x+1 = y  得证。

4.7 设二叉树有树叶L1,L2,... ,LM,各树叶的深度分别是d1,d2,...,dM。 证明,\sum_{1}^{M}2^{-d_{i}}\leq 1 并确定何时等号成立。

证:

This can be shown by induction. In a tree with no nodes, the sum is zero, and in a one-node tree, the root is a leaf at depth zero, so the claim is true. Suppose the theorem is true for all trees with at most kO nodes. Consider any tree with kO+1 nodes. Such a tree consists of an iO node left subtree and a kO − iO node right subtree. By the inductive hypothesis, the sum for the left subtree leaves is at most one with respect to the left tree root. Because all leaves are one deeper with respect to the original tree than with respect to the subtree, the sum is at most ⁄ 1 2 with respect to the root. Similar logic implies that the sum for leaves in the right subtree is at most ⁄ 1 2, proving the theorem. The equality is true if and only if there are no nodes with one child. If there is a node with one child, the equality cannot be true because adding the second child would increase the sum to higher than 1. If no nodes have one child, then we can find and remove two sibling leaves, creating a new tree. It is easy to see that this new tree has the same sum as the old. Applying this step repeatedly, we arrive at a single node, whose sum is 1. Thus the original tree had sum 1.(原答案)

通过归纳法可以证明,首先当树无节点时,和为0;当只有一个节点时,根的深度为0,则和为1,此时公式成立。假设当树有K个节点时公式成立,则当树有K+1个节点时,设左子树存在i个节点,右子树存在k-i个节点(根为一个节点)。将左子树单独看作一颗树时,因为 i<K 所以和最大为1的公式成立。此时由于左子树为子树,所有叶子节点的深度均增加1,所以和最大值为1/2,同理可证右子树和最大值为1/2,则K+1时树的和最大仍为1,公式得证。

当树中只存在0个孩子或2个孩子的节点时,等号成立。

4.8

前缀: - * * a b + c d e

中缀: a * b * c + d - e

后缀: a b * c d + * e -

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值