数据结构:二叉树,AVL树和优先级队列:二叉树及其应用(Binary Trees and Their Applications)

章节主题

  • 二叉树及其应用

  • 二叉搜索树

  • AVL树

  • 优先级队列

 

Chapter Topics

  • Binary Trees and Their Applications 

  • Binary Search Trees

  • AVLTrees

  • Priority Queues

 

二叉树及其应用(Binary Trees and Their Applications)

 

二叉树

  • 二叉树就像一个链表,除了每个节点最多可以有两个后继。

  • 二叉树中节点X的后继者称为X的子节点。

  • 在二叉树中,每个节点最多只有一个前驱。 X的前身称为X的父节点。

  • 二叉树中节点的子节点是左子节点或右子节点。

 

Binary Trees

  • A binary tree is like a linked list, except each node may have up to two successors.

  • A successor of a node X in a binary tree is called a child of X.

  • In a binary tree, each node has at most one predecessor. The predecessor of X is called the parent of X.

  • A child of a node in a binary tree is either a left child or a right child.

 

关于二叉树的事实

  • 如果节点C是另一个节点P的子节点,则P称为C的父节点。

  • 二叉树可能为空。

  • 非空二叉树具有唯一没有父节点的节点。 此节点称为二叉树的根。

  • 没有子节点的节点称为叶子。

 

Facts About Binary Trees

  • If a node C is a child of another node P, then P is called the parent of C.

  • A binary tree may be empty.

  • A nonempty binary tree has a unique node that has no parent. This node is called the root of the binary tree.

  • A node with no children is called a leaf.

 

二叉树

  • C是A的右子节点。

  • E是C的左子节点。

  • D和G是叶子。

  • A是根。

 

A Binary Tree

  • C is the right child of A.

  • E is the left child of C.

  • D and G are leaves.

  • A is the root.

节点的后代

设X是二叉树T中的节点。如果Y在从X到T的叶子的路径上,则节点Y是X的后代。

  • C的后代是E,F和G.

  • B的后代是D.

 

Descendants of a Node

Let X be a node in a binary tree T. A node Y is a descendant of X if Y is on the path from X to a leaf of T.

  • Descendants of C are E, F, and G.

  • Descendants of B is D.

子树

  • 节点X的所有后代的集合形成二叉树,称为以X为根的T的子树。

  • 如果R是T的根,则以R的左子节为根的子树称为T的左子树,以R为右子节的子树称为T的右子树。

 

Subtrees

  • The collection of all descendants of a node X forms a binary tree, called the subtree of T rooted at X.

  • If R is the root of T, then the subtree rooted at the left child of R is called the left subtree of T, and the subtree rooted at the right child of R is called the right subtree of T.

二叉树的应用

  • 二叉树用于组织信息以支持快速搜索。

  • 二进制树的推广在数据库系统中用于存储数据。

  • 二叉树用于表示算术表达式。

 

Applications of Binary Trees

  • Binary trees are used to organize information to support fast search.

  • Generalizations of binary trees are used in database systems to store data.

  • Binary trees are used to represent arithmetic expressions.

 

ADT二叉树的实现

  • 实现基于类似于链接列表中使用的Node类。

  • SeeTreeNode.java。

 

Implementation of ADT Binary Trees

  • Implementation is based on a Node class similar to what is used in linked lists.

  • SeeTreeNode.java.

 

表示二叉树

  • 二叉树由对其根节点的引用表示。

节点根;

  • 空二进制树用引用表示,其值为null。

 

Representing a Binary Tree

  • A binary tree is represented by a reference to its root node.

Node root;

  • An empty binary tree is represented with a reference whose value is null.

 

ADT二叉树:

ADT二叉树的基本操作

  • 作为抽象数据类型,二叉树具有添加和删除节点和子树的操作。 通过使用这些基本操作,您可以构建任何二叉树。

  • 其他操作在树的根目录中设置或检索数据,并确定树是否为空。

  • 遍历操作访问二叉树中的每个节点,使用它或对其执行某些操作。 三个标准命令是前序,中序和后序。

 

The ADT Binary Tree:

Basic Operations of the ADT Binary Tree

  • As an abstract datatype, the binary tree has operations that add and remove nodes and subtrees. By using these basic operations, you can build any binary tree.

  • Other operations set or retrieve the data in the root of the tree and determine whether the tree is empty.

  • Traversal operations visit every node in a binary tree, do something with it or to it. Three standard orders are preorder, inorder, and postorder.

 

ADT二叉树:

ADT二叉树的基本操作

  • 可用于特定ADT二叉树的操作取决于要实现的二叉树的类型

  • ADT二叉树的基本操作

 

The ADT Binary Tree:

