数据结构:二叉树,AVL树和优先级队列:二叉搜索树(Binary Search Trees)

二叉搜索树(Binary Search Trees)

 

ADT二进制搜索树(The ADT Binary Search Tree)

 

  • 搜索特定项目是不适合ADT二叉树的一种操作。

  • 它由ADT二进制搜索树更正

  • 二叉搜索树按值组织其数据

  • 二叉搜索树中的每个节点n满足以下属性

    • n的值大于其左子树TL中的所有值 

    • n的值小于其右子树TR中的所有值 

    • TL和TR都是二叉搜索树

 

  • Searching for a particular item is one operation for which the ADT binary tree is ill suited.

  • It is corrected by the ADT binary search tree

  • A binary search tree organizes its data by value

  • Each node n in a binary search tree satisfies the following properties

    • n’s value is greater than all values in its left subtree TL 

    • n’s value is less than all values in its right subtree TR 

    • Both TL and TR are binary search trees

 

  • 通过这种数据组织,您可以在二叉树中搜索特定项目,而不是位置

  • 二叉搜索树通常用于存储在树中的实例包含许多不同信息字段的情况。 例如,二叉搜索树中的每个节点可能是包含人名,身份证号码,地址,电话号码等的人物对象。

 

  • This organization of data enable you to search a binary tree for a particular item, given its value instead of position

  • A binary search tree is often used in situations where the instances stored in the tree contain many different fields of information. For example, each node in a binary search tree might be a person object containing a person’s name, id number, address, telephone number, and so on

 

  • 二叉搜索树中的节点或实例具有专门指定的搜索关键字

    • 搜索关键字是节点的一部分,用于在节点集合中标识它(例如,学生ID标识每个人对象。)

    • 搜索键值不应该是可修改的

    • 此搜索关键数据类型应实现Comparable接口

 

  • A node or an instance in a binary search tree has a specially designated search key

    • A search key is the part of a node that identifies it within a collection of nodes( For example, a student id identifies each person object.)

    • The search key value should not be modifiable

    • This search-key data type should implement the Comparable interface

 

  • 二叉搜索树的递归定义是二叉搜索树中的每个节点n满足以下属性

    • n的搜索关键字大于其左子树TL中的搜索关键字

    • n的搜索关键字小于右子树TR中的搜索关键字

    • TL和TR都是二叉搜索树

 

  • The recursive definition of a binary search tree is Each node n in a binary search tree satisfies the following properties

    • n’s search key is greater than search keys in its left subtree TL

    • n’s search key is less than search keys in its right subtree TR

    • Both TL and TR are binary search trees

 

  • 将基本ADT二叉树扩展到ADT二进制搜索树的操作如下:

    • 将新项插入二叉搜索树

    • 使用二叉搜索树中的给定搜索关键字删除项目

    • 使用二叉搜索树中的给定搜索关键字检索项目

    • 按前序,按中序或后序遍历二叉搜索树中的项目

 

  • Operations that extends the basic ADT binary tree to the ADT binary search tree are as follows:

    • Insert a new item into a binary search tree

    • Delete the item with a given search key from a binary search tree

    • Retrieve the item with a given search key from a binary search tree

    • Traverse the items in a binary search tree in preorder, inorder, or postorder

 

 

  • 在面向位置的ADT堆栈和队列的实现中,插入和删除到ADT中与数据的值无关

  • 但是,在二叉搜索树中,插入,删除和检索操作是按搜索键值,而不是按位置

  • 搜索键有助于搜索过程,因为我们只需知道关键信息即可找到特定实例或确定树中的正确位置

 

  • In the implementations of the position- oriented ADT stack, and queue, insertion and deletion into the ADT was independent of the value of the data

  • In the binary search tree, however, the insertion, deletion, and retrieval operations are by search-key value, not by position

  • The search key facilitates the search process, since we need to know only the key information to find a particular instance or to determine the proper position for it in the tree

 

  • 二叉树的遍历操作适用于二进制搜索树而不进行更改,因为二叉搜索树是二叉树

 

  • The traversal operations for a binary tree apply to a binary search tree without changes, because a binary search tree is a binary tree

 

ADT二进制搜索树操作的算法(Algorithms for the Operations of the ADT Binary Search Tree)

  • 由于二叉搜索树本质上是递归的,因此为其操作制定递归算法是很自然的

  • 搜索算法在二叉搜索树binarySearchTree中搜索搜索键为searchKey的项目

    • search(binarySearchTree,searchKey)

 

  • Since the binary search tree is recursive in nature, it is natural to formulate recursive algorithms for its operations

  • A search algorithm searches the binary search tree binarySearchTree for the item whose search key is searchKey

    • search(binarySearchTree, searchKey)

 

 

  • 搜索算法(A search algorithm)

                搜索(BinarySearchTree树,KeyType键)

                if(树为空){

                        找不到所需的记录

                } else if(key == root的项目的搜索键){

                         找到所需的记录

                } else if(key <root项目的搜索键){

                          搜索(树的左子树,键)

                }其他{

                          搜索(树的右子树,键)

                }

 

        search(BinarySearchTree tree, KeyType key) 

        if ( tree is empty){

            The desired record is not found

        }else if(key == search key of root’s item){

            The desired record is found

        }else if (key < search key of root’s item){

             search(Left subtree of tree, key)

         }else{

             search(Right subtree of tree, key)

         }

 

 

