Data Structure: Trees (树状数据结构)

@[TOC](Data Structure: Trees (树状数据结构))

数据结构存在的目的

数据结构便于数据管理data management和检索retrieval

复杂度概要

在这里插入图片描述

Binary Search Tree 二叉搜索树

BST property:

  • 每个node最多有俩子节点
  • 左子节点<父节点
  • 右子节点>父节点

BST insertion:

  • 保证BST property在insertion之后仍然保持
  • Recursion大概步骤:
    • if x>node: insert(right subtree)
    • else: insert(left subtree)
    • 直到subtree = null, 把x放下就行了

BST deletion:

考虑以下三种情况:

  1. If x has no children: then remove x
  2. If x has one child: then make p[x.child] point to p[x]
  3. If x has two children (subtrees): then replace x with its successor, delete successor in subtree

Balance Tree

  • 属于BST
  • 对于n个节点保证高度为 O(lg n)
  • Height = maximum # edges from root to node
  • E.g. AVL Trees, Red-Black Trees

Red-Black Tree 红黑树

  • 红黑树接近于平衡树但不是完全平衡
  • 树结构要求在每个节点中有一个额外的一位颜色bit:红色或黑色
  • 色彩是用来保持平衡的
  • 每一个节点包含: key, color, left, right, parent, (data).

Red-black properties:

  1. 每个节点是red或者black.
  2. Root and leaves都是black. (每一个node都只有2个children)
  3. 如果node是red的, 那么children必须是black. (*不能有两个连续的red)
  4. Every path from node to后代leave都包含相同数量的black节点

RBT Rotation

在这里插入图片描述

RBT Deletion

Read Chapter 14 of Introduction to Algorithms (by Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest).

Black-height

  • black-height: bh(x)定义为从node x到leave(包括null leave)的任意path上的black node数。
  • 最小black-height: a height-h node has black-height ≥ h/2
  • 定理: A red-black tree with n internal nodes has height h≤2 log⁡(n+1). (Prove by induction)

时间复杂度

  • A red-black tree has O(log⁡n) height.
  • Corollary: These operations take O(log⁡n) time:
    • Minimum(), Maximum(), Successor(), Predecessor(), Search()
    • Insert(), Delete(): but will need special care since they modify tree

可视化

https://www.cs.usfca.edu/~galles/visualization/RedBlack.html

AVL Tree

  • AVL是人名: Adelson-Velskii & Landis
  • AVL是balance tree. (在所有节点中,右子树的高度与左子树的高度最多相差1)
  • AVL被称为第一个自平衡二叉搜索树

AVL height

N(h)=min(#nodes of height h AVL) ⇒ 一个子树的高度至少为h-1,另一个子树的高度至少为h-2。
在这里插入图片描述
AVL树的高度约为1.44 log_2⁡n,而红黑树的高度可能高达2 log_2⁡n。因此在AVL中,平均查找速度略快。

Balance factor

Bf = height(Left Subtree) - height(Right Subtree) ∈{-1,0,1}
如果balance factor不在{-1,0,1}范围内就需要进行single rotation或者double rotation操作来使其平衡~

AVL Rotation

Single Rotation

在这里插入图片描述

Double Rotation

Right-Left Rotation:
在这里插入图片描述
Feft-Right Rotation:
在这里插入图片描述

AVL insertion

和BST insertion类似, 但在插入节点后如果tree not balance, 需要通过rotation使其重新平衡. (四种情况: right/left rotation, right-left/left-right rotation)

AVL deletion

和BST deletion类似, 但在删除节点后如果tree not balance, 需要通过(多次)rotation使其重新平衡.

AVL 可视化

https://www.cs.usfca.edu/~galles/visualization/AVLtree.html

Reference

  • Chapter 14 of Introduction to Algorithms (by Thomas H. Cormen, Charles E. Leiserson, and Ronald L. Rivest)
  • Chapter 10 of Lee K.D., Hubbard S. (2015) Balanced Binary Search Trees. In: Data Structures and Algorithms with Python. Undergraduate Topics in Computer Science. Springer, Cham
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

失眠软糖12

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值