二进制搜索树

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.

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

搜索 (Search)

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 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是结构化的(按照其定义),所以右孩子总是比父孩子大,而左孩子总是小。

插 (Insert)

It is very similar to the search function. You again start at the root of the tree and go down recursively, searching for the right place to insert our new node, in the same way as explained in the search function. If a node with the same value is already in the tree, you can choose to either insert the duplicate or not. Some trees allow duplicates, some don’t. It depends on the certain implementation.

它与搜索功能非常相似。 您再次从树的根开始,然后递归地向下搜索,以与搜索功能中所述相同的方式搜索插入我们新节点的正确位置。 如果树中已经存在具有相同值的节点,则可以选择是否插入重复项。 有些树允许重复,有些则不允许。 这取决于特定的实现。

删除 (Delete)

There are 3 cases that can happen when you are trying to delete a node. If it has,

当您尝试删除节点时,可能会发生3种情况。 如果有的话

  1. No subtree (no children): This one is the easiest one. You can simply just delete the node, without any additional actions required.无子树(无子树):这是最简单的树。 您只需删除节点即可,而无需任何其他操作。
  2. One subtree (one child): You have to make sure that after the node is deleted, its child is then connected to the deleted node’s parent.一个子树(一个子树):您必须确保在删除节点后,将其子树连接到已删除节点的父树。
  3. Two subtrees (two children): You have to find and replace the node you want to delete with its successor (the letfmost node in the right subtree).两个子树(两个子树):必须找到要删除的节点并将其替换为其后继节点(右侧子树中的letfmost节点)。

The time complexity for creating a tree is O(1). The time complexity for searching, inserting or deleting a node depends on the height of the tree h, so the worst case is O(h).

创建树的时间复杂度为O(1) 。 搜索,插入或删除节点的时间复杂度取决于树h的高度,因此最坏的情况是O(h)

节点的前身 (Predecessor of a node)

Predecessors can be described as the node that would come right before the node you are currently at. To find the predecessor of the current node, look at the right-most/largest leaf node in the left subtree.

前任节点可以描述为您当前所在节点之前的节点。 要查找当前节点的前任节点,请查看左侧子树中最右侧/最大的叶节点。

节点的后继者 (Successor of a node)

Successors can be described as the node that would come right after the node you are currently at. To find the successor of the current node, look at the left-most/smallest leaf node in the right subtree.

继任者可以描述为您当前所在节点之后的节点。 要查找当前节点的后继节点,请查看右侧子树中最左侧/最小的叶节点。

特殊类型的BT (Special types of BT)

  • Heap堆
  • Red-black tree红黑树
  • B-treeB树
  • Splay tree八叉树
  • N-ary treeN元树
  • Trie (Radix tree)特里(基数树)

运行 (Runtime)

数据结构:数组 (Data structure: Array)

  • Worst-case performance: O(log n)

    最坏情况下的性能: O(log n)

  • Best-case performance: O(1)

    最佳情况下的性能: O(1)

  • Average performance: O(log n)

    平均表现: O(log n)

  • Worst-case space complexity: O(1)

    最坏情况下的空间复杂度: O(1)

Where n is the number of nodes in the BST.

其中n是BST中的节点数。

实施BST (Implementation of BST)

Here’s a definiton for a BST node having some data, referencing to its left and right child nodes.

这是具有一些数据的BST节点的定义,参考其左子节点和右子节点。

struct node {int data;struct node *leftChild;struct node *rightChild;
};

搜索操作 (Search Operation)

Whenever an element is to be searched, start searching from the root node. Then if the data is less than the key value, search for the element in the left subtree. Otherwise, search for the element in the right subtree. Follow the same algorithm for each node.

每当要搜索元素时,都从根节点开始搜索。 然后,如果数据小于键值,则在左侧子树中搜索元素。 否则,在右子树中搜索该元素。 每个节点遵循相同的算法。

struct node* search(int data){struct node *current = root;printf("Visiting elements: ");while(current->data != data){if(current != NULL) {printf("%d ",current->data);//go to left treeif(current->data > data){current = current->leftChild;}//else go to right treeelse {                current = current->rightChild;}//not foundif(current == NULL){return NULL;}}           }return current;
}

插入操作 (Insert Operation)

Whenever an element is to be inserted, first locate its proper location. Start searching from the root node, then if the data is less than the key value, search for the empty location in the left subtree and insert the data. Otherwise, search for the empty location in the right subtree and insert the data.

每当要插入元素时,请先找到其正确位置。 从根节点开始搜索,然后如果数据小于键值,则在左侧子树中搜索空位置并插入数据。 否则,请在右侧子树中搜索空白位置并插入数据。

void insert(int data) {struct node *tempNode = (struct node*) malloc(sizeof(struct node));struct node *current;struct node *parent;tempNode->data = data;tempNode->leftChild = NULL;tempNode->rightChild = NULL;//if tree is emptyif(root == NULL) {root = tempNode;} else {current = root;parent = NULL;while(1) {                parent = current;//go to left of the treeif(data < parent->data) {current = current->leftChild;                //insert to the leftif(current == NULL) {parent->leftChild = tempNode;return;}}//go to right of the treeelse {current = current->rightChild;//insert to the rightif(current == NULL) {parent->rightChild = tempNode;return;}}}            }
}

Binary search trees (BSTs) also give us quick access to predecessors and successors. Predecessors can be described as the node that would come right before the node you are currently at.

二进制搜索树(BST)也使我们可以快速访问前任和后继。 先前的节点可以描述为您当前所在节点之前的节点。

