Vous êtes sur la page 1sur 61

Algoritmen & Datastructuren

2014 2015
Priority Queues & Search Trees
Philip Dutr
Dept. of Computer Science, K.U.Leuven

Overview Lecture
Priority Queues
Binary Trees

Binary Heap

Binary Search Tree

Search, Insert, Analysis, Delete

2-3 Trees

Promotion, Insertion, Demotion, Delete Max


Heapsort

Search, Insert, Analysis


Red-Black Trees

Copyright Ph.Dutr, Spring 2015

Priority Queues
Collections: insert and delete items

Most recent Stack


Least recent Queue
Largest (or smallest) Priority Queues

Applications of priority queues

Operating systems [load balancing, interrupt handling]


Statistics [maintain largest M values in a sequence]
Graph searching [Dijkstra's algorithm, Prim's algorithm]
Streams of Data [Monetary transactions]
Copyright Ph.Dutr, Spring 2015

Priority Queues

Copyright Ph.Dutr, Spring 2015

Priority Queues
Insert / Remove operations

Copyright Ph.Dutr, Spring 2015

Priority Queues
Insert / Remove operations

Copyright Ph.Dutr, Spring 2015

Binary Trees
Binary tree

Empty or node with links to left and right binary trees

Complete tree

Perfectly balanced, except for bottom level

Property: Height of tree with N nodes is 1+log2N


Copyright Ph.Dutr, Spring 2015

Complete binary tree in nature

Hyphaene Compressa

Copyright Ph.Dutr, Spring 2015

Binary heap
Heap-ordered binary tree

Keys in nodes
No smaller than childrens keys.

Array representation

Take nodes in level order


No explicit links needed

Copyright Ph.Dutr, Spring 2015

Binary heap

Largest key is a[1]


Use array indices to move
through tree:

10

Parent of node k is at k/2


Children of node k are at
2k and 2k+1

Copyright Ph.Dutr, Spring 2015

Promotion in a heap

Scenario:

Node's key is replaced by a larger key than its parent's key

Exchange key in node with key in parent


Repeat until heap order restored

11

Copyright Ph.Dutr, Spring 2015

Insertion in a heap

Add node at end, then swim it up


At most log2N compares

12

Copyright Ph.Dutr, Spring 2015

Demotion in a heap

Scenario:

Node's key becomes smaller than one (or both) of its


children's keys

Exchange key in node with key in larger child


Repeat until heap order restored

13

Copyright Ph.Dutr, Spring 2015

Delete maximum in a heap

Exchange root with node at end, then sink it down


At most 2log2N compares

14

Copyright Ph.Dutr, Spring 2015

Overview

Heapsort:

15

Keep all elements in priority queue


Remove largest, rearrange queue

Copyright Ph.Dutr, Spring 2015

Binary Search Trees (BST)

A BST is a binary tree in symmetric order


A binary tree is either:

16

Empty
Two disjoint binary trees (left and right)

Copyright Ph.Dutr, Spring 2015

Binary Search Trees (BST)

Symmetric order:
Each node has a key, and every nodes key is:

Larger than all keys in its left subtree.

Smaller than all keys in its right subtree

17

Copyright Ph.Dutr, Spring 2015

BST in Java

Often also size of the (sub)tree:


size(x) = size(x.left) + size(x.right) + 1

18

Copyright Ph.Dutr, Spring 2015

BST Search

Return value corresponding to given key,


or null if no such key.

19

Copyright Ph.Dutr, Spring 2015

BST Search

Implementation:

Iterative (while-loop)
Recursion

# compares = depth of node

20

Copyright Ph.Dutr, Spring 2015

BST Insert

Search for key, then


two cases:

21

Key in tree reset value


(if no duplicate keys allowed)
Key not in tree add new node.

Copyright Ph.Dutr, Spring 2015

BST Analysis

Many different BSTs are possible for same set of keys


#compares = depth of node

22

Copyright Ph.Dutr, Spring 2015

BST Analysis

Experiment: random keys, trees stay flat

23

Copyright Ph.Dutr, Spring 2015

BST Analysis

Search hits in a BST tree from N random keys


require ~1.39log2N compares, on average.

24

Cfr. quicksort

Copyright Ph.Dutr, Spring 2015

BST Ordered Operations

Find minimum key?

Move left as far as possible

Find maximum key?

25

Move right as far as possible

Copyright Ph.Dutr, Spring 2015

BST Ordered Operations

Floor: find largest key <= given key

If key == root: floor is the root


If key < root: floor must be in left tree
If key > root: floor could be in right subtree, or the root

Ceiling: find smallest key >= given key

26

Cfr. supra

Copyright Ph.Dutr, Spring 2015

27

Copyright Ph.Dutr, Spring 2015

BST Delete

Lay approach

Make node empty, but leave node in tree


Tombstone overhead

28

Copyright Ph.Dutr, Spring 2015

BST Delete: minimum

To delete the minimum


key:

29

Go left until finding a node


with a null left link
Replace that node by its
right link
(Update subtree counts)

