การวิเคราะห์และออกแบบขั้นตอนวิธี CSE 221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้าง ข้อมูลแบบ Trees ดร.สุรศักดิ์ มังสิงห์ 5/18/2019
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
Introduction to Trees General Trees Binary Trees Binary Search Trees AVL Trees 5/18/2019
Tree 5/18/2019
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
Sub-trees 5/18/2019
Tree 5/18/2019
height = depth = number of levels Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException Level 4 Level 2 Level 1 5/18/2019
Node Degree = Number Of Children 3 Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 1 2 1 1 5/18/2019
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
Binary Tree 5/18/2019
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
Binary Tree 5/18/2019
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
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
Forms of Binary Trees 5/18/2019
Complete Binary Trees 5/18/2019
Tree Traversal 5/18/2019
Processing and Walking Order 5/18/2019
Depth First Processing 5/18/2019
Preorder Traversal 5/18/2019
Breath First Processing 5/18/2019
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
5/18/2019
การประยุกต์ใช้ Tree Expression Tree 5/18/2019
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 ((, )). 5/18/2019
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 + 3.25 5/18/2019
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
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
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
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
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
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
Expression Tree 5/18/2019
Expression Tree 5/18/2019
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
Binary Tree Form / + a b - c d e f * (a + b) * (c – d) / (e + f) 5/18/2019
Expression Tree Infix Expression =? 5/18/2019
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
การประยุกต์ใช้ Tree Binary Search Trees 5/18/2019
Figure 8-1 Binary Search Tree 5/18/2019
Binary Search Trees 5/18/2019
Are these Binary Search Trees? 5/18/2019
Construct a Binary Search Tree เวลาที่ใช้ในการค้นหาข้อมูล Worst case? Average case? 5/18/2019
5/18/2019
Balance Binary Search Tree AVL Trees 5/18/2019
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
Binary Search Trees (b) AVL Tree (a) An unbalanced BST 5/18/2019
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
Out of Balance (left of left) 5/18/2019
Out of Balance (left of left) 5/18/2019
Out of Balance (right of right) 5/18/2019
Out of Balance (right of right) 5/18/2019
Simple double rotation right 5/18/2019
Complex double rotation right 5/18/2019
Insert a node to AVL tree 5/18/2019
Balancing BST 5/18/2019
Deleting a node from AVL tree 5/18/2019
Balance Binary Search Tree เวลาที่ใช้ในการค้นหาข้อมูลใน AVL Tree Worst case? Average case? 5/18/2019
5/18/2019
Priority Queue Heap Sort 5/18/2019
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
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
Root has minimum element. Min Tree Example 2 4 9 3 4 8 7 9 9 Root has minimum element.
Root has maximum element. Max Tree Example 9 4 9 8 4 2 7 3 1 Root has maximum element.
complete binary tree min tree Min Heap Definition complete binary tree min tree
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.
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.
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)).
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
Moving Up And Down A Heap 9 8 6 7 2 5 1 3 4
Putting An Element Into A Max Heap 9 8 6 7 2 5 1 7 Complete binary tree with 10 nodes.
Putting An Element Into A Max Heap 9 8 7 6 7 2 6 5 1 5 7 New element is 5.
Putting An Element Into A Max Heap 9 8 7 6 7 2 6 5 1 7 7 New element is 20.
Putting An Element Into A Max Heap 9 8 7 6 2 6 5 1 7 7 7 New element is 20.
Putting An Element Into A Max Heap 9 7 6 8 2 6 5 1 7 7 7 New element is 20.
Putting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 New element is 20.
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.
Putting An Element Into A Max Heap 20 9 7 6 8 2 6 5 1 7 7 7 New element is 15.
Putting An Element Into A Max Heap 20 9 7 6 2 6 5 1 7 7 8 7 8 New element is 15.
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.
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.
Removing The Max Element 8 6 7 2 5 1 20 9 15 Max element is in the root.
Removing The Max Element 15 7 6 9 2 6 5 1 7 7 8 7 8 After max element is removed.
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.
Removing The Max Element 15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.
Removing The Max Element 15 7 6 9 2 6 5 1 7 7 7 Reinsert 8 into the heap.
Removing The Max Element 15 9 7 6 8 2 6 5 1 7 7 7 Reinsert 8 into the heap.
Removing The Max Element 15 9 7 6 8 2 6 5 1 7 7 7 Max element is 15.
Removing The Max Element 9 7 6 8 2 6 5 1 7 7 7 After max element is removed.
Removing The Max Element 9 7 6 8 2 6 5 1 7 7 7 Heap with 9 nodes.
Removing The Max Element 9 7 6 8 2 6 5 1 Reinsert 7.
Removing The Max Element 9 7 6 8 2 6 5 1 Reinsert 7.
Removing The Max Element 9 8 7 6 7 2 6 5 1 Reinsert 7.
Complexity Of Remove Max Element 6 2 5 1 7 9 8 Complexity is O(log n).
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
การประยุกต์ใช้ Tree กับปัญหาต้องตัดสินใจ Next Lecture การประยุกต์ใช้ Tree กับปัญหาต้องตัดสินใจ 5/18/2019