简体   繁体   English

通过对二叉树的归纳证明

[英]Proof by induction on binary trees

Right now I'm currently working on a problem in my algorithm design textbook, and I've hit a bit of a brick wall. 现在我正在研究算法设计教科书中的一个问题,而且我遇到了一些问题。

The problem is: 问题是:

A binary tree is a rooted tree in which each node has at most two children. 二叉树是一个有根树,其中每个节点最多有两个子节点。 Show by induction that in any binary tree that the number of nodes with two children is exactly one less than the number of leaves. 通过归纳显示,在任何二叉树中,具有两个子节点的节点数正好比叶子数少一个。

I'm reasonably certain of how to do this: the base case has a single node, which means that the tree has one leaf and zero nodes with two children. 我有理由相信如何做到这一点:基本情况有一个节点,这意味着树有一个叶子,零节点有两个子节点。 However, I'm not quite certain exactly what the inductive step would entail. 但是,我不太确定归纳步骤究竟会带来什么。

A tree with a single node with no children (obviously), has/is one leaf. 具有单个节点但没有子节点的树(显然)具有/是一片叶子。 The number of nodes with two children (0) is exactly one less than the number of leaves (1). 具有两个子节点(0)的节点数正好比叶子数(1)少一个。

Adding a node to an existing node that has no children, does not change the number of nodes with two children, nor the number of leaves. 将节点添加到没有子节点的现有节点不会更改具有两个子节点的节点数,也不会更改叶子数。

Adding a node to an existing node that has one child, increases the number of nodes with two children by 1, and also increases the number of leaves by 1. 将节点添加到具有一个子节点的现有节点,将具有两个子节点的节点数增加1,并且还将叶数增加1。

You cannot add a node to an existing node with any other number of children. 您无法将节点添加到具有任何其他子节点数的现有节点。

Since the number of nodes with two children starts as exactly one less than the number of leaves, and adding a node to the tree either changes neither number, or increases both by exactly one, then the difference between them will always be exactly one. 由于具有两个子节点的节点的数量恰好比叶子的数量少一个,并且向树中添加节点既不改变数字,也不改变两者,那么它们之间的差异将始终为1。

With induction on the number of nodes with 2 children : 通过感应有2个孩子的节点数:

Base - 0 nodes with 2 children - 1 leaf (assuming the root is not counted as one). 基数 - 0个节点有2个子节点 - 1个叶子(假设根不计为一个)。 Step - Let T be a tree with n+1 > 0 nodes with 2 children 步骤 - 令T为具有n + 1> 0个节点且具有2个子节点的树

=> there is a node a with 2 children a1, a2 and in the subtree rooted in a1 or a2 there are no nodes with 2 children. =>有一个节点a有2个子节点a1,a2,并且在以a1或a2为根的子树中没有带有2个子节点的节点。 we can assume it's the subtree rooted in a1. 我们可以假设它是以a1为根的子树。

=> remove the subtree rooted in a1, we got a tree T' with n nodes with 2 children. =>删除以a1为根的子树,我们得到一个树T',其中n个节点有2个子节点。

=> T' has n+1 leaves. => T'有n + 1个叶子。

=> add the subtree rooted in a1 to T' - we added one leaf and one node with 2 children. =>将以a1为根的子树添加到T' - 我们添加了一个叶子和一个带有2个子节点的节点。

There are some holes that you need to complete, but it's working. 你需要完成一些漏洞,但它有效。 Sorry for my bad english. 对不起,我的英语不好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM