二叉树平衡二叉树
二叉树简介 (Introduction To Binary Trees)
A binary tree is a hierarchical data structure in which each node has at most two children generally referred as left child and right child.
二叉树是一种分层数据结构,其中每个节点最多具有两个子节点,通常称为左子节点和右子节点。
Each node contains three components:
每个节点包含三个组件:
- Pointer to left subtree 指向左子树的指针
- Pointer to right subtree 指向右子树的指针
- Data element 数据元素
The topmost node in the tree is called the root. An empty tree is represented by NULL pointer.
树中最顶层的节点称为根。 空树由NULL指针表示。
A representation of binary tree is shown:
显示了二叉树的表示形式:
二叉树:通用术语 (Binary Tree: Common Terminologies)
- Root: Topmost node in a tree.根:树中的最高节点。
- Parent: Every node (excluding a root) in a tree is connected by a directed edge from exactly one other node. This node is called a parent.父级:树中的每个节点(不包括根节点)都通过有向边与正好一个其他节点相连。 该节点称为父节点。
- Child: A node directly connected to another node when moving away from the root.
- 子节点:远离根节点时直接连接到另一个节点的节点。
- Leaf/External node: Node with no children.
- 叶子/外部节点:没有子节点的节点。
- Internal node: Node with atleast one children.内部节点:至少有一个孩子的节点。
- Depth of a node: Number of edges from root to the node.节点深度:从根到节点的边数。
- Height of a node: Number of edges from the node to the deepest leaf. Height of the tree is the height of the root.节点的高度:从节点到最深的叶子的边数。 树的高度是根的高度。
In the above binary tree we see that root node is A. The tree has 10 nodes with 5 internal nodes, i.e, A,B,C,E,G and 5 external nodes, i.e, D,F,H,I,J. The height of the tree is 3. B is the parent of D and E while D and E are children of B.
在上面的二叉树中,我们看到根节点是A。 该树有10个节点,其中5个内部节点即A,B,C,E,G和5个外部节点即D,F,H,I,J 。 树的高度是3 B是d和E的母公司,而d和E是B的孩子。
树木的优势 (Advantages of Trees)
Trees are so useful and frequently used, because they have some very serious advantages:
树是如此有用且经常使用,因为它们具有一些非常重要的优点:
- Trees reflect structural relationships in the data. 树反映了数据中的结构关系。
- Trees are used to represent hierarchies. 树用于表示层次结构。
- Trees provide an efficient insertion and searching. 树提供了有效的插入和搜索。
- Trees are very flexible data, allowing to move subtrees around with minumum effort. 树是非常灵活的数据,允许以最小的努力来移动子树。
二叉树的类型(基于结构) (Types of Binary Trees (Based on Structure))
- Rooted binary tree: It has a root node and every node has atmost two children.根二叉树:它有一个根节点,每个节点最多有两个子节点。
-
Full binary tree: It is a tree in which every node in the tree has either 0 or 2 children.
完整的二叉树:这是一棵树,其中树中的每个节点都有0或2个子节点。
- n, in a full binary tree is atleast n = 2h – 1, and atmost n至少为n = 2h – 1,最大为h+1 – 1h + 1 – 1h is the height of the tree. h是树的高度。
- l, in a full binary tree is number, l是内部节点的数量L of internal nodes + 1, i.e, L + 1,即l = L+1.l = L + 1 。
-
Perfect binary tree: It is a binary tree in which all interior nodes have two children and all leaves have the same depth or same level.
完美的二叉树:这是一棵二叉树,其中所有内部节点都有两个子节点,所有叶子的深度或级别相同。
- l leaves has l个叶子的理想二叉树具有n = 2l-1 nodes.n = 2l-1个节点。
- l = 2h and l = 2h且n = 2h+1 - 1 where, n = 2h + 1-1,其中, n is number of nodes, n是节点数, h is height of tree and h是树的高度, l is number of leaf nodesl是叶节点的数目
-
Complete binary tree: It is a binary tree in which every level, except possibly the last, is completely filled, and all nodes are as far left as possible.
完整的二叉树:它是一棵二叉树,其中除最后一个级别外,每个级别都已完全填充,并且所有节点都尽可能地靠左。
- floor(n/2).floor(n / 2) 。
-
Balanced binary tree: A binary tree is height balanced if it satisfies the following constraints:
平衡二叉树:如果满足以下约束,则二叉树是高度平衡的:
- The left and right subtrees' heights differ by at most one, AND 左右子树的高度最多相差一,并且
- The left subtree is balanced, AND 左子树是平衡的,并且
- The right subtree is balanced 正确的子树是平衡的
An empty tree is height balanced.
空树是高度平衡的。
- Log n) where Log n ),其中n is number of nodes.n是节点数。
-
Degenarate tree: It is a tree is where each parent node has only one child node. It behaves like a linked list.
退化树:每个父节点只有一个子节点的树。 它的行为就像一个链表。
翻译自: https://www.studytonight.com/data-structures/introduction-to-binary-trees
二叉树平衡二叉树