Data Structure
Pooint: organize data so that it can be accessed quickly and usefully.
Examples: lists,stacks,queues,heaps,search trees,hashtables,
bloom filters, union-find, etc
Rule of thumb: choose the “minimal” data structure that supports all the operations that you need.
Heap
Heap: Supported Operations
⋅
⋅
A container for objects that have keys
Insert: add a new object to a heap.
Running time: O(log(n))
Extract-Min: remove an object in heap with a minimum key value.
Running time: O(log(n))
Also:Heapify(堆化)(n batched Inserts in O(n) time), Delete (O(log(n)) time) .
Application: Sorting
Canonical use of heap: fast way to do repeated minimum computations.
Example: Selection Sort
θ(n)
θ
(
n
)
,
θ(n2)
θ
(
n
2
)
running time on array of length n
Heap Sort:
1.) insert all n array elements into a heap.
2.) Extract-Min to pluck out elements in sorted in sorted order.
Running time = 2n heap operations = O(nlog(n)) time.
Application: Speeding Up Dijkstra
Dijkstra’s Shortest-Path Algorithm
n: number of vertices,m: number of edges.
⋅
⋅
Naive implementation => running time =
θ(nm)
θ
(
n
m
)
⋅
⋅
with heaps => running time = O(mlog(n))
The Heap Property
Conceptually: think of a heap as a tree.
-rooted, binary, as complete as possible
Heap Property: at every node x.
Key[x] <= all keys of x’s children
Consequence:
object at root must have minimum key value
Array Implementation
Note: parent[i] = i / 2 if i even else [i/2] (向下取整)if i odd
children of i are 2i, 2i + 1.
Insert and Bubble-Up(插入操作)
Implementation of Insert(given key k)
Step1: stick k at end of last level.
Step2: Bubble-Up k until heap property is restored.
check:
1) bubbling up process must stop, with heap property restored.
2) run time = O(log(n))
Extract-Min and Bubble-Down
Implementation of Extract-Min
1.Delete toot.
2.Move last leaf to be new root.
3.Iteratively Bubble-Down until heap property has been restored.
always swap with smaller child
check:
1.) only Bubble-Down once per level, halt with a heap
2.) run time = O(log(n))
Sorted Arrays: Supported Operations (有序数组)
operations | Running time |
---|---|
Search | θ(log(n)) θ ( l o g ( n ) ) |
Select(give order statistic l) | O(1) |
Min/Max | O(1) |
Predecessor/Successor(given pointer to a key) | O(1) |
Rank(#of keys less than or equal to a given value) | O(log(n)) |
Output in sorted order | O(n) |
Balanced Search Trees & Supported Operations (平衡二叉树)
operations | Running time |
---|---|
Search | θ(log(n)) θ ( l o g ( n ) ) |
Select(give order statistic l) | O(log(n)) |
Min/Max | O(log(n)) |
Predecessor/Successor(given pointer to a key) | O(log(n)) |
Rank(#of keys less than or equal to a given value) | O(log(n)) |
Output in sorted order | O(n) |
Insert | O(log(n)) |
Delete | O(log(n)) |
Binary Search Tree Structure
二叉搜索树
1) exactly one node per key
2) mostly basic version:
each node has
⋅
⋅
left child pointer
⋅
⋅
right child pointer
⋅
⋅
parent pointer
对于搜索树的每个节点,左子树的所有节点均小于该节点,右子树的所有节点均大于该节点。
The Height of a BST
Note: height[(also known as depth) longest root-leaf path]
could be anywhere from
log2n
l
o
g
2
n
(best case,perferctly balanced) to
n
n
(Worst case).
Searching and Inserting
To Search for key k in tree T
start at the root
⋅
⋅
traverse left/right child pointers as needed
⋅
⋅
return node with key k or NULL, as appreciable.
To Insert a new key k into a tree T
⋅
⋅
search for k(unsuccessfully)
⋅
⋅
rewrite final NULL ptr to point to a new node with key k.
In-Order Traversal
To print out keys in increasing order
⋅
⋅
Let r = root of search tree, with subtrees TL and TR
⋅
⋅
recurse on TL
[by recursion(induction) prints out keys of TL in increasing order]
⋅
⋅
Print out r’s key
⋅
⋅
Recurse on TR
[prints out keys of TR in increasing order]
Deletion
⋅
⋅
To delete a key k from a search tree.
⋅
⋅
Search for k
⋅
⋅
Easy case(k’s node has no children)
⋅
⋅
Just delete k’s node from tree,done
⋅
⋅
Medium case(k’s node has one child)
(unique child assumes(夺取) position previously held by k’s node)
Deletion(con’ d)
(Difficult case(k’s node has 2 children))
⋅
⋅
Compute k’s predecessor l.
[traverse k’s (non-NULL) left child ptr, then right child ptrs until no longer possible]
⋅
⋅
in it’s new position, k has no right child, k has no right child!=>easy to delete or splice(拼接) out k’s new node.
⋅
⋅
predecessor: next smallest child in the tree.
Exercise: at end, have a valid search tree.
Select and Rank
Idea: store a little bit of extra indo at each tree node about the tree itself (not about the data)
Example Augmentation: size(x) = # of tree nodes in subtree rooted at x.
Note: if x has children y and z,
then size(y)(population in left subtree) + size(z)(population in right subtree) + 1(x itself)
Also: easy to keep sizes up-to-date during an Insertion or Deletion.
Select and Rank
How to select
ith
i
t
h
order statistic from augmentated search tree.(with subtree sizes)
⋅
⋅
start at root x ,with children y and z.
⋅
⋅
let a = size(y) [a=0 if x has no left child]
⋅
⋅
if a =
i
i
-1,return x’s key.
if a
≥i
≥
i
,recursively compute
ith
i
t
h
order statistic of search tree rooted at y.
⋅
⋅
if a <
i
i
-1,recursively compute order stastic of search tree rooted at z.
Running Time =
θ(height)
θ
(
h
e
i
g
h
t
)
Red-Black Invariants (常量)
(1) Each node red or black
(2) Root is black
(3) No 2 reds in a row[red node => only black children]
(4)Every root-NULL (like in an unsuccessful search ) path has same number of black nodes
Height Guarantee
Claim: every red-black tree with n nodes has height
≤2log2(n+1)
≤
2
l
o
g
2
(
n
+
1
)
.
Proof:Observation:
if every root-NULL path has
≥
≥
k nodes,then tree includes (at the top) a perfectly balanced search tree of depth k-1.
计算中位数字
/* 1.i个元素中较大值的一半放在最大堆中,较小的部分放在最小堆中
* 2.在保持两个堆的大小较为平衡
* 3.中位数分布在最大堆中的最后一个值和最小堆中的最后一个元素
* 4.插入元素j时,j>Max_heap_min,放入最大堆,j