二叉搜索树bst

In this tutorial, we’ll be discussing the Binary Search Tree Data Structure. We’ll be implementing the functions to search, insert and remove values from a Binary Search Tree. We’ll implement these operations recursively as well as iteratively.

在本教程中,我们将讨论二进制搜索树数据结构。 我们将实现在Binary Search Tree中搜索,插入和删除值的功能。 我们将以递归方式和迭代方式实施这些操作。

二进制搜索树 (Binary Search Tree)

A Binary Search tree has the following property:

二叉搜索树具有以下属性:

  • All nodes should be such that the left child is always less than the parent node.所有节点的左子节点应始终小于父节点。
  • The right child is always greater than the parent node.正确的子节点始终大于父节点。

In the following sections, we’ll see how to search, insert and delete in a BST recursively as well as iteratively.

在以下各节中,我们将了解如何以递归和迭代方式在BST中搜索,插入和删除。

Let’s create our Binary Tree Data Structure first:

让我们首先创建我们的二叉树数据结构:

public class BinaryTree {public TreeNode root;public static class TreeNode {public TreeNode left;public TreeNode right;public Object data;public TreeNode(Object data) {this.data = data;left = right = null;}}
}

Note that the above implementation is not a binary search tree because there is no restriction in inserting elements to the tree.

注意,上述实现不是二叉搜索树,因为在树中插入元素没有限制。

BST递归搜索 (BST Search Recursively)

The following java program contains the function to search a value in a BST recursively.

以下Java程序包含用于在BST中递归搜索值的函数。

public class SearchInsertRemoveFromTree {public static void main(String[] args) {/***   Our Example Binary Search Tree*       10*     5    20*   4  8  15 25*/BinaryTree tree = new BinaryTree();tree.root = new TreeNode(10);tree.root.left = new TreeNode(5);tree.root.right = new TreeNode(20);tree.root.left.left = new TreeNode(4);tree.root.left.right = new TreeNode(8);tree.root.right.left = new TreeNode(15);tree.root.right.right = new TreeNode(25);System.out.println("Search Value 2 is in tree? " + searchRecursively(tree.root, 2));System.out.println("Search Value 10 in tree? " + searchRecursively(tree.root, 10));}public static boolean searchRecursively(TreeNode root, int value) {if (root == null)return false;if ((int) root.data == value)return true;if (value < (int) root.data)return searchRecursively(root.left, value);else if (value > (int) root.data)return searchRecursively(root.right, value);return false;}
}

The output is:

输出为:

BST迭代搜索 (BST Search Iteratively)

To search iteratively, use the following method instead:

要迭代搜索,请改用以下方法:

public static boolean searchIteratively(TreeNode root, int value) {while (root != null) {if ((int) root.data == value)return true;if (value < (int) root.data)root = root.left;elseroot = root.right;}return false;}

Let's look at how to insert a new node in a Binary Search Tree.

让我们看一下如何在二进制搜索树中插入新节点。

BST递归插入 (BST Insertion Recursively)

public static TreeNode insertionRecursive(TreeNode root, int value) {if (root == null)return new TreeNode(value);if (value < (int) root.data) {root.left = insertionRecursive(root.left, value);} else if (value > (int) root.data) {root.right = insertionRecursive(root.right, value);}return root;}public static void printInorderTraversal(TreeNode root) {if (root != null) {printInorderTraversal(root.left);System.out.print(root.data + " ");printInorderTraversal(root.right);}}

Call the above method in the main method:

在main方法中调用上述方法:

tree.root = insertionRecursive(tree.root, 24);
tree.root = insertionRecursive(tree.root, 2);
printInorderTraversal(tree.root);

The tree is printed in the form of inorder traversal.

该树以有序遍历的形式打印。

BST插入迭代 (BST Insertion Iterative)

To insert a Node iteratively in a BST tree, we will need to traverse the tree using two pointers.

要将节点迭代地插入BST树中,我们将需要使用两个指针遍历该树。

public static TreeNode insertionIterative(TreeNode root, int value) {TreeNode current, parent;TreeNode tempNode = new TreeNode(value);if (root == null) {root = tempNode;return root;} else {current = root;}while (true) {parent = current;if (value < (int) current.data) {current = current.left;if (current == null) {parent.left = tempNode;return root;}} else if (value > (int) current.data) {current = current.right;if (current == null) {parent.right = tempNode;return root;}}}}

BST递归删除元素 (BST Removing Element Recursively)

