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
-
if bigger than current value, to the right child
-
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
- a leaf, just remove
- with one child, link parent and child
- 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
- AVL trees
- Splay trees
- 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
- find the node to access(or not in tree)
- splay the accessed node to the root of the tree
Insertion
- insert as a leaf
- splay to the root of the tree
ppt图解
Deletion
- access the node(move to root)
- delete the node
- 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
- Each node is colored either red or black(废话)
- The root is black(源是黑的)
- If a node is red, its children must be black(红下一定黑)
- 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