Académique Documents
Professionnel Documents
Culture Documents
2014 2015
Priority Queues & Search Trees
Philip Dutr
Dept. of Computer Science, K.U.Leuven
Overview Lecture
Priority Queues
Binary Trees
Binary Heap
2-3 Trees
Priority Queues
Collections: insert and delete items
Priority Queues
Priority Queues
Insert / Remove operations
Priority Queues
Insert / Remove operations
Binary Trees
Binary tree
Complete tree
Hyphaene Compressa
Binary heap
Heap-ordered binary tree
Keys in nodes
No smaller than childrens keys.
Array representation
Binary heap
10
Promotion in a heap
Scenario:
11
Insertion in a heap
12
Demotion in a heap
Scenario:
13
14
Overview
Heapsort:
15
16
Empty
Two disjoint binary trees (left and right)
Symmetric order:
Each node has a key, and every nodes key is:
17
BST in Java
18
BST Search
19
BST Search
Implementation:
Iterative (while-loop)
Recursion
20
BST Insert
21
BST Analysis
22
BST Analysis
23
BST Analysis
24
Cfr. quicksort
25
26
Cfr. supra
27
BST Delete
Lay approach
28
29
30
Case 1: 1 child
31
Case 2: 2 children
32
BST Summary
33
Too expensive
34
2-3 tree
Practical implementation: red-black tree
2-3 Trees
Perfect balance
35
36
37
38
39
40
Insertion Example
41
Insertion Example
42
43
Global properties
44
46
47
Invariants:
Implementation
48
1 field in node
indicates whether left
link is red
Copyright Ph.Dutr, Spring 2015
RB Trees Rotations
49
RB Trees Rotations
50
51
RB Trees Insertion
52
RB Trees Insertion
53
RB Trees Insertion
54
RB Trees Insertion
55
RB Trees Insertion
56
Insert Example
57
Insert Example
58
Insert Example
59
RB Trees Analysis
60
Real Story
Court case
Legal testimony:
If implemented properly, the height of a red-black
BST with N keys is at most 2log2N.
61