C#数据结构和算法 [Binary Trees and Binary]

Trees are a very common data structure in computer science. A tree is a
nonlinear data structure that is used to store data in a hierarchical manner.
We examine one primary tree structure in this chapter, the binary tree, along
with one implementation of the binary tree, the binary search tree. Binary
trees are often chosen over more fundamental structures, such as arrays and
linked lists, because you can search a binary tree quickly (as opposed to a
linked list) and you can quickly insert data and delete data from a binary tree
(as opposed to an array).

树是一种很常见的数据结构。树是一种按层次存储的非线性的数据结构。

本章我们讲一种主要的树结构--二叉树,以及二叉树的一种实现。

二叉树是一种基础的数据结构,如数组,链表,你可以快速地从二对树中查找数据(相比较链表)

而且你可以从二叉树中快递地插入和删除数据(相比较数组)。

THE DEFINITION OF A TREE
Before we examine the structure and behavior of the binary tree, we need to
define what we mean by a tree. A tree is a set of nodes connected by edges. An
example of a tree is a company’s organization chart (see Figure 12.1).
The purpose of an organization chart is to communicate to the viewer the
structure of the organization. In Figure 12.1, each box is a node and the
lines connecting the boxes are the edges. The nodes, obviously, represent
the entities (people) that make up an organization. The edges represent the
relationship between the entities. For example, the Chief Information Officer
(CIO), reports directly to the CEO, so there is an edge between these two

nodes. The IT manager reports to the CIO so there is an edge connecting
them. The Sales VP and the Development Manager in IT do not have a direct
edge connecting them, so there is not a direct relationship between these two
entities.

二叉树的定义

在讲解之前,我们先给出二叉树的定义。树是一组用边联结起的结点。有个公司行政图的例子
如下图:

此图的目的是给观众传达组织的体系。图中每一个方块是一个结点。联结这些方块的连接线是边。
很明显,这些结点代表组成公司的实体(人),这些边代表实体间的关系。比如,CIO直属CEO,

所以有一条连接CIO和CEO的线,之类的。
C#数据结构和算法 [Binary Trees and Binary]_第1张图片
Figure 12.2 displays another tree that defines a few terms we need when
discussing trees. The top node of a tree is called the root node. If a node is
connected to other nodes below it, the top node is called the parent, and

the nodes below it are called the parent’s children. A node can have zero,
one, or more nodes connected to it. Special types of trees, called binary trees,
restrict the number of children to no more than two. Binary trees have certain
computational properties that make them very efficient for many operations.
Binary trees are discussed extensively in the sections of this chapter. A node
without any child node is called a leaf.
Continuing to examine Figure 12.2, you can see that by following certain
edges, you can travel from one node to other nodes that are not
directly connected. The series of edges you follow to get from one node
to another is called a path (depicted in the figure with dashed lines). Visiting
all the nodes in a tree in some particular order is known as a tree
transversal.

下面的图显示了另一种树,顶部的结点叫做根结点。如果一个结点有被其他结点相连,上面的结点叫父结点,

下面的叫子结点。一个结点可以有0,1,或多个结点相连。有种特别的树,叫做二叉树,最多只能有两个子结点。

二叉树因为某种计算目的使得很多操作更有效。这一节大概说一下二叉树。没有子结点的结点叫叶子。

你可以从下图看到:沿着一些边,你可以从一个结点到另一个不直接相联的结点,这些边叫路径(图中的虚线)。

以一定的顺序查看所有的结点叫做遍历。

C#数据结构和算法 [Binary Trees and Binary]_第2张图片
A tree can be broken down into levels. The root node is at Level 0, its
children at Level 1, those node’s children are at Level 2, and so on. A node at
any level is considered the root of a subtree, which consists of that root node’s
children, its children’s children, and so on. We can define the depth of a tree
as the number of layers in the tree.
Finally, each node in a tree has a value. This value is sometimes referred to
as the key value.

树 可以打断成很多层级。根结点是第0层,他的子结点是第1层。以此类推。

一个在任何层的结点都叫做根结点的子树。深度是指树的层数。

最后,每一个结点都有一个值,这个值有时被称为键值。

BINARY TREES
A binary tree is defined as a tree where each node can have no more than
two children. By limiting the number of children to 2, we can write efficient
programs for inserting data, deleting data, and searching for data in a binary
tree.
Before we discuss building a binary tree in C#, we need to add two
terms to our tree lexicon. The child nodes of a parent node are referred
to as the left node and the right node. For certain binary tree implementations,
certain data values can only be stored in left nodes and other data
values must be stored in right nodes. An example binary tree is shown in
Figure 12.3.
Identifying the child nodes is important when we consider a more specific
type of binary tree—the binary search tree. A binary search tree is a binary tree
where data with lesser values are stored in left nodes and values with greater
values are stored in right nodes. This property provides for very efficient
searches, as we shall soon see.

二叉树

树中的结点最多有两个子结点的树叫做二叉树。通过限制子结点,我们可以在二叉树中快速地插入,删除,查找数据。

之前我们讨论的C#中的二叉树,我们要增加两组词汇。父结点的子结点被称为左右结点,有二叉树的有些实现代码中,

左右结点存储的值是很讲究的,如下图的例子:

当我们考虑一种更具体的二叉树--二叉搜索树时,识别子结点是很重要的。二叉搜索树是一种小值在左,大值在右的二叉树。
这么做的目是让查找更快。

C#数据结构和算法 [Binary Trees and Binary]_第3张图片

Building a Binary Search Tree
A binary search tree is made up of nodes, so we need a Node class that is
similar to the Node class we used in the linked list implementation. Let’s look
at the code for the Node class first:

建立一个二叉搜索树

二叉搜索树是由结点构成的,所以我们需要一个类似链表中的结点类。

 


你可能感兴趣的:(数据结构和算法)