  • To find the predecessor of the current node, look at the rightmost/largest leaf node in the left subtree. Successors can be described as the node that would come right after the node you are currently at.要查找当前节点的前任节点,请查看左侧子树中最右侧/最大的叶节点。 继任者可以描述为您当前所在节点之后的节点。
  • To find the successor of the current node, look at the leftmost/smallest leaf node in the right subtree.要查找当前节点的后继节点,请查看右侧子树中最左侧/最小的叶节点。

让我们看一下在树上运行的几个过程。 (Let’s look at a couple of procedures operating on trees.)

Since trees are recursively defined, it’s very common to write routines that operate on trees that are themselves recursive.

由于树是递归定义的,因此编写对本身是递归的树进行操作的例程非常普遍。

So for instance, if we want to calculate the height of a tree, that is the height of a root node, We can go ahead and recursively do that, going through the tree. So we can say:

因此,例如,如果我们要计算树的高度(即根节点的高度),则可以继续并递归地遍历树。 所以我们可以说:

  • For instance, if we have a nil tree, then its height is a 0.例如,如果我们有一棵零树,那么它的高度是0。
  • Otherwise, We’re 1 plus the maximum of the left child tree and the right child tree.否则,我们为1加上左子树和右子树的最大值。

So if we look at a leaf for example, that height would be 1 because the height of the left child is nil, is 0, and the height of the nil right child is also 0. So the max of that is 0, then 1 plus 0.

因此,例如,如果查看一片叶子,则该高度将为1,因为左子级的高度为nil,为0,而nil右级子级的高度也为0。因此,该最大值为0,则为1加0。

高度(树)算法 (Height(tree) algorithm)

if tree = nil:
return 0
return 1 + Max(Height(tree.left),Height(tree.right))

这是C ++中的代码 (Here is the code in C++)

int maxDepth(struct node* node)
{if (node==NULL)return 0;else{int rDepth = maxDepth(node->right);int lDepth = maxDepth(node->left);if (lDepth > rDepth){return(lDepth+1);}else{return(rDepth+1);}}
}

We could also look at calculating the size of a tree that is the number of nodes.

我们还可以查看计算树的大小(即节点数)。

  • Again, if we have a nil tree, we have zero nodes.同样,如果我们有一个零树,那么我们有零个节点。

Otherwise, we have the number of nodes in the left child plus 1 for ourselves plus the number of nodes in the right child. So 1 plus the size of the left tree plus the size of the right tree.

否则,我们得到左子节点中的节点数加上自己的1,再加上右子节点中的节点数。 所以1加左树的大小再加上右树的大小。

大小(树)算法 (Size(tree) algorithm)

if tree = nil
return 0
return 1 + Size(tree.left) + Size(tree.right)

这是C ++中的代码 (Here is the code in C++)

int treeSize(struct node* node)
{if (node==NULL)return 0;elsereturn 1+(treeSize(node->left) + treeSize(node->right));
}

freeCodeCamp YouTube频道上的相关视频 (Relevant videos on freeCodeCamp YouTube channel)

  • Binary Search Tree

    二进制搜索树

  • Binary Search Tree: Traversal and Height

    二进制搜索树:遍历和高度

以下是二叉树的常见类型: (Following are common types of Binary Trees:)

Full Binary Tree/Strict Binary Tree: A Binary Tree is full or strict if every node has exactly 0 or 2 children.

完整的二叉树/严格的二叉树:如果每个节点恰好具有0或2个子节点,则二叉树是完整的或严格的。

18/       \  15         30  /  \        /  \40    50    100   40

In Full Binary Tree, number of leaf nodes is equal to number of internal nodes plus one.

在完整二叉树中,叶节点数等于内部节点数加一。

Complete Binary Tree: A Binary Tree is complete Binary Tree if all levels are completely filled except possibly the last level and the last level has all keys as left as possible

完整的二叉树:如果所有级别都已完全填充,则二叉树就是完整的二叉树,除了最后一个级别,并且最后一个级别的所有键都尽可能保留

18/       \  15         30  /  \        /  \40    50    100   40/  \   /
8   7  9

翻译自: https://www.freecodecamp.org/news/binary-search-tree-what-is-it/

二进制搜索树

二进制搜索树_二进制搜索树数据结构举例说明相关推荐