Basic Operations of the ADT Binary Tree

  • The operations available for a particular ADT binary tree depend on the type of binary tree being implemented

  • Basic operations of the ADT binary tree

 

    • createBinaryTree()

    • createBinaryTree(rootItem)

    • makeEmpty()

    • isEmpty()

    • getRoot() throws TreeException 

    • setRoot(rootItem) throws

            UnsupportedOperationException

 

  • ADT二叉树的常规操作(附加操作)

 

  • General operations (additional operations) of the ADT binary tree

    • createBinaryTree (rootItem, leftTree, rightTree) 

    • setRoot(newItem)

    • attachLeft(newItem) throws TreeException

    • attachRight(newItem) throws TreeException

    • attachLeftSubtree(leftTree) throws TreeException

    • attachRightSubtree(rightTree) throws TreeException 

    • detachLeftSubtree() throws TreeException

    • detachRightSubtree() throws TreeException

 

  • 二叉树的遍历

  • 二叉树的遍历算法访问树中的每个节点

  • 递归遍历算法 

    • 前序遍历

    • 中序遍历

    • 后序遍历

  • 这些遍历中的每一个都只访问二叉树中的每个节点一次,执行相同的操作(O(1)),独立于n,遍历为O(n)

 

  • Traversals of a Binary Tree

  • A traversal algorithm for a binary tree visits each node in the tree

  • Recursive traversal algorithms 

    • Preorder traversal

    • Inorder traversal

    • Postorder traversal

  • Each of these traversals visits every node in a binary tree exactly once, performs the same operation (O(1)),independently of n, and traversal is O(n)

 

  • 前序遍历算法假设“访问节点”表示显示节点的数据项。

if(树不为空){

    显示树根中的数据

    以前序遍历(树根的左子树)

    以前序遍历((树的根的右子树)

}

 

  • Preorder traversal algorithm Assumes that “visit a node” means to display the node’s data item.

if (the tree is not empty) {

    Display the data in the root of the tree

    preorder(Left subtree of the tree ‘s root) 

    preorder(Right subtree of the tree ‘s root)

}

  • 中序遍历算法

假设“访问节点”意味着显示节点的数据项。

if(树不为空){

    中序(树根的左子树)

    显示树根中的数据

    中序(树根的右子树)

}

 

  • Inorder traversal algorithm 

Assumes that “visit a node” means to display the node’s data item.

if (the tree is not empty) {

    Inorder(Left subtree of the tree ‘s root) 

    Display the data in the root of the tree

    Inorder(Right subtree of the tree ‘s root)

 }

  • 后序遍历算法

假设“访问节点”意味着显示节点的数据项。

if(树不为空){

后序(树根的左子树)

后序(树根的右子树)

显示树根中的数据

}

 

  • Post-order traversal algorithm

Assumes that “visit a node” means to display the node’s data item.

if (the tree is not empty) {

Postorder(Left subtree of the tree ‘s root)

Postorder(Right subtree of the tree ‘s root)

Display the data in the root of the tree

}

 

  • 二叉树的可能表示

    • 基于数组的表示

    • Java类用于在树中定义节点

    • 使用树节点数组表示二叉树

    • 每个树节点包含一个数据部分和两个索引(每个节点的子节点一个)

    • 需要创建一个跟踪可用节点的空闲列表

 

  • Possible Representations of a Binary Tree

    • An array-based representation

    • A Java class is used to define a node in the tree

    • A binary tree is represented by using an array of tree nodes

    • Each tree node contains a data portion and two indexes (one for each of the node’s children)

    • Requires the creation of a free list which keeps track of available nodes

 

  • 基于数组的完整树表示

    • 如果二叉树已完成并仍保持完整

      • 可以使用基于阵列的内存高效实现

 

  • An array-based representation of a complete tree

    • If the binary tree is complete and remains complete

      • A memory-efficient array-based implementation can be used

 

  • 基于参考的表示

    • Java引用可用于链接树中的节点

 

  • A reference-based representation

    • Java references can be used to link the nodes in the tree

基于参考的ADT二叉树实现

  • 为ADT二叉树提供基于引用的实现的类

    • TreeNode

      • 表示二叉树中的节点

    • TreeException

      • 异常类

    • BinaryTreeBasis

      • 基本树操作的抽象类

    • BinaryTree

      • 提供二叉树的常规操作

      • 扩展BinaryTreeBasis

 

A Reference-Based Implementation of the ADT Binary Tree

  • Classes that provide a reference-based implementation for the ADT binary tree

    • TreeNode

      • Represents a node in a binary tree

    • TreeException

      • An exception class

    • BinaryTreeBasis

      • An abstract class of basic tree operation

    • BinaryTree

      • Provides the general operations of a binary tree 

      • Extends BinaryTreeBasis

 

使用迭代器的树遍历

  • TreeIterator

    • 实现Java Iterator接口

      • 必须提供三种方法:next,hasNext和remove

      • 未实现删除

        • 删除节点取决于树的类型

        • BinaryTreeBasis没有删除节点的方法。

  • 提供将迭代器设置为所需遍历类型的方法

  • 使用队列来维护树中节点的当前遍历

 

Tree Traversals Using an Iterator

  • TreeIterator

    • Implements the Java Iterator interface

    • must provide three methods: next, hasNext, and remove

    • Not implement remove

      • Removal of a node depends on the type of tree

      • BinaryTreeBasis doesn’t have a method removing a node.

  • Provides methods to set the iterator to the type of traversal desired

  • Uses a queue to maintain the current traversal of the nodes in the tree

 

使用迭代器的树遍历

  • 非递归遍历(可选)

    • 迭代方法和显式堆栈可用于模拟从递归调用返回到中序的操作

 

Tree Traversals Using an Iterator

  • Nonrecursive traversal (optional)

    •  An iterative method and an explicit stack can be used to mimic actions at a return from a recursive call to inorder

 

 

    原文作者:AVL树
    原文地址: https://blog.csdn.net/qq_27467365/article/details/84455435
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