Copyright Ph.Dutr, Spring 2015

BST Delete: Hibbard

Search for node with key


Case 0: 0 children

30

Copyright Ph.Dutr, Spring 2015

BST Delete: Hibbard

Case 1: 1 child

31

Copyright Ph.Dutr, Spring 2015

BST Delete: Hibbard

Case 2: 2 children

32

Find successor x of node with key k (node t)


Delete the minimum in t's right subtree
Put x in t's spot

Copyright Ph.Dutr, Spring 2015

BST Summary

33

Copyright Ph.Dutr, Spring 2015

Problem with BSTs

BST has poor worst case performance

Ideal: perfectly balanced BST

Too expensive

Theoretical solution to keep tree balanced:

34

2-3 tree
Practical implementation: red-black tree

Copyright Ph.Dutr, Spring 2015

2-3 Trees

Allow 1 or 2 keys per node

2-node: one key, two children


3-node: two keys, three children

Perfect balance

35

Every path from root to null link has same length.

Copyright Ph.Dutr, Spring 2015

2-3 Trees Search

36

Copyright Ph.Dutr, Spring 2015

2-3 Trees Insert

Case 1: insert into a 2-node

37

Copyright Ph.Dutr, Spring 2015

2-3 Trees Insert

Case 2: insert into a 3-node, whose parent is a 2-node

38

Copyright Ph.Dutr, Spring 2015

2-3 Trees Insert

Case 3: insert into a 3-node, whose parent is a 3-node

39

Copyright Ph.Dutr, Spring 2015

2-3 Trees Insert

If you reach the root and it's a 4-node:

split the root into three 2-nodes

Splitting the root increases height by 1

40

Copyright Ph.Dutr, Spring 2015

Insertion Example

41

Copyright Ph.Dutr, Spring 2015

Insertion Example

42

Copyright Ph.Dutr, Spring 2015

Local transformations in a 2-3 tree

43

Copyright Ph.Dutr, Spring 2015

Global properties

Each transformation maintains perfect balance and


symmetric order

44

Copyright Ph.Dutr, Spring 2015

2-3 Trees Analysis

Every path from root to null-link has same length

Worst case: log2N [all 2-nodes]


Best case: log3N 0.631 log2N [all 3-nodes]

Between 12 and 20 for a million nodes


Between 18 and 30 for a billion nodes
Guaranteed logarithmic performance for search and insert
45

Copyright Ph.Dutr, Spring 2015

2-3 Trees Analysis

46

Copyright Ph.Dutr, Spring 2015

Red-Black (RB) Trees

Represent 2-3 tree as BST


Use internal left-leaning links as glue for 3-nodes

47

Copyright Ph.Dutr, Spring 2015

Red-Black (RB) Trees

Invariants:

No node has two red


links connected to it
Every path from root
to null link has the
same number of black
links (black-balance)
Red links lean left

Implementation

48

1 field in node
indicates whether left
link is red
Copyright Ph.Dutr, Spring 2015

RB Trees Rotations

Left rotation: Orient a (temp) right red link to the left

49

Copyright Ph.Dutr, Spring 2015

RB Trees Rotations

Right rotation: orient a left red link (temp) to the right

50

Copyright Ph.Dutr, Spring 2015

RB Trees Color Flip

Recolor to split a (temp) 4-node

51

Copyright Ph.Dutr, Spring 2015

RB Trees Insertion

Basic strategy: Maintain 1-1 correspondence with 2-3


trees by applying elementary red-black tree operations

52

Copyright Ph.Dutr, Spring 2015

RB Trees Insertion

Simple case: Insert in tree with exactly 1 node

53

Copyright Ph.Dutr, Spring 2015

RB Trees Insertion

Case 1: insert into 2-node at bottom

54

Do standard BST insert; color new link red


If new red link is a right link, rotate left

Copyright Ph.Dutr, Spring 2015

RB Trees Insertion

Simple case: insert in tree with exactly 2 nodes

55

Copyright Ph.Dutr, Spring 2015

RB Trees Insertion

Case 2: insert in 3-node

56

Do standard BST insert; color new link red.


Rotate to balance the 4-node (if needed).
Flip colors to pass red link up one level.
Rotate to make lean left (if needed).

Copyright Ph.Dutr, Spring 2015

Insert Example

57

Copyright Ph.Dutr, Spring 2015

Insert Example

58

Copyright Ph.Dutr, Spring 2015

Insert Example

59

Copyright Ph.Dutr, Spring 2015

RB Trees Analysis

Height with N nodes is <= 2log2N

60

Every path has same number of black links


Never 2 red links in a row

Copyright Ph.Dutr, Spring 2015

Real Story

On-line business wanted customer database with


real-time updates
Database provider decided to use RB tree

Max height 80 (2^40 records), error triggered otherwise


Rebalance only after insertions, not deletions

extended service outage

Court case
Legal testimony:
If implemented properly, the height of a red-black
BST with N keys is at most 2log2N.
61

Copyright Ph.Dutr, Spring 2015

Vous aimerez peut-être aussi