Skip to content

Commit

Permalink
Fix binary search tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
krahets committed Dec 27, 2022
1 parent 36507b8 commit 8e9bfab
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions docs/chapter_tree/binary_search_tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -761,14 +761,14 @@ comments: true

- **查找元素:** 由于数组是无序的,因此需要遍历数组来确定,使用 $O(n)$ 时间;
- **插入元素:** 只需将元素添加至数组尾部即可,使用 $O(1)$ 时间;
- **删除元素:** 先查找元素,使用 $O(\log n)$ 时间,再在数组中删除该元素,使用 $O(n)$ 时间;
- **删除元素:** 先查找元素,使用 $O(n)$ 时间,再在数组中删除该元素,使用 $O(n)$ 时间;
- **获取最小 / 最大元素:** 需要遍历数组来确定,使用 $O(n)$ 时间;

为了得到先验信息,我们也可以预先将数组元素进行排序,得到一个「排序数组」,此时操作效率为:

- **查找元素:** 由于数组已排序,可以使用二分查找,使用 $O(\log n)$ 时间;
- **插入元素:** 为了保持数组是有序的,需插入到数组某位置,平均使用 $O(n)$ 时间;
- **删除元素:** 与无序数组中的情况相同,使用 $O(n)$ 时间;
- **查找元素:** 由于数组已排序,可以使用二分查找,平均使用 $O(\log n)$ 时间;
- **插入元素:** 先查找插入位置,使用 $O(\log n)$ 时间,再插入到指定位置,使用 $O(n)$ 时间;
- **删除元素:** 先查找元素,使用 $O(\log n)$ 时间,再在数组中删除该元素,使用 $O(n)$ 时间;
- **获取最小 / 最大元素:** 数组头部和尾部元素即是最小和最大元素,使用 $O(1)$ 时间;

观察发现,无序数组和有序数组中的各项操作的时间复杂度是“偏科”的,即有的快有的慢;**而二叉搜索树的各项操作的时间复杂度都是对数阶,在数据量 $n$ 很大时有巨大优势**
Expand Down

0 comments on commit 8e9bfab

Please sign in to comment.