完全二叉树

2024年8月28日 | 阅读 21 分钟

完全二叉树是一种满足特定条件的二叉树。这些条件是:

  • 完全二叉树中的每一层都应该是完全填充的,只有最后一层除外。某一层的完全填充意味着该层中的每个父节点都应该有两个子节点,即左节点和右节点。如果某一层的任何父节点没有右子节点和左子节点,则不被认为是完全填充的。
  • 二叉树要被称为完全二叉树,还需要满足的另一个条件是,树的最后一层中的所有键都应尽可能靠左。这意味着如果父节点出现在完全二叉树的最后一层,那么它应该只有一个左子节点。

如果任何二叉树满足上述两个特定条件,则该二叉树被标记为完全二叉树。

现在,为了更好地理解,让我们编写一个 C++ 代码来创建和显示完全二叉树。

代码

输出

Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.

1
Enter the data that you want to add to the Complete binary tree::
12      
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.

1
Enter the data that you want to add to the Complete binary tree::
14
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.

1 
Enter the data that you want to add to the Complete binary tree::
16     
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.

1
Enter the data that you want to add to the Complete binary tree::
18
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.

1
Enter the data that you want to add to the Complete binary tree::
15
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.

1
Enter the data that you want to add to the Complete binary tree::
24
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.

2
Contents of the Complete binary tree are::
12 14 16 18 15 24 
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
N

因此,在我们上面编写和执行的 C++ 程序中,我们提供了一种菜单驱动的方法来向完全二叉树添加数据,并通过打印完全二叉树中存在的数据来验证添加操作。

这里我们首先通过调用 insert 函数向完全二叉树添加了六个节点,该函数将向现有的完全二叉树添加或追加一个新节点。这些完全二叉树新节点的值由用户在向完全二叉树添加节点时输入。因此,用户输入菜单中显示的任何选项,操作都成功执行。

那么,现在让我们用 Java 编程语言编写完全二叉树的代码。

代码

输出

Please Choose one of the Operations from the menu Displayed below::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.
1
Enter the data that you want to add to the Complete binary tree::
11
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations from the menu Displayed below::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.
1
Enter the data that you want to add to the Complete binary tree::
23
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations from the menu Displayed below::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.
1 
Enter the data that you want to add to the Complete binary tree::
16
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations from the menu Displayed below::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.
1
Enter the data that you want to add to the Complete binary tree::
76
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations from the menu Displayed below::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.
1
Enter the data that you want to add to the Complete binary tree::
43
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations from the menu Displayed below::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.
1
Enter the data that you want to add to the Complete binary tree::
33
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations from the menu Displayed below::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.
1
Enter the data that you want to add to the Complete binary tree::
92
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations from the menu Displayed below::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.
1
Enter the data that you want to add to the Complete binary tree::
31
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
y
Please Choose one of the Operations from the menu Displayed below::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.
2
Contents of the Complete binary tree are::
31 76 23 43 11 33 16 92 
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.
n

因此,在我们上面编写和执行的 Java 程序中,我们提供了一种菜单驱动的方法来向完全二叉树添加数据,它将提供两个功能;一个用于向完全二叉树添加数据,另一个功能则可以帮助我们通过打印完全二叉树中存在的数据来验证添加操作。

这里,我们首先通过调用 insert 函数向完全二叉树添加了八个节点,它们的值分别为 31、76、23、43、11、33、16 和 92。该函数将向现有的完全二叉树添加或追加一个新节点,但如果将要添加的节点是第一个节点,它将创建一个新的完全二叉树作为完全二叉树的根。这些完全二叉树新节点的值由用户在向完全二叉树添加节点时输入,并作为参数传递给 insert 函数中的完全二叉树的 add 函数。因此,用户输入菜单中显示的任何选项,操作都成功执行。如果用户输入的选项不是指定的任何操作,则会显示错误消息“请从菜单中输入有效选项以继续。”。为了退出程序,用户需要输入“N”或“n”,为了继续程序以获取更多选项,用户输入“Y”或“y”。

那么,现在让我们用 Python 编程语言编写完全二叉树的代码。

代码

输出

Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


1
Enter the data that you want to add to the Complete binary tree::
23
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


1
Enter the data that you want to add to the Complete binary tree::
76
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


1
Enter the data that you want to add to the Complete binary tree::
42
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


1
Enter the data that you want to add to the Complete binary tree::
11
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


30

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


1
Enter the data that you want to add to the Complete binary tree::
56
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


1
Enter the data that you want to add to the Complete binary tree::
68
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


1
Enter the data that you want to add to the Complete binary tree::
03
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


1
Enter the data that you want to add to the Complete binary tree::
44
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


1
Enter the data that you want to add to the Complete binary tree::
17
Data Added Successfully.

1
Enter the data that you want to add to the Complete binary tree::
28
Data Added Successfully.

Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

y
Please Choose one of the Operations::
1. To Insert Data in the Complete binary tree.
2. To Display Data from the Complete binary tree.


2
Contents of the Complete binary tree are::
23 76 42 11 56 68 3 44 17 28
Type [N or n] to terminate the program.
Type [Y or y] to continue the program.

N

因此,在我们上面编写和执行的 Python 程序中,我们提供了一种菜单驱动的方法来向完全二叉树添加数据,它将提供两个功能;一个用于向完全二叉树添加数据,另一个功能则可以帮助我们通过打印完全二叉树中存在的数据来验证添加操作。

这里我们首先通过调用 insert 函数向完全二叉树添加节点,该函数将向现有的完全二叉树添加或追加一个新节点,但如果将要添加的节点是第一个节点,它将创建一个新的完全二叉树作为完全二叉树的根。这些完全二叉树新节点的值由用户在向完全二叉树添加节点时输入,并作为参数传递给 insert 函数中的完全二叉树的 add 函数。因此,用户输入菜单中显示的任何选项,操作都成功执行。如果用户输入的选项不是指定的任何操作,则会显示错误消息“请从菜单中输入有效选项以继续。”。为了退出程序,用户需要输入“N”或“n”,为了继续程序以获取更多选项,用户输入“Y”或“y”。

因此,本文解释了完全二叉树数据结构以及我们可以在完全二叉树上执行的基本函数或操作。我们还了解了完全二叉树在 Java、Python 和 C++ 等各种编程语言中的使用,以及在这些数据结构上执行基本操作所需的功能。除了这些示例,在各种场景中我们都可以使用完全二叉树。


下一主题线索二叉树