ADT二进制搜索树操作的算法:插入(Algorithms for the Operations of the ADT Binary Search Tree: Insertion)

 

  • 使用搜索确定树中插入新项目的位置始终可以轻松插入

  • 无论您在树中插入什么新项,搜索都将始终在空子树上终止

  • 因此,搜索始终会告诉您将项目作为新叶子插入

 

  • Using search to determine where in the tree to insert a new item always lead to an easy insertion

  • No matter what new item you insert into the tree, search will always terminate at the empty subtree

  • Thus, search always tells you to insert the item as a new leaf

 

  • 添加叶子只需更改父级中的相应引用,插入所需的工作几乎与相应的搜索相同

 

  • Adding a leaf requires only a change of the appropriate reference in the parent, the work required for an insertion is virtually the same as that for the corresponding search

 

  • insertItem(treeNode,newItem)

    • 将newItem插入到treeNode为根的二叉搜索树中

 

  • insertItem(treeNode, newItem) 

    • Inserts newItem into the binary search tree of which treeNode is the root

 

b) search terminates at a leaf 

c) insertion at a leaf

ADT二进制搜索树操作的算法:删除(Algorithms for the Operations of the ADT Binary Search Tree: Deletion)

  • 删除步骤

    • 使用搜索算法查找具有指定键的项目

    • 如果找到该项目,请从树中删除该项目

  • 节点N包含要删除的项目的三种可能情况

    • N是一片叶子

    • N只有一个孩子 

    • N有两个孩子

 

  • Steps for deletion

    • Use the search algorithm to locate the item with the specified key

    • If the item is found, remove the item from the tree

  • Three possible cases for node N containing the item to be deleted

    • N is a leaf

    • N has only one child 

    • N has two children

 

  • 删除节点N的策略 

    • 如果N是叶子

      • 将N的父级中的引用设置为null 

    •  如果N只有一个子级

      • 让N的父母领养N的孩子 

    • 如果N有两个孩子

      • 找到比树点N更容易从树中删除的另一个节点M.

      • 将M中的项目复制到N.

      • 从树中删除节点M.

        • 节点M可以

          • inorder successor – Bob右侧子树中最左边的节点,或者

          • Inorder previouscessor – Bob左侧子树中最右边的节点。

  • Strategies for deleting node N 

    • If N is a leaf

      • Set the reference in N’s parent to null 

    • If N has only one child

      • Let N’s parent adopt N’s child 

    • If N has two children

      • Locate another node M that is easier to remove from the tree than the node N

      • Copy the item that is in M to N

      • Remove the node M from the tree

        • Node M can be

          • inorder successor – the leftmost node in Bob’s right subtree, or

          • Inorder predecessor – the rightmost node in Bob’s left subtree.

 

ADT二进制搜索树操作的算法:检索(Algorithms for the Operations of the ADT Binary Search Tree: Retrieval)

  • 可以通过优化搜索算法来实现检索操作

    • 如果项目存在,则返回带有所需搜索键的项目

    • 否则,返回null引用

  • 二叉搜索树的遍历与二叉树的遍历相同

 

  • Retrieval operation can be implemented by refining the search algorithm

    • Return the item with the desired search key if it exists

    • Otherwise, return a null reference

  • Traversals for a binary search tree are the same as the traversals for a binary tree

 

基于参考的ADT二进制搜索树实现(A Reference-Based Implementation of the ADT Binary Search Tree)

 

  • BinarySearchTree

    • 扩展BinaryTreeBasis

    • 从BinaryTreeBasis继承以下内容•isEmpty()

      • makeEmpty()

      • getRootItem()

      • 构造函数的使用

  • TreeIterator

    • 可与BinarySearchTree一起使用

 

  • BinarySearchTree

  • Extends BinaryTreeBasis

  • Inherits the following from BinaryTreeBasis • isEmpty()

    • makeEmpty()

    • getRootItem()

  • The use of the constructors

  • TreeIterator

    • Can be used with BinarySearchTree

 

二叉搜索树操作的效率(The Efficiency of Binary Search Tree Operations)

 

具有七个节点的最大高度二叉树

  • 检索,插入或删除的最大比较次数是树的高度

  • 二叉搜索树的最大和最小高度

    • n是具有n个节点的二叉树的最大高度

 

A maximum-height binary tree with seven nodes

  • The maximum number of comparisons for a retrieval, insertion, or deletion is the height of the tree

  • The maximum and minimum heights of a binary search tree

    • n is the maximum height of a binary tree with n nodes

 

 

 

 

ADT二叉搜索树的基于引用的实现的检索,插入,删除和遍历操作的顺序

The order of the retrieval, insertion, deletion, and traversal operations for the reference-based implementation of the ADT binary search tree

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