Removing an element from a BST is a little complex than searching and insertion since we must ensure that the BST property is conserved.

从BST中删除元素比搜索和插入要复杂一些,因为我们必须确保BST属性得到保留。

To delete a node we need first search it. Then we need to determine if that node has children or not.

要删除节点,我们需要先搜索它。 然后,我们需要确定该节点是否有子节点。

  • If no children - Just delete.如果没有孩子 -请删除。
  • If a single child - Copy that child to the node.如果是单个孩子 -将那个孩子复制到该节点。
  • If two children - Determine the next highest element (inorder successor) in the right subtree. Replace the node to be removed with the inorder successor. Delete the inorder successor duplicate.如果有两个孩子 -确定右子树中的下一个最高元素(顺序后继)。 用顺序后继节点替换要删除的节点。 删除顺序的后继副本。
The inorder successor can be obtained by finding the minimum value in right child of the node.
可通过在节点的右子节点中找到最小值来获得有序后继者。

The following java program removes elements from a BST:

以下Java程序从BST中删除元素:

public static TreeNode deleteRecursively(TreeNode root, int value) {if (root == null)return root;if (value < (int) root.data) {root.left = deleteRecursively(root.left, value);} else if (value > (int) root.data) {root.right = deleteRecursively(root.right, value);} else {if (root.left == null) {return root.right;} else if (root.right == null)return root.left;root.data = inOrderSuccessor(root.right);root.right = deleteRecursively(root.right, (int) root.data);}return root;}public static int inOrderSuccessor(TreeNode root) {int minimum = (int) root.data;while (root.left != null) {minimum = (int) root.left.data;root = root.left;}return minimum;}

Call the above delete method in the main method:

main方法中调用上述delete方法:

tree.root = deleteRecursively(tree.root, 4);
tree.root = deleteRecursively(tree.root, 20);
printInorderTraversal(tree.root);

The output is:
2 5 8 10 15 24 25

输出为:
2 5 8 10 15 24 25

Let's do the same iteratively.

让我们反复进行相同的操作。

BST迭代删除元素 (BST Removing Element Iteratively)

public static TreeNode deleteNodeIteratively(TreeNode root, int value) {TreeNode parent = null, current = root;boolean hasLeft = false;if (root == null)return root;while (current != null) {if ((int) current.data == value) {break;}parent = current;if (value < (int) current.data) {hasLeft = true;current = current.left;} else {hasLeft = false;current = current.right;}}if (parent == null) {return deleteNodeIteratively(current);}if (hasLeft) {parent.left = deleteNodeIteratively(current);} else {parent.right = deleteNodeIteratively(current);}return root;}private static TreeNode deleteNodeIteratively(TreeNode node) {if (node != null) {if (node.left == null && node.right == null) {return null;}if (node.left != null && node.right != null) {TreeNode inOrderSuccessor = deleteInOrderSuccessorDuplicate(node);node.data = inOrderSuccessor.data;} else if (node.left != null) {node = node.left;} else {node = node.right;}}return node;}private static TreeNode deleteInOrderSuccessorDuplicate(TreeNode node) {TreeNode parent = node;node = node.right;boolean rightChild = node.left == null;while (node.left != null) {parent = node;node = node.left;}if (rightChild) {parent.right = node.right;} else {parent.left = node.right;}node.right = null;return node;}

h is the height of the tree.
h是树的高度。

That brings an end to this tutorial.

这样就结束了本教程。

GitHub Repository.GitHub存储库中检出完整的代码以及更多DS和算法示例。

翻译自: https://www.journaldev.com/23086/binary-search-tree-bst-search-insert-remove

二叉搜索树bst

