二进制搜索树_二进制搜索树数据结构举例说明

二进制搜索树

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

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

  1. Each tree has a root node (at the top) having some value.

    每棵树都有一个具有某些值的根节点(在顶部)。
  2. The root node has zero or more child nodes.

    根节点具有零个或多个子节点。
  3. Each child node has zero or more child nodes, and so on. This create a subtree in the tree. Every node has it’s own subtree made up of his 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 up 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).

BST建立在二进制搜索算法的基础上,该算法允许快速查找,插入和删除节点。 设置它们的方式意味着,平均而言,每个比较操作都可以跳过树的大约一半,因此每次查找,插入或删除操作所花费的时间与树中存储的项目数的对数成正比, 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.

但是,有时会发生最坏的情况,即树不平衡且所有这三个函数的时间复杂度均为O(n) 。 这就是为什么自平衡树(AVL,红黑色等)比基本BST更有效的原因。

Worst case scenario example: This can happen when you keep adding nodes that are always larger than the node before (it’s 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.

    删除:从树中删除节点。

创造 (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.

最初会创建一个没有任何节点的空树。 必须指向根节点的

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值