外教课004_4_advanced_data_structures

Advanced data structures

Binary search trees(repetition)

Tree

  • 第1单元有

Rooted tree

  • 第1单元有

Binary tree

a (rooted) tree where no node have more than two children

Binary search trees

  • all nodes has a comparable key

  • the key is larger than left subtree’s and smaller than right subtree’s

  • search

    • if empty, not contain any node with value X

    • if not

      • if match, done

        1. if bigger than current value, to the right child

        2. if smaller than current value, to the left child

      • if reach the leaf and not match, not contain

  • smallest/largest

    • traverse the tree from the root by following the left(right) child until without child
  • insert

  • delete

    1. a leaf, just remove
    2. with one child, link parent and child
    3. with two child, replace to smallest in the right subtree
  • lazy delete

    keep the node in the tree but mark it not any value

Tree balance

  • the heigt difference between the right and left subtree of all its nodes is not greater than one
  • fast to insert and delete if balanced
  • height log ⁡ n \log n logn, n is the number of nodes in the tree, means insertion and deletion can be done in O( log ⁡ n \log n logn)

self-balancing trees

  1. AVL trees
  2. Splay trees
  3. Red black trees

AVL trees

  • a type of self balancing binary search tree
  • height O( log ⁡ n \log n logn)
  • two operations
    • single rotation
    • double rotation

Single rotation

在这里插入图片描述

在这里插入图片描述

Double rotation

在这里插入图片描述
在这里插入图片描述

Inserting

练习

Splay trees

  • another type of self-adjusting binary search tree(不要求平衡)

  • basic idea: if a node is accessed(inserted or looked up), there is a high chance that it will be accessed again soon

  • also modified on lookup(被查看)

  • the accessed node is moved “splayed” to the top of the tree, and most other nodes on the access path to the accessed node is also moved closer to the top of the tree

  • amortized cost O( log ⁡ n \log n logn)

    • it means any sequence of n operations on a splay tree is nlogn
    • worst case O(n) when linear a line
  • zig 对应 single rotation

  • zig-zig 对应 两个相同方向的single rotation,先上面后下面

  • zig-zag 对应 double rotation

  • ppt图解很清晰;中方老师也有讲

Access

  1. find the node to access(or not in tree)
  2. splay the accessed node to the root of the tree

Insertion

  1. insert as a leaf
  2. splay to the root of the tree

ppt图解

Deletion

  1. access the node(move to root)
  2. delete the node
  3. splay the rightmost(largest) element of T L T_L TL to the top(或者相反)

ppt图解

top down splay trees

一种比普通的伸展树认为更高效的特例

Red-black trees

  • another type of self-balancing binary search tree

4 coloring prooperties

  1. Each node is colored either red or black(废话)
  2. The root is black(源是黑的)
  3. If a node is red, its children must be black(红下一定黑)
  4. Each path from a node to a leaf must contain the same number of black nodes(每一条路黑相同)

Insert

先以红色注入;这里就不写在这了

Delete

自学

Complexity of red-black trees

height at most 2log(N+1) (O(logN))

at minimum log(N+1) (if all nodes are black)

Suffix arrays and suffix trees

  • used for full text indexing and data compression
  • example in text processing
    • find out if there is a substring of a text T matching a pattern P
    • find out how many times P appear in T
    • find out where are all occurences of P in T
  • is built once and can be used over and over again

Definition - suffix

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值