bst 二叉搜索树_二进制搜索树:BST附带示例

bst 二叉搜索树

什么是二叉搜索树? (What is a Binary Search Tree?)

A tree is a data structure composed of nodes that has the following characteristics:

树是由具有以下特征的节点组成的数据结构:

  1. Each tree has a root node at the top (also known as Parent Node) containing some value (can be any datatype).

    每棵树的顶部都有一个根节点(也称为父节点),其中包含一些值(可以是任何数据类型)。
  2. The root node has zero or more child nodes.

    根节点具有零个或多个子节点。
  3. Each child node has zero or more child nodes, and so on. This creates a subtree in the tree. Every node has its own subtree made up of its children and their children, etc. This means that every node on its own can be a tree.

    每个子节点都有零个或多个子节点,依此类推。 这将在树中创建一个子树。 每个节点都有由其子代及其子代组成的子树。这意味着每个节点自身都可以是一棵树。

A binary search tree (BST) adds these two characteristics:

二进制搜索树(BST)添加了以下两个特征:

  1. Each node has a maximum of up to two children.

    每个节点最多有两个子节点。
  2. For each node, the values of its left descendent nodes are less than that of the current node, which in turn is less than the right descendent nodes (if any).

    对于每个节点,其左后代节点的值小于当前节点的值,而当前节点又小于右后代节点的值(如果有)。

The BST is built on the idea of the binary search algorithm, which allows for fast lookup, insertion and removal of nodes. The way that they are set up means that, on average, each comparison allows the operations to skip about half of the tree, so that each lookup, insertion or deletion takes time proportional to the logarithm of the number of items stored in the tree,  O(log n) . However, some times the worst case can happen, when the tree isn't balanced and the time complexity is  O(n)  for all three of these functions. That is why self-balancing trees (AVL, red-black, etc.) are a lot more effective than the basic BST.

BST基于二进制搜索算法的思想,该算法允许快速查找,插入和删除节点。 设置它们的方式意味着,平均而言,每个比较操作都可以跳过树的大约一半,因此每次查找,插入或删除操作所花费的时间与树中存储的项目数的对数成正比, O(log n) 。 但是,有时会发生最坏的情况,即树不平衡且所有这三个函数的时间复杂度均为O(n) 。 这就是为什么自平衡树(AVL,红黑色等)比基本BST更有效的原因。

Worst case scenario example:  This can happen when you keep adding nodes that are  always  larger than the node before (its parent), the same can happen when you always add nodes with values lower than their parents.

最坏的情况示例:当您添加的节点总是大于其前一个节点(其父节点)时,可能会发生这种情况;当您始终添加值小于其父节点的节点时,也会发生这种情况。

BST的基本操作 (Basic operations on a BST)

  • Create: creates an empty tree.

    创建:创建一个空树。
  • Insert: insert a node in the tree.

    插入:在树中插入一个节点。
  • Search: Searches for a node in the tree.

    搜索:在树中搜索节点。
  • Delete: deletes a node from the tree.

    删除:从树中删除节点。
  • Inorder: in-order traversal of the tree.

    有序:树的有序遍历。
  • Preorder: pre-order traversal of the tree.

    预购:预购遍历树。
  • Postorder: post-order traversal of the tree.

    后置顺序:树的后置顺序遍历。
创造 (Create)

Initially an empty tree without any nodes is created. The variable/identifier which must point to the root node is initialized with a  NULL  value.

最初会创建一个没有任何节点的空树。 必须指向根节点的变量/标识符使用NULL值初始化。

You always start searching the tree at the root node and go down from there. You compare the data in each node with the one you are looking for. If the compared node doesn't match then you either proceed to the right child or the left child, which depends on the outcome of the following comparison: If the node that you are searching for is lower than the one you were comparing it with, you proceed to the left child, otherwise (if it's larger) you go to the right child. Why? Because the BST is structured (as per its definition), that the right child is always larger than the parent and the left child is always lesser.

您总是开始在根节点上搜索树,然后从那里向下走。 您将每个节点中的数据与要查找的节点进行比较。 如果比较的节点不匹配,那么您将继续选择右边的子节点还是左边的子节点,这取决于以下比较的结果:如果您要搜索的节点比您要比较的节点低,您继续向左孩子前进,否则(如果较大),您将向右孩子前进。 为什么? 因为BST是结构化的(按照其定义),所以右孩子总是比父孩子大,而左孩子总是小。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值