AVL Tree

CSC201
Analysis and Design of Algorithms
Analysis of Algorithm using Tree
Data Structure
Asst.Prof. Dr.Surasak Mungsing
E-mail: Surasak.mu@spu.ac.th
Jul-17
1
Trees
7/13/2017
2
Introduction to Trees




7/13/2017
General Trees
Binary Trees
Binary Search Trees
AVL Trees
3
Tree
7/13/2017
4
Definition
 A tree t is a finite nonempty set of elements.
 One of these elements is called the root.
 The remaining elements, if any, are partitioned
into trees, which are called the subtrees of t.
7/13/2017
5
Sub-trees
7/13/2017
6
Tree
7/13/2017
7
height = depth = number of levels
Object
Level 1
Level 2
Number
OutputStream
Throwable
Level 3
Integer
Double
FileOutputStream
Exception
Level 4
RuntimeException
Jul-17
8
Node Degree = Number Of Children
Object
2
Number
0
Integer
1
1
OutputStream
Throwable
0
Double
0
1
FileOutputStream
Exception
0
RuntimeException
Jul-17
9
Binary Tree
7/13/2017
10
Binary Tree
 Finite (possibly empty) collection of elements.
 A nonempty binary tree has a root element.
 The remaining elements (if any) are partitioned
into two binary trees.
 These are called the left and right subtrees of the
binary tree.
7/13/2017
11
Binary Tree
7/13/2017
12
A Tree vs A Binary Tree
 No node in a binary tree may have a degree
more than 2, whereas there is no limit on the
degree of a node in a tree.
 A binary tree may be empty; a tree cannot be
empty.
7/13/2017
13
A Tree vs A Binary Tree
 The subtrees of a binary tree are ordered;
those of a tree are not ordered.
a
a
b
b
• Are different when viewed as binary
trees.
• Are the same when viewed as trees.
7/13/2017
14
Forms of Binary Trees
7/13/2017
15
Complete Binary Trees
7/13/2017
16
Tree Traversal
7/13/2017
17
Processing and Walking Order
7/13/2017
18
Depth First Processing
7/13/2017
19
Preorder Traversal
7/13/2017
20
Breath First Processing
7/13/2017
21
Height and number of nodes
 Maximum height of a binary tree
Hmax = N
 Minimum height of a binary tree
Hmin = logN + 1
 Maximum and Minimum number of nodes
Nmin = H and Nmax = 2H - 1
7/13/2017
22
7/13/2017
23
การประยุกต์ ใช้ Tree
Expression Tree
7/13/2017
24
Arithmetic Expressions
(a + b) * (c + d) + e – f/g*h + 3.25
Expressions comprise three kinds of entities.
 Operators (+, -, /, *).
 Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d),
etc.).
 Delimiters ((, )).
7/13/2017
25
Infix Form
 Normal way to write an expression.
 Binary operators come in between their left and
right operands.




7/13/2017
a*b
a+b*c
a*b/c
(a + b) * (c + d) + e – f/g*h + 3.25
26
Operator Priorities
 How do you figure out the operands of an
operator?
 a+b*c
 a*b+c/d
 This is done by assigning operator priorities.
 priority(*) = priority(/) > priority(+) = priority(-)
 When an operand lies between two operators,
the operand associates with the operator that has
higher priority.
7/13/2017
27
Tie Breaker
 When an operand lies between two operators
that have the same priority, the operand
associates with the operator on the left.
a+b-c
a*b/c/d
7/13/2017
28
Delimiters
 Subexpression within delimiters is treated as
a single operand, independent from the
remainder of the expression.
 (a + b) * (c – d) / (e – f)
7/13/2017
29
Infix Expression Is Hard To Parse
 Need operator priorities, tie breaker, and
delimiters.
 This makes computer evaluation more
difficult than is necessary.
 Postfix and prefix expression forms do not
rely on operator priorities, a tie breaker, or
delimiters.
 So it is easier for a computer to evaluate
