Download presentation
Presentation is loading. Please wait.
1
การวิเคราะห์และออกแบบขั้นตอนวิธี
CSE 221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้าง ข้อมูลแบบ Trees ดร.สุรศักดิ์ มังสิงห์ 5/18/2019
2
Trees Trees are a very useful data structure. Many different kinds of trees are used in Computer Science. We shall study just a few of these. 5/18/2019
3
Introduction to Trees General Trees Binary Trees Binary Search Trees
AVL Trees 5/18/2019
4
Tree 5/18/2019
5
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. 5/18/2019
6
Sub-trees 5/18/2019
7
Tree 5/18/2019
8
height = depth = number of levels
Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException Level 4 Level 2 Level 1 5/18/2019
9
Node Degree = Number Of Children
3 Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 1 2 1 1 5/18/2019
10
Tree Degree = Max Node Degree
3 Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 1 2 1 1 Note that it is possible to have a tree whose degree is (say) 3 and whose root has a degree that is < 3. Degree of tree = 3. 5/18/2019
11
Binary Tree 5/18/2019
12
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. 5/18/2019
13
Binary Tree 5/18/2019
14
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. 5/18/2019
15
Are different when viewed as binary trees.
A Tree vs A Binary Tree The subtrees of a binary tree are ordered; those of a tree are not ordered. a b Are different when viewed as binary trees. Are the same when viewed as trees. 5/18/2019
16
Forms of Binary Trees 5/18/2019
17
Complete Binary Trees 5/18/2019
18
Tree Traversal 5/18/2019
19
Processing and Walking Order
5/18/2019
20
Depth First Processing
5/18/2019
21
Preorder Traversal 5/18/2019
22
Breath First Processing
5/18/2019
23
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 5/18/2019
24
5/18/2019
25
การประยุกต์ใช้ Tree Expression Tree 5/18/2019
26
Arithmetic Expressions
(a + b) * (c + d) + e – f/g*h Expressions comprise three kinds of entities. Operators (+, -, /, *). Operands (a, b, c, d, e, f, g, h, 3.25, (a + b), (c + d), etc.). Delimiters ((, )). 5/18/2019
27
Infix 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 5/18/2019
28
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. 5/18/2019
29
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 5/18/2019
30
Delimiters Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. (a + b) * (c – d) / (e – f) 5/18/2019
31
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. 5/18/2019
32
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+ 5/18/2019
33
Postfix Examples a b c * + a b * c + a b + c d - * e f + /
Infix = a + b * c Postfix = a b c * + Infix = a * b + c Postfix = a b * c + Infix = (a + b) * (c – d) / (e + f) Postfix = a b + c d - * e f + / 5/18/2019
34
Expression Tree 5/18/2019
35
Expression Tree 5/18/2019
36
Binary Tree Form + a b - a - a a + b
Each leaf represents a variable or constant. Each nonleaf represents an operator. The left operand (if any) of this operator is represented by the left subtree, the right subtree represents the right operand of the operator. 5/18/2019
37
Binary Tree Form / + a b - c d e f * (a + b) * (c – d) / (e + f)
5/18/2019
38
Expression Tree Infix Expression =? 5/18/2019
39
Constructing an Expression Tree
a b + c d * - (a) (b) a a b + c b d (c) (d) + * - a b c d + * a b c d 5/18/2019
40
การประยุกต์ใช้ Tree Binary Search Trees 5/18/2019
41
Figure 8-1 Binary Search Tree 5/18/2019
42
Binary Search Trees 5/18/2019
43
Are these Binary Search Trees?
5/18/2019
44
Construct a Binary Search Tree
เวลาที่ใช้ในการค้นหาข้อมูล Worst case? Average case? 5/18/2019
45
5/18/2019
46
Balance Binary Search Tree
AVL Trees 5/18/2019
47
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 5/18/2019
48
Binary Search Trees (b) AVL Tree (a) An unbalanced BST 5/18/2019
49
Out of Balance Four cases of out of balance:
left of left - requires single rotation right of right - requires single rotation Left of right - requires double rotation Right of left - requires double rotation 5/18/2019
50
Out of Balance (left of left)
5/18/2019
51
Out of Balance (left of left)
5/18/2019
52
Out of Balance (right of right)
5/18/2019
53
Out of Balance (right of right)
5/18/2019
54
Simple double rotation right
5/18/2019
55
Complex double rotation right
5/18/2019
56
Insert a node to AVL tree
5/18/2019
57
Balancing BST 5/18/2019
58
Deleting a node from AVL tree
5/18/2019
59
Balance Binary Search Tree
เวลาที่ใช้ในการค้นหาข้อมูลใน AVL Tree Worst case? Average case? 5/18/2019
60
5/18/2019
61
Priority Queue Heap Sort 5/18/2019
62
Min Priority Queue 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 priority remove element with min priority
63
Max Priority Queue 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 max priority remove element with max priority
64
Root has minimum element.
Min Tree Example 2 4 9 3 4 8 7 9 9 Root has minimum element.
65
Root has maximum element.
Max Tree Example 9 4 9 8 4 2 7 3 1 Root has maximum element.
66
complete binary tree min tree
Min Heap Definition complete binary tree min tree
67
Complete binary tree with 9 nodes that is also a min tree.
Min Heap With 9 Nodes 2 4 6 7 9 3 8 Complete binary tree with 9 nodes that is also a min tree.
68
Complete binary tree with 9 nodes that is also a max tree.
Max Heap With 9 Nodes 9 8 6 7 2 5 1 Complete binary tree with 9 nodes that is also a max tree.
69
What is the height of an n node heap ?
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). Since height must be an integer, the precise expression for height is ceiling(log2 (n+1)).
70
A Heap Is Efficiently Represented As An Array
9 8 6 7 2 5 1 9 8 7 6 2 5 1 3 4 10
71
Moving Up And Down A Heap
9 8 6 7 2 5 1 3 4
72
Putting An Element Into A Max Heap
9 8 6 7 2 5 1 7 Complete binary tree with 10 nodes.
73
Putting An Element Into A Max Heap
9 8 7 6 7 2 6 5 1 5 7 New element is 5.
74
Putting An Element Into A Max Heap
9 8 7 6 7 2 6 5 1 7 7 New element is 20.
75
Putting An Element Into A Max Heap
9 8 7 6 2 6 5 1 7 7 7 New element is 20.
76
Putting An Element Into A Max Heap
9 7 6 8 2 6 5 1 7 7 7 New element is 20.
77
Putting An Element Into A Max Heap
20 9 7 6 8 2 6 5 1 7 7 7 New element is 20.
78
Putting An Element Into A Max Heap
20 9 7 6 8 2 6 5 1 7 7 7 Complete binary tree with 11 nodes.
79
Putting An Element Into A Max Heap
20 9 7 6 8 2 6 5 1 7 7 7 New element is 15.
80
Putting An Element Into A Max Heap
20 9 7 6 2 6 5 1 7 7 8 7 8 New element is 15.
81
Putting An Element Into A Max Heap
20 15 7 6 9 2 6 5 1 7 7 8 7 8 New element is 15.
82
Complexity is O(log n), where n is heap size.
Complexity Of Put 8 6 7 2 5 1 20 9 15 Complexity is O(log n), where n is heap size.
83
Removing The Max Element
8 6 7 2 5 1 20 9 15 Max element is in the root.
84
Removing The Max Element
15 7 6 9 2 6 5 1 7 7 8 7 8 After max element is removed.
85
Removing The Max Element
15 7 6 9 2 6 5 1 7 7 8 7 8 Heap with 10 nodes. Reinsert 8 into the heap.
86
Removing The Max Element
15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.
87
Removing The Max Element
15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.
88
Removing The Max Element
15 9 7 6 8 2 6 5 1 7 7 7 Reinsert 8 into the heap.
89
Removing The Max Element
15 9 7 6 8 2 6 5 1 7 7 7 Max element is 15.
90
Removing The Max Element
9 7 6 8 2 6 5 1 7 7 7 After max element is removed.
91
Removing The Max Element
9 7 6 8 2 6 5 1 7 7 7 Heap with 9 nodes.
92
Removing The Max Element
9 7 6 8 2 6 5 1 Reinsert 7.
93
Removing The Max Element
9 7 6 8 2 6 5 1 Reinsert 7.
94
Removing The Max Element
9 8 7 6 7 2 6 5 1 Reinsert 7.
95
Complexity Of Remove Max Element
6 2 5 1 7 9 8 Complexity is O(log n).
96
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
97
การประยุกต์ใช้ Tree กับปัญหาต้องตัดสินใจ
Next Lecture การประยุกต์ใช้ Tree กับปัญหาต้องตัดสินใจ 5/18/2019
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.