  1. java 删除二进制内容_二进制搜索树节点删除不删除替换Java

    我试图从二进制搜索树中删除节点.除了一个特定的情况,我可以成功删除树上的任何其他节点.如果目标节点有2个子节点,并且左子节点具有右子树,我可以找到正确的替换节点并将值切换到目标节点,但是永远不会删除替 ...

  2. java 删除二进制内容_二进制搜索树节点删除

    我正在实现从二叉搜索树中删除节点的功能 . 该功能的原型已设置,我无法更改它,这是一项学校作业 . 我的代码: typedef struct tBSTNode { char Key; struct t ...

  3. c++将十进制转换为二进制 小数_二进制、八进制、十六进制与转换

    将二进制.八进制.十六进制转换为十进制 二进制.八进制和十六进制向十进制转换都是非常容易的,就是"按权相加". 所谓"权",也即"位权". ...

  4. 二进制搜索算法_二进制搜索的一个扭曲故事

    二进制搜索算法 by Divya Godayal 通过Divya Godayal 二进制搜索的一个扭曲故事 (A twisted tale of Binary Search) Awesome. Tha ...

  5. mysql二进制格式_二进制格式安装 MySQL

    二进制格式安装 MySQL 什么是通用二进制格式? 已经编译进行过编译的软件包, 下载到本机直接解压到特定的目录下就可以使用的格式. 1. 查询本地是否安装 mysql 数据库相关的软件包 (卸载之) ...

  6. 二进制搜索算法_使用安全摄像机镜头解释二进制搜索算法

    二进制搜索算法 by Julia Geist Julia·盖斯特(Julia Geist) 使用安全摄像机镜头解释二进制搜索算法 (Binary Search Algorithms explained ...

  7. 二进制除法移位相减_二进制除法计算器

    二进制除法怎么算(共2篇)二进制的运算法则1 2 微型计算机运算基础1 2 1 二进制数的运算方法电子计算机具有强大的运算能力,它可以进行两种运算:算术运算和逻辑运算.1.二进制数的算术运算二进制数的 ...

  8. c++十进制转二进制_二进制与十进制如何互相转换?

    正整数的十进制转换二进制 将一个十进制数除以二,得到的商再除以二,依此类推直到商等于一或零时为止,倒取除得的余数,即换算为二进制数的结果.只需记住要点:除二取余,倒序排列. 由于计算机内部表示数的字节 ...

  9. 二进制树形搜索算法_二进制搜索

    二进制树形搜索算法 二进制搜索用于在 值的排序列表 . 它选择排序值数组中的中间元素,并将其与目标值进行比较: 这就是我们在数组中寻找的关键. 如果它小于目标值,则在中间元素之后搜索,直到数组末尾. ...

最新文章

  1. [SQL Server]无法创建 SSIS 运行时对象,请验证 DTS.dll 是否可用及是否已注册
  2. Fabric学习笔记-智能合约
  3. fortran subroutine_Fortran:派生数组与数组传递进子程序耗费时间比较
  4. 安装配置opensips过程记录
  5. 要闻君说:台积电将为iPhone生产5纳米A系列芯片?腾讯云TStack与银河麒麟完成互认证……...
  6. 黑客攻防技术宝典Web实战篇第2版—第9章 攻击数据存储区
  7. 项目分享 | 好牛X的开源项目,看完忍不住分享(高手作品分享)
  8. 千元满血续航王!iQOO Z5发布 售价1799元起
  9. 【数据结构(C语言)】数据结构-表
  10. C# WinForm窗体上的按钮结束回车确认
  11. wordpress建立数据库连接时出错
  12. 高性能MYSQL(查询优化)
  13. 服务器系统万能驱动,IT天空万能驱动程序
  14. C# wpf确认取消MessageBox选择按钮
  15. colorbox iframe小记
  16. oracle 中 的 =,oracle中=是什么意思
  17. raster包—projectRaster函数
  18. 数据库实验——简单数据库应用系统设计与实现
  19. python 模拟键盘鼠标输入_Python模拟键盘输入和鼠标操作
  20. WoShop跨境电商USDT支付语言插件全开源无加密商城源码

热门文章

  1. input输入框为number类型时,去掉上下小箭头
  2. 在?三缺一,来斗个地主——肝个斗地主案例(java)
  3. ps制作20种特效文字_ps技巧:给照片制作特效(刀光剑影)
  4. IOS手机全屏长按识别二维码HTML代码
  5. 20-flutter下拉刷新与上拉加载
  6. iOS HitTest 机制
  7. .NET 程序设计实验 含记事本通讯录代码
  8. R语言文摘:Subsetting Data
  9. Python编写Hive UDF
  10. 《统一沟通-微软-实战》-5-部署-SharePoint Server 2010