二叉搜索树bst_二进制搜索树(BST)–搜索插入和删除相关推荐

  1. 二叉平衡树之二叉树搜索树【咱们一起手动模拟实现】

    目录 1.什么是二叉搜索树? 2.手动模拟二叉搜索树 2.1.整体代码 2.2.查找数据 2.3.插入数据 2.4.删除数据 3.性能分析 1.什么是二叉搜索树? 二叉搜索树也叫作二叉排序树,可以使一 ...

  2. 利用C语言实现二叉搜索树的遍历、查找、插入、删除

    IDE:codebloks,编译器:gcc5.1.0 二叉搜索树和我们通常的二叉树还是有一定的区别,顾名思义,一颗二叉搜索树以一颗二叉树来组织,其中每一个结点就是一个对象.除了key(关键字)和卫星数 ...

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

    二进制搜索树 A tree is a data structure composed of nodes that has the following characteristics: 树是由具有以下特 ...

  4. 中根遍历二叉查找树所得序列一定是有序序列_二叉搜索树(BST)

    点击上方"蓝字",发现更多精彩. 前面我们介绍了树的基本概念,并引出了二叉树.值得注意的是,无特征的二叉树在工程上几乎没啥用处,一般都是使用bst.avl,trie,rbtree等 ...

  5. 【数据结构与算法基础】二叉搜索树和平衡二叉树

    写在前面 今天学习在排序和查找中都很有用的特殊二叉树,平衡二叉树和搜索二叉树. 相关代码实现已上传至Github:data_structure/Tree/ 1.二叉搜索树(Binary Search ...

  6. 详解二叉搜索树的增删改查

    文章目录 二叉搜索树 1.1 定义 二.二叉搜索树基本结构 三.二叉搜索树的具体实现 3.1 插入节点 3.2 查询节点 3.3 删除节点(⭐️) 3.4 二叉搜索树的遍历 四.二叉搜索树的性能分析和 ...

  7. 真c++ 从二叉树到红黑树(4)之二叉平衡搜索树AVL

      此文章为从二叉树到红黑树系列文章的第四节,主要介绍介绍二叉平衡搜索树AVL,当你理解了AVL,红黑树你就理解了一半了! 文章目录 一.前面文章链接~(点击右边波浪线可以返回目录) 二.由BST引入 ...

  8. 【算法导论】 二叉搜索树、AVL树、和红黑树

    二叉搜索树 二叉搜索树是一颗二叉树或一颗空树且满足以下性质: 1)根节点 x的key值大于任意左子树上节点的key值,小于右子树上任意节点的key值 : 2)其左右子树也分别是一颗二叉搜索树. 使用二 ...

  9. 红黑树算法原理(从二叉搜索树讲起)

    原文:红黑树深入剖析及Java实现,本文修改了原文的一些小错误,如果想看红黑树的Java实现可以到原文去看. 红黑树是平衡二叉查找树的一种.为了深入理解红黑树,我们需要从二叉查找树开始讲起. BST ...

最新文章

  1. zbb20170824 oracle expdp/impdp 导入导出数据
  2. 文件处理-写模式操作文件
  3. java开发websocket聊天室_java实现基于websocket的聊天室
  4. 体积最小桌面linux,Tiny Core Linux - 体积最小的精简 Linux 操作系统发行版之一 (仅10多MB) - 蓝月网络...
  5. 跨域问题解决(我只是搬运工)
  6. 技术实践丨体验量子神经网络在自然语言处理中的应用
  7. 红米Redmi品牌独立首战告捷:半月多出货超100万
  8. amazeui学习笔记--css(布局相关2)--等分网格 AVG Grid
  9. 《Java并发编程实践》读书笔记
  10. wpf esc key 检测不到_爬虫笔记之requests检测网站编码方式(zozo.jp)(碎碎念) - CC11001100...
  11. 大数据架构方案总结-ljt(转载)
  12. JVM内存分配担保机制
  13. 生鲜电商、社区团购、团长中心、地址管理、自提点、订单列表、限时折扣、预售、会员储值、钱包、同城配送、门店自提、团长自提、采购、履约、仓储、运输、财务、移动端电商原型、rp源文件、axure电商原型
  14. CSR蓝牙开发调试经验
  15. 访问Windows 11恢复环境的5种简单方法
  16. 上海亚商投顾大盘回顾:两市成交仅5600亿元 创两年半以来新低
  17. 解决VSCode终端Ctrl+V无法粘贴问题
  18. SpringBoot(一):项目概述
  19. 计算机冯诺依曼体系结构与哈佛体系结构
  20. Haxe FD 开发学习

热门文章

  1. 深入理解计算机系统(2.4)---C语言的有符号与无符号、二进制整数的扩展与截断...
  2. VC++2012编程演练数据结构《36》磁盘文件进行排序
  3. [转载] Numpy之logspace
  4. [转载] 使用Python中的NLTK和spaCy删除停用词与文本标准化
  5. warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory
  6. SQL SERVER导入EXCEL文件:无法创建链接服务器 (null) 的 OLE DB 访问接口 Microsoft.Ace.OLEDB.12.0 的实例。...
  7. Linux学习第一篇之Linux系统安装——系统分区
  8. (转)Fabric 1.0 读写集
  9. MyEclipse的html页面 design视图中 关闭可视化界面
  10. 【Flask】 结合wtforms的文件上传表单