expressions that are in these forms.
7/13/2017
30
Postfix Form
 The postfix form of a variable or constant is
the same as its infix form.
 a, b, 3.25
 The relative order of operands is the same in
infix and postfix forms.
 Operators come immediately after the postfix
form of their operands.
 Infix = a + b
 Postfix = ab+
7/13/2017
31
Postfix Examples
 Infix = a + b * c
 Postfix = ab c * +
• Infix = a * b + c
 Postfix = a b * c +
• Infix = (a + b) * (c – d) / (e + f)
 Postfix = a b + c d - * e f + /
7/13/2017
32
Expression Tree
7/13/2017
33
Expression Tree
7/13/2017
34
Binary Tree Form
 a+b
+
a
b
-
• -a
a
7/13/2017
35
Binary Tree Form
 (a + b) * (c – d) / (e + f)
//
*
+
e
+
a
7/13/2017
-
b
c
d
36
f
Expression Tree
Infix Expression =?
7/13/2017
37
Constructing an Expression Tree
ab+cd*(a)
(b)
+
a b
a
(c)
(d)
+
a
*
b
c
-
d
+
a
7/13/2017
c
b
38
*
b
c
d
d
การประยุกต์ ใช้ Tree
Binary Search Trees
7/13/2017
39
Figure 8-1
Binary Search Tree
7/13/2017
40
Binary Search Trees
7/13/2017
41
Are these Binary Search Trees?
7/13/2017
42
Construct a Binary Search Tree
เวลาทีใ่ ช้ ในการค้ นหาข้ อมูล
Worst case?
Average case?
7/13/2017
43
7/13/2017
44
Balance Binary Search Tree
AVL Trees
7/13/2017
45
AVL Trees
 Balanced binary tree structure, named after
Adelson, Velski, and Landis
 An AVL tree is a height balanced binary
search tree.

|HL – HR| <= 1
where HL is the height of the left subtree and
HR is the height of the left subtree
7/13/2017
46
Binary Search Trees
(b) AVL Tree
(a) An unbalanced BST
7/13/2017
47
Out of Balance
Four cases of out of balance:
 left of left (LL)
 right of right (RR)
 Left of right (LR)
 Right of left (RL)
7/13/2017
- requires single rotation
- requires single rotation
- requires double rotation
- requires double rotation
48
Out of Balance (left of left)
7/13/2017
49
Out of Balance (left of left)
7/13/2017
50
Out of Balance (right of right)
7/13/2017
51
Out of Balance (right of right)
7/13/2017
52
Simple double rotation right
7/13/2017
53
Complex double rotation right
7/13/2017
54
Insert a node to AVL tree
7/13/2017
55
Balancing BST
7/13/2017
56
Deleting a node from AVL tree
7/13/2017
57
Balance Binary Search Tree
เวลาทีใ่ ช้ ในการค้ นหาข้ อมูลใน AVL Tree
Worst case?
Average case?
7/13/2017
58
7/13/2017
59
Priority Queue
• Collection of elements.
• Each element has a priority or key.
• Supports following operations:





Jul-17
isEmpty
size
add/put an element into the priority queue
get element with min/max priority
remove element with min/max priority
60
Min Tree Example
2
4
4
9
3
8
7
9
9
Root is the minimum element
Jul-17
61
Max Tree Example
9
4
4
9
8
2
7
3
1
Root is the maximum element
Jul-17
62
Min Heap Definition
• complete binary tree
• min tree
2
4
6
8
3
7
9
3
6
Complete binary tree with 9 nodes that is also a min tree.
Jul-17
63
Max Heap With 9 Nodes
9
8
6
5
7
7
2
6
1
Complete binary tree with 9 nodes that is also a max tree.
Jul-17
64
Heap Height
 What is the height of an n node heap ?
