Date post: | 04-Jan-2016 |
Category: |
Documents |
Upload: | barbra-barber |
View: | 220 times |
Download: | 0 times |
CSE221/ICT221CSE221/ICT221Analysis and Design of AlgorithmsAnalysis and Design of Algorithms
Analysis of Algorithm using Tree Data Structure
Asst.Prof. Dr.Surasak MungsingAsst.Prof. Dr.Surasak MungsingE-mail: [email protected]
04/20/23 1
04/20/23 2
Trees
04/20/23 3
Introduction to TreesIntroduction to Trees
General Trees Binary Trees Binary Search Trees AVL Trees
04/20/23 4
Tree
04/20/23 5
DefinitionDefinition
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.
04/20/23 6
Sub-trees
04/20/23 7
Tree
04/20/23 8
Level 3
Object
Number Throwable OutputStream
Integer Double Exception FileOutputStream
RuntimeException
Level 4
Level 2
Level 1
height = depth = number of levels
04/20/23 9
Object
Number Throwable OutputStream
Integer Double Exception FileOutputStream
RuntimeException
2 1 1
0 0 1 0
0
Node Degree = Number Of Children
04/20/23 10
Binary Tree
04/20/23 11
Binary TreeBinary 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.
04/20/23 12
Binary Tree
04/20/23 13
A Tree vs A Binary TreeA 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.
04/20/23 14
A Tree vs A Binary TreeA Tree vs A Binary Tree
The subtrees of a binary tree are ordered; those of a tree are not ordered.
a
b
a
b
• Are different when viewed as binary trees.
• Are the same when viewed as trees.
04/20/23 15
Forms of Binary Trees
04/20/23 16
Complete Binary Trees
04/20/23 17
Tree Traversal
04/20/23 18
Processing and Walking OrderProcessing and Walking Order
04/20/23 19
Depth First ProcessingDepth First Processing
04/20/23 20
Preorder TraversalPreorder Traversal
04/20/23 21
Breath First ProcessingBreath First Processing
04/20/23 22
Height and number of nodesHeight and number of nodes
Maximum height of a binary tree Hmax = N
Minimum height of a binary treeHmin = logN + 1
Maximum and Minimum number of nodesNmin = H and Nmax = 2H - 1
04/20/23 23
04/20/23 24
Expression Tree
การประยุ�กต์ใช้� Tree
04/20/23 25
Arithmetic ExpressionsArithmetic Expressions
Expressions comprise three kinds of entities. Operators (+, -, /, *). Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d),
etc.). Delimiters ((, )).
(a + b) * (c + d) + e – f/g*h + 3.25
04/20/23 26
Infix FormInfix Form
Normal way to write an expression. Binary operators come in between their
left and right operands. a * b a + b * c a * b / c (a + b) * (c + d) + e – f/g*h + 3.25
04/20/23 27
Operator PrioritiesOperator 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.
04/20/23 28
Tie BreakerTie 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
04/20/23 29
DelimitersDelimiters
Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. (a + b) * (c – d) / (e – f)
04/20/23 30
Infix Expression Is Hard To ParseInfix 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.
04/20/23 31
Postfix FormPostfix 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+
04/20/23 32
Postfix ExamplesPostfix 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 + /
04/20/23 33
Expression TreeExpression Tree
04/20/23 34
Expression TreeExpression Tree
04/20/23 35
Binary Tree FormBinary Tree Form a + b +
a b
• - a -
a
04/20/23 36
Binary Tree FormBinary Tree Form
(a + b) * (c – d) / (e + f)
/
+
a b
-
c d
+
e f
*
/
04/20/23 37
Expression Tree
Infix Expression =?
04/20/23 38
Constructing an Expression TreeConstructing an Expression Treea b + c d * -
a bc da b
+
a b
+
c d
*
a b
+
c d
*
-
(a) (b)
(c) (d)
04/20/23 39
Binary Search Trees
การประยุ�กต์ใช้� Tree
04/20/23 40
Figure 8-1
Binary Search Tree
04/20/23 41
Binary Search TreesBinary Search Trees
04/20/23 42
Are these Binary Search Trees?
04/20/23 43
Construct a Binary Search TreeConstruct a Binary Search Tree
เวลาที่��ใช้�ในการค้�นหาข้�อมู�ล Worst case?Average case?
04/20/23 44
04/20/23 45
AVL Trees
Balance Binary Search Tree
04/20/23 46
AVL TreesAVL 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
04/20/23 47
Binary Search TreesBinary Search Trees
(b) AVL Tree
(a) An unbalanced BST
04/20/23 48
Out of BalanceOut of Balance
Four cases of out of balance:
left of left (LL) - requires single rotation right of right (RR) - requires single rotation
Left of right (LR) - requires double rotation Right of left (RL) - requires double rotation
04/20/23 49
Out of Balance (left of left)Out of Balance (left of left)
04/20/23 50
Out of Balance (left of left)Out of Balance (left of left)
04/20/23 51
Out of Balance (right of right)Out of Balance (right of right)
04/20/23 52
Out of Balance (right of right)Out of Balance (right of right)
04/20/23 53
Simple double rotation rightSimple double rotation right
04/20/23 54
Complex double rotation rightComplex double rotation right
04/20/23 55
Insert a node to AVL treeInsert a node to AVL tree
04/20/23 56
Balancing BSTBalancing BST
04/20/23 57
Deleting a node from AVL treeDeleting a node from AVL tree
04/20/23 58
เวลาที่��ใช้�ในการค้�นหาข้�อมู�ลใน AVL Tree
Worst case?Average case?
Balance Binary Search Tree
04/20/23 59
Priority QueuePriority Queue
04/20/23 60
• Collection of elements.• Each element has a priority or key.• Supports following operations:
isEmpty size add/put an element into the priority queue get element with min/max priority remove element with min/max priority
Min Tree ExampleMin Tree Example
04/20/2361
2
4 9 3
4 8 7
9 9
Root is the minimum element
Max Tree ExampleMax Tree Example
04/20/2362
9
4 9 8
4 2 7
3 1
Root is the maximum element
Min Heap DefinitionMin Heap Definition
• complete binary tree• min tree
04/20/2363
Complete binary tree with 9 nodes that is also a min tree.
2
4
6 7 9 3
8 6
3
Max Heap With 9 NodesMax Heap With 9 Nodes
04/20/2364
9
8
6 7 2 6
5 1
7
Complete binary tree with 9 nodes that is also a max tree.
Heap HeightHeap Height
What is the height of an n node heap ?
04/20/23 65
Since a heap is a complete binary tree, the height of an n node heap is log2 (n+1).
9 8 7 6 7 2 6 5 1
1 2 3 4 5 6 7 8 9 100
A Heap Is Efficiently Represented As An Array
9
8
6 7 2 6
5 1
7
Moving Up And Down A Heap
9
8
6 7 2 6
5 1
7
1
2 3
4 5 6 7
8 9
Putting An Element Into A Max Heap
Complete binary tree with 10 nodes.
9
8
6 7 2 6
5 1
7
7
Putting An Element Into A Max Heap
New element is 5.
9
8
6 7 2 6
5 1
7
75
Putting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
7
7
Putting An Element Into A Max Heap
New element is 20.
9
8
6
7
2 6
5 1
7
77
Putting An Element Into A Max Heap
New element is 20.
9
86
7
2 6
5 1
7
77
Putting An Element Into A Max Heap
New element is 20.
9
86
7
2 6
5 1
7
77
20
Putting An Element Into A Max Heap
Complete binary tree with 11 nodes.
9
86
7
2 6
5 1
7
77
20
Putting An Element Into A Max Heap
New element is 15.
9
86
7
2 6
5 1
7
77
20
Putting An Element Into A Max Heap
New element is 15.
9
8
6
7
2 6
5 1
7
77
20
8
Putting An Element Into A Max Heap
New element is 15.
8
6
7
2 6
5 1
7
77
20
8
9
15
Complexity Of Put
Complexity is O(log n), where n is heap size.
8
6
7
2 6
5 1
7
77
20
8
9
15
Removing The Max Element
Max element is in the root.
8
6
7
2 6
5 1
7
77
20
8
9
15
Removing The Max Element
After max element is removed.
8
6
7
2 6
5 1
7
77 8
9
15
Removing The Max Element
Heap with 10 nodes.
8
6
7
2 6
5 1
7
77 8
9
15
Reinsert 8 into the heap.
Removing The Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
Removing The Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
Removing The Max Element
Reinsert 8 into the heap.
6
7
2 6
5 1
7
77
9
15
8
Removing The Max Element
Max element is 15.
6
7
2 6
5 1
7
77
9
15
8
Removing The Max Element
After max element is removed.
6
7
2 6
5 1
7
77
9
8
Removing The Max Element
Heap with 9 nodes.
6
7
2 6
5 1
7
77
9
8
Removing The Max Element
Reinsert 7.
6 2 6
5 1
79
8
Removing The Max Element
Reinsert 7.
6 2 6
5 1
7
9
8
Removing The Max Element
Reinsert 7.
6 2 6
5 1
7
9
8
7
Complexity Of Remove Max Element
Complexity is O(log n).
6 2 6
5 1
7
9
8
7
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
04/20/23 93
Practical ComplexitiesPractical Complexities
109 instructions/second
n n nlogn n2 n3
1000 1mic 10mic 1milli 1sec
10000 10mic 130mic 100milli 17min
106 1milli 20milli 17min 32years
04/20/23 94
Impractical ComplexitiesImpractical Complexities109 instructions/second
n n4 n10 2n
1000 17min 3.2 x 1013 years
3.2 x 10283 years
10000
116 days
??? ???
106 3 x 107 years
?????? ??????
04/20/23 95
Summary
n Insertion Sort
O(n2)
Shellsort
O(n7/6)
Heapsort
O(n log n)
Quicksort
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
04/20/23 96
Faster Computer Vs Better Faster Computer Vs Better AlgorithmAlgorithm
Algorithmic improvement more usefulthan hardware improvement.
E.g. 2n to n3
Apr 20, 2023 97