(Les06) Trees
Transcript of (Les06) Trees
-
7/25/2019 (Les06) Trees
1/61
Algoritmen & Datastructuren2014 2015
Priority Queues & Search Trees
Philip Dutr
Dept. of Computer Science, K.U.Leuven
-
7/25/2019 (Les06) Trees
2/61
Overview Lecture
Copyright Ph.Dutr, Spring 20152
Priority Queues Binary Trees
Binary Heap
Promotion, Insertion, Demotion, Delete Max
Heapsort Binary Search Tree
Search, Insert,Analysis, Delete
2-3 Trees
Search, Insert,Analysis Red-Black Trees
-
7/25/2019 (Les06) Trees
3/61
Priority Queues
Copyright Ph.Dutr, Spring 20153
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]
-
7/25/2019 (Les06) Trees
4/61
Priority Queues
Copyright Ph.Dutr, Spring 20154
-
7/25/2019 (Les06) Trees
5/61
Priority Queues
Copyright Ph.Dutr, Spring 20155
Insert / Remove operations
-
7/25/2019 (Les06) Trees
6/61
-
7/25/2019 (Les06) Trees
7/61
Binary Trees
Copyright Ph.Dutr, Spring 20157
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
-
7/25/2019 (Les06) Trees
8/61
Complete binary tree in nature
Copyright Ph.Dutr, Spring 20158
Hyphaene Compressa
-
7/25/2019 (Les06) Trees
9/61
Binary heap
Copyright Ph.Dutr, Spring 20159
Heap-ordered binary tree Keys in nodes
No smaller than childrens keys.
Array representation
Take nodes in level order No explicit links needed
-
7/25/2019 (Les06) Trees
10/61
Binary heap
Copyright Ph.Dutr, Spring 201510
Largest key is a[1] Use array indices to move
through tree:
Parent of node kis at k/2
Children of node kare at2kand 2k+1
-
7/25/2019 (Les06) Trees
11/61
Promotion in a heap
Copyright Ph.Dutr, Spring 201511
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
-
7/25/2019 (Les06) Trees
12/61
Insertion in a heap
Copyright Ph.Dutr, Spring 201512
Add node at end, then swim it up At most log2N compares
-
7/25/2019 (Les06) Trees
13/61
Demotion in a heap
Copyright Ph.Dutr, Spring 201513
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
-
7/25/2019 (Les06) Trees
14/61
Delete maximum in a heap
Copyright Ph.Dutr, Spring 201514
Exchange root with node at end, then sink it down At most 2log2N compares
-
7/25/2019 (Les06) Trees
15/61
Overview
Copyright Ph.Dutr, Spring 201515
Heapsort:
Keep all elements in priority queue Remove largest, rearrange queue
-
7/25/2019 (Les06) Trees
16/61
Binary Search Trees (BST)
Copyright Ph.Dutr, Spring 201516
A BST is a binary tree in symmetric order A binary tree is either:
Empty
Two disjoint binary trees (left and right)
-
7/25/2019 (Les06) Trees
17/61
Binary Search Trees (BST)
Copyright Ph.Dutr, Spring 201517
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
-
7/25/2019 (Les06) Trees
18/61
BST in Java
Copyright Ph.Dutr, Spring 201518
Often also size of the (sub)tree:
size(x) = size(x.left) + size(x.right) + 1
-
7/25/2019 (Les06) Trees
19/61
BST Search
Copyright Ph.Dutr, Spring 201519
Return value corresponding to given key,or nullif no such key.
-
7/25/2019 (Les06) Trees
20/61
BST Search
Copyright Ph.Dutr, Spring 201520
Implementation: Iterative (while-loop)
Recursion
# compares = depth of node
-
7/25/2019 (Les06) Trees
21/61
BST Insert
Copyright Ph.Dutr, Spring 201521
Search for key, thentwo cases:
Key in tree reset value
(if no duplicate keys allowed)
Key not in tree add new node.
-
7/25/2019 (Les06) Trees
22/61
BST Analysis
Copyright Ph.Dutr, Spring 201522
Many different BSTs are possible for same set of keys #compares = depth of node
-
7/25/2019 (Les06) Trees
23/61
BST Analysis
Copyright Ph.Dutr, Spring 201523
Experiment: random keys, trees stay flat
-
7/25/2019 (Les06) Trees
24/61
BST Analysis
Copyright Ph.Dutr, Spring 201524
Search hits in a BST tree from N random keysrequire ~1.39log2N compares, on average.
Cfr. quicksort
-
7/25/2019 (Les06) Trees
25/61
BST Ordered Operations
Copyright Ph.Dutr, Spring 201525
Find minimum key? Move left as far as possible
Find maximum key?
Move right as far as possible
-
7/25/2019 (Les06) Trees
26/61
BST Ordered Operations
Copyright Ph.Dutr, Spring 201526
Floor: find largest key root: floor couldbe in right subtree, or the root
Ceiling: find smallest key >= given key Cfr. supra
-
7/25/2019 (Les06) Trees
27/61
Copyright Ph.Dutr, Spring 201527
-
7/25/2019 (Les06) Trees
28/61
BST Delete
Copyright Ph.Dutr, Spring 201528
Lay approach
Make node empty, but leave node in tree
Tombstone overhead
-
7/25/2019 (Les06) Trees
29/61
BST Delete: minimum
Copyright Ph.Dutr, Spring 201529
To delete the minimum
key:
Go left until finding a node
with a null left link
Replace that node by its
right link
(Update subtree counts)
-
7/25/2019 (Les06) Trees
30/61
BST Delete: Hibbard
Copyright Ph.Dutr, Spring 201530
Search for node with key
Case 0: 0 children
-
7/25/2019 (Les06) Trees
31/61
BST Delete: Hibbard
Copyright Ph.Dutr, Spring 201531
Case 1: 1 child
-
7/25/2019 (Les06) Trees
32/61
BST Delete: Hibbard
Copyright Ph.Dutr, Spring 201532
Case 2: 2 children
Find successor xof node with key k (node t)
Delete the minimum in t's right subtree
Put xin t's spot
-
7/25/2019 (Les06) Trees
33/61
BST Summary
Copyright Ph.Dutr, Spring 201533
-
7/25/2019 (Les06) Trees
34/61
Problem with BSTs
Copyright Ph.Dutr, Spring 201534
BST has poor worst case performance
Ideal: perfectly balanced BST
Too expensive
Theoretical solution to keep tree balanced:
2-3 tree
Practical implementation: red-black tree
-
7/25/2019 (Les06) Trees
35/61
2-3 Trees
Copyright Ph.Dutr, Spring 201535
Allow 1 or 2 keys per node
2-node: one key, two children
3-node: two keys, three children
Perfect balance
Every path from root to null link has same length.
-
7/25/2019 (Les06) Trees
36/61
2-3 Trees Search
Copyright Ph.Dutr, Spring 201536
-
7/25/2019 (Les06) Trees
37/61
2-3 Trees Insert
Copyright Ph.Dutr, Spring 201537
Case 1: insert into a 2-node
-
7/25/2019 (Les06) Trees
38/61
2-3 Trees Insert
Copyright Ph.Dutr, Spring 201538
Case 2: insert into a 3-node, whose parent is a 2-node
-
7/25/2019 (Les06) Trees
39/61
2-3 Trees Insert
Copyright Ph.Dutr, Spring 201539
Case 3: insert into a 3-node, whose parent is a 3-node
-
7/25/2019 (Les06) Trees
40/61
2-3 Trees Insert
Copyright Ph.Dutr, Spring 201540
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
-
7/25/2019 (Les06) Trees
41/61
Insertion Example
Copyright Ph.Dutr, Spring 201541
-
7/25/2019 (Les06) Trees
42/61
Insertion Example
Copyright Ph.Dutr, Spring 201542
-
7/25/2019 (Les06) Trees
43/61
Local transformations in a 2-3 tree
Copyright Ph.Dutr, Spring 201543
-
7/25/2019 (Les06) Trees
44/61
Global properties
Copyright Ph.Dutr, Spring 201544
Each transformation maintains perfect balance and
symmetric order
-
7/25/2019 (Les06) Trees
45/61
2-3 Trees Analysis
Copyright Ph.Dutr, Spring 201545
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
-
7/25/2019 (Les06) Trees
46/61
2-3 Trees Analysis
Copyright Ph.Dutr, Spring 201546
-
7/25/2019 (Les06) Trees
47/61
Red-Black (RB) Trees
Copyright Ph.Dutr, Spring 201547
Represent 2-3 tree as BST
Use internal left-leaning links as glue for 3-nodes
-
7/25/2019 (Les06) Trees
48/61
Red-Black (RB) Trees
Copyright Ph.Dutr, Spring 201548
Invariants:
No node has two red
links connected to it
Every path from root
to null link has the
same number of blacklinks (black-balance)
Red links lean left
Implementation
1 field in node
indicates whether left
link is red
-
7/25/2019 (Les06) Trees
49/61
RB Trees Rotations
Copyright Ph.Dutr, Spring 201549
Left rotation: Orient a (temp) right red link to the left
-
7/25/2019 (Les06) Trees
50/61
RB Trees Rotations
Copyright Ph.Dutr, Spring 201550
Right rotation: orient a left red link (temp) to the right
-
7/25/2019 (Les06) Trees
51/61
RB Trees Color Flip
Copyright Ph.Dutr, Spring 201551
Recolor to split a (temp) 4-node
-
7/25/2019 (Les06) Trees
52/61
RB Trees Insertion
Copyright Ph.Dutr, Spring 201552
Basic strategy: Maintain 1-1 correspondence with 2-3
trees by applying elementary red-black tree operations
-
7/25/2019 (Les06) Trees
53/61
RB Trees Insertion
Copyright Ph.Dutr, Spring 201553
Simple case: Insert in tree with exactly 1 node
-
7/25/2019 (Les06) Trees
54/61
RB Trees Insertion
Copyright Ph.Dutr, Spring 201554
Case 1: insert into 2-node at bottom
Do standard BST insert; color new link red
If new red link is a right link, rotate left
-
7/25/2019 (Les06) Trees
55/61
RB Trees Insertion
Copyright Ph.Dutr, Spring 201555
Simple case: insert in tree with exactly 2 nodes
-
7/25/2019 (Les06) Trees
56/61
RB Trees Insertion
Copyright Ph.Dutr, Spring 201556
Case 2: insert in 3-node 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).
-
7/25/2019 (Les06) Trees
57/61
Insert Example
Copyright Ph.Dutr, Spring 201557
-
7/25/2019 (Les06) Trees
58/61
Insert Example
Copyright Ph.Dutr, Spring 201558
-
7/25/2019 (Les06) Trees
59/61
Insert Example
Copyright Ph.Dutr, Spring 201559
-
7/25/2019 (Les06) Trees
60/61
RB Trees Analysis
Copyright Ph.Dutr, Spring 201560
Height with N nodes is
-
7/25/2019 (Les06) Trees
61/61
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.