Since a heap is a complete binary tree, the
height of an n node heap is log2 (n+1).
Jul-17
65
A Heap Is Efficiently Represented As An Array
9
8
7
6
5
7
2
1
0
9
8
7
6
7
2
6
5
1
1
2
3
4
5
6
7
8
9 10
6
Moving Up And Down A Heap
1
9
2
3
8
7
4
5
6
7
5
1
8
9
7
6
2
6
Putting An Element Into A Max Heap
9
8
7
6
5
7
1
2
7
Complete binary tree with 10 nodes.
6
Putting An Element Into A Max Heap
9
8
7
6
5
7
1
5
7
New element is 5.
2
6
Putting An Element Into A Max Heap
9
8
7
6
5
7
1
7
New element is 20.
2
6
Putting An Element Into A Max Heap
9
8
7
6
5
2
1
7
New element is 20.
6
Putting An Element Into A Max Heap
9
7
6
5
8
1
7
New element is 20.
2
6
Putting An Element Into A Max Heap
20
9
7
6
5
8
1
7
New element is 20.
2
6
Putting An Element Into A Max Heap
20
9
7
6
5
8
1
2
6
7
Complete binary tree with 11 nodes
.
Putting An Element Into A Max Heap
20
9
7
6
5
8
1
7
New element is 15.
2
6
Putting An Element Into A Max Heap
20
9
7
6
5
2
1
7
8
New element is 15.
6
Putting An Element Into A Max Heap
20
7
15
6
5
9
1
7
2
8
New element is 15.
6
Complexity Of Put
20
7
15
6
5
9
1
7
2
8
Complexity is O(log n), where n is heap
size.
6
Removing The Max Element
20
7
15
6
5
9
1
7
2
8
Max element is in the root.
6
Removing The Max Element
7
15
6
5
9
1
7
2
8
After max element is removed.
6
Removing The Max Element
7
15
6
5
9
1
7
2
8
Heap with 10 nodes.
Reinsert 8 into the heap.
6
Removing The Max Element
7
15
6
5
9
1
7
Reinsert 8 into the heap.
2
6
Removing The Max Element
15
7
6
5
9
1
7
Reinsert 8 into the heap.
2
6
Removing The Max Element
15
9
7
6
5
8
1
7
Reinsert 8 into the heap.
2
6
Removing The Max Element
15
9
7
6
5
8
1
7
Max element is 15.
2
6
Removing The Max Element
9
7
6
5
8
1
2
7
After max element is removed.
6
Removing The Max Element
9
7
6
5
8
1
7
Heap with 9 nodes.
2
6
Removing The Max Element
9
6
5
7
8
1
Reinsert 7.
2
6
Removing The Max Element
9
7
6
5
8
1
Reinsert 7.
2
6
Removing The Max Element
9
8
6
5
7
7
1
Reinsert 7.
2
6
Complexity Of Remove Max Element
9
8
6
5
7
7
1
Complexity is O(log n).
2
6
Complexity of Operations
Two good implementations are heaps and leftist trees.
isEmpty, size, and get => O(1) time
put and remove => O(log n) time where n is the size
of the priority queue
Practical Complexities
109 instructions/second
7/13/2017
2
3
n
n
nlogn n
n
1000
1mic
10mic
1milli
1sec
10000
10mic
130mic
100milli
17min
106
1milli
20milli
17min
32years
93
Impractical Complexities
109 instructions/second
7/13/2017
4
10
n
n
n
n
2
1000
17min
3.2 x 1013
years
3.2 x 10283
years
10000
116
days
106
3 x 107
years
???
???
??????
94
??????
Summary
n
Insertion Sort
Shellsort
Heapsort
Quicksort
O(n2)
O(n7/6)
O(n log n)
O(n log n)
10
0.00044
0.00041
0.00057
0.00052
100
0.00675
0.00171
0.00420
0.00284
1000
0.59564
0.02927
0.05565
0.03153
10000
58864
0.42998
0.71650
0.36765
100000
NA
5.7298
8.8591
4.2298
1000000
NA
71.164
104.68
47.065
7/13/2017
95
Faster Computer Vs Better Algorithm
Algorithmic improvement more useful
than hardware improvement.
E.g. 2n to n3
7/13/2017
96
13-Jul-17
97