From 3fa68a7ac8fe95190b3e5893b1d469c6befce687 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Tue, 13 Apr 2021 12:06:57 +0800 Subject: [PATCH] =?UTF-8?q?post:=E5=A2=9E=E5=8A=A0=E4=BA=8C=E5=8F=89?= =?UTF-8?q?=E6=90=9C=E7=B4=A2=E6=A0=91=E6=96=87=E7=AB=A0=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/_posts/binary-search-tree.md | 169 +++++++++++++++++++++++ source/_posts/binary-search-tree/bst.svg | 3 + source/_posts/binary-tree-basic.md | 5 + 3 files changed, 177 insertions(+) create mode 100644 source/_posts/binary-search-tree.md create mode 100644 source/_posts/binary-search-tree/bst.svg diff --git a/source/_posts/binary-search-tree.md b/source/_posts/binary-search-tree.md new file mode 100644 index 0000000..e51680c --- /dev/null +++ b/source/_posts/binary-search-tree.md @@ -0,0 +1,169 @@ +--- +title: 二叉查找树 +date: 2021-04-13 11:16:29 +tags: [架构知识, 算法设计, 树形结构, 数据结构, 二叉树, 查找树, Java] +categories: + - [架构知识, 算法设计] + - [架构知识, 数据结构] +mathjax: true +--- +二叉查找树(Binary Search Tree,BST)是一种特殊的二叉树,BST通过定义节点的左孩子和右孩子的约定关系,提升了二叉树节点的搜索效率。 + + +相比普通二叉树,BST有以下额外的特点: + +- 对于任意一个节点n,其左子树下的每个后代节点的值,都小于节点n的值。 +- 对于任意一个节点n,其右子树下的每个后代节点的值,都大于节点n的值。 + +以下是一个BST的示例。 + +{% asset_img bst.svg 300 %} + +可能看着这个二叉树比较丑陋,但是这的确是一个现实中常常会碰到的BST。有二叉树排序经验的读者可能会觉得这个二叉树十分熟悉,的确,BST也常常用来做排序,这和你所看到和用到的二叉树排序几乎是一样的。 + +## BST构建 + +在使用BST之前,需要先对之前定义的二叉树节点类稍微扩展一下。 + +``` java +public class BSTNode extends Node implements Comparable { + public BSTNode(Integer value) { + super(value); + } + + public bool isLeftChild() { + return this.getParent() + .map(parent -> this.compareTo(parent) < 0) + .orElse(false); + } + + public bool isRightChild() { + return this.getParent() + .map(parent -> this.compareTo(parent) > 0) + .orElse(false); + } + + @Override + public int compareTo(BSTNode other) { + return this.value - other.getValue(); + } +} +``` + +## 节点搜索 + +在理论上,使用BST查找节点可以使时间减半。要搜索一个节点,BST需要利用以下步骤。 + +1. 如果给定要搜索的值c为空,那么这个值必定不在BST中。 +1. 取得当前节点n,如果是搜索起始,此时的节点n是BST的根节点。 +1. 比较给定值c和节点n,如果值相同,则节点n为所需要的节点。 +1. 如果节点n的值大于给定值c,那么所需要的节点应该存在于节点n的左子树中。此时可以向节点n的左子树前进一层,即将节点n的左孩子作为下一次比较的节点n。 +1. 如果节点n的值小于给定值c,那么所需要的节点应该存在于节点n的右子树中。此时可以向节点n的右子树前进一层,即将节点n的右孩子作为下一次比较的节点n。 + +BST的这种搜索策略,查找一个节点不需要遍历整个树,只需要遍历树中的一个路径即可,这个遍历的复杂度为$O(log_2N)$。以下给出一个BST的搜索方法实现示例。 + +```java +public Node search(Integer value) { + LinkedList queue = new LinkedList<>(); + queue.push(this.root); + while (!queue.isEmpty()) { + Node pointer = queue.pollFirst(); + if (pointer.getValue() == value) { + return pointer; + } + if (pointer.getValue() > value) { + pointer.getLeftChild().ifPresent(queue::push) + } + if (pointer.getValue() < value) { + pointer.getRightChild().ifPresent(queue::push); + } + } +} +``` + +但是对于BST来说,其搜索效率完全取决于其树的拓扑结构。如果树中只存在一条路径,那么搜索的复杂度就会变成$O(n)$。所以这也是后面引入自平衡二叉查找树的原因。 + +## 插入节点 + +由于BST是一个十分有序的结构,任何违反BST规则的树结构都会使BST的搜索失效。所以向BST中插入一个节点的时候,最重要的是定位新节点的父节点,而且新插入的节点始终是作为叶子节点的,不存在需要重新排列的问题。在BST中定位新节点n的父节点的步骤如下。 + +1. 如果是搜索起始,当前节点c为BST的根节点。 +1. 如果节点c为空,则节点c的父节点p即为节点n的父节点。如果节点n的值小于父节点p的值,那么节点n将作为节点p的左孩子,否则节点n作为节点p的右孩子。 +1. 如果节点c与节点n的值相等,那么说明当前正在插入一个重复节点,此时可以选择丢弃节点n或者抛出错误。 +1. 如果节点n小于节点c的值,那么节点n的父节点在节点c的左子树中,此时需要取得节点c的左孩子作为新的节点c继续进行比较。 +1. 如果节点n大于节点c的值,那么节点n的父节点在节点c的右子树中,此时需要取得节点c的右孩子作为新的节点c继续进行比较。 + +以下给出一个向BST中插入新节点的示例方法。 + +```java +public void insert(Node newNode) throws RepeatValueException { + LinkedList queue = new LinkedList<>(); + queue.push(this.root); + while (!queue.isEmpty()) { + BSTNode pointer = queue.pollFirst(); + if (pointer.compareTo(newNode) == 0) { + throw new RepeatValueException(); + } + if (pointer.compareTo(newNode) > 0) { + pointer.getLeftChild() + .ifPresentOrElse(queue::push, + () -> pointer.attachLeftChild(newNode)); + } + if (pointer.compareTo(newNode) < 0) { + pointer.getRightChild() + .ifPresentOrElse(queue::push, + () -> pointer.attachRightChild(newNode)); + } + } +} +``` + +## 删除节点 + +从BST中删除一个节点的难度比插入节点更大。如果被删除的是一个非叶子节点,那就意味着树的断裂,此时就必须选择一个合适的节点来填补。所以在定位被删除的节点之后,最重要的事情是选择一个合适的节点来代替被删除节点的位置。一般在删除BST的非叶子节点时,会采用以下策略来完成整个操作。 + +1. 如果被删除的节点没有右孩子,那么就直接选择它的左孩子代替原来的节点。 +1. 如果被删除的节点没有左孩子,那么就直接选择它的右孩子代替原来的节点。 +1. 如果被删除的节点既有左孩子又有右孩子,那么就需要使用被删除节点的右子树中最小值的节点来代替原来的节点。 + +在这个策略中,第三条策略是最复杂的,但是并不难理解。被删除节点的值必定要小于右子树中所有的值,且大于左子树中所有的值,所以,选择右子树中最小的值来代替被删除节点,是完全能够保证BST继续遵守约定规则的。 + +以下是操作BST删除节点的例程,这个例程在删除节点的时候,首先需要找到被删除节点的父节点,然后定位用来替代被删除节点的节点,在最复杂的情况下,需要做两次节点删除操作。 + +```java +public BSTNode minimum(BSTNode root) { + return root.getLeftChild() + .map(this::minimum) + .orElse(root); +} + +public void remove(Node pendingDelete) { + BSTNode parent = pendingDelete.getParent(); + if (!pendingDelete.getRightChild().isPresent()) { + pendingDelete.getLeftChild().ifPresent( + pendingDelete.isLeftChild() ? parent::attachLeftChild : parent::attachRightChild); + } else if (!pendingDelete.getLeftChild().isPresent()) { + pendingDelete.getRightChild().ifPresent( + pendingDelete.isLeftChild() ? parent::attachLeftChild : parent::attachRightChild); + } else { + BSTNode minimumNode = this.minimum(pendingDelete); + this.remove(minimumNode); + pendingDelete.getLeftChild().ifPresent(minimumNode::attachLeftChild); + pendingDelete.getRightChild().ifPresent(minimumNode::attachRightChild); + if (pendingDelete.isLeftChild()) { + parent.attachLeftChild(minimumNode); + } else { + parent.attachRightChild(minimumNode); + } + } +} +``` + +## BST的缺点 + +BST的缺点在于,如果没有能够很好的规划树的拓扑结构,那么BST将会失去它所有的搜索优势,节点检索的复杂度也会从$O(logN)$变为$O(n)$。这种情况在大量节点缺失单侧子树时,最常发生,所以在使用BST时,需要仔细规划树的拓扑结构,或者采用自平衡二叉查找树。 + +## 系列文章 + +1. {% post_link binary-tree-basic %} +1. 二叉搜索树 diff --git a/source/_posts/binary-search-tree/bst.svg b/source/_posts/binary-search-tree/bst.svg new file mode 100644 index 0000000..c548163 --- /dev/null +++ b/source/_posts/binary-search-tree/bst.svg @@ -0,0 +1,3 @@ + + +
5
5
3
3
6
6
4
4
8
8
2
2
7
7
1
1
二叉查找树
二叉查找树
Viewer does not support full SVG 1.1
\ No newline at end of file diff --git a/source/_posts/binary-tree-basic.md b/source/_posts/binary-tree-basic.md index 169030d..1163ef3 100644 --- a/source/_posts/binary-tree-basic.md +++ b/source/_posts/binary-tree-basic.md @@ -190,3 +190,8 @@ public void breadthFirstTraversal(Node root) { } } ``` + +## 系列文章 + +1. 二叉树基础 +1. {% post_link binary-search-tree %}