CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof. Dr.Surasak Mungsing Nov-151
11/5/20152 Trees
11/5/20153 Introduction to Trees General Trees Binary Trees Binary Search Trees AVL Trees
11/5/20154 Tree
11/5/20155 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.
11/5/20156 Sub-trees
11/5/20157 Tree
Nov-158 Level 3 Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException Level 4 Level 2 Level 1 height = depth = number of levels
Nov-159 Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException Node Degree = Number Of Children
11/5/ Binary Tree
11/5/ 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.
11/5/ Binary Tree
11/5/ 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.
11/5/ A 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.
11/5/ Forms of Binary Trees
11/5/ Complete Binary Trees
11/5/ Tree Traversal
11/5/ Processing and Walking Order
11/5/ Depth First Processing
11/5/ Preorder Traversal
11/5/ Breath First Processing
11/5/ Height and number of nodes Maximum height of a binary tree H max = N Minimum height of a binary tree H min = logN + 1 Maximum and Minimum number of nodes N min = H and N max = 2 H - 1
11/5/201523
11/5/ Expression Tree การประยุกต์ใช้ Tree
11/5/ Arithmetic 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
11/5/ 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
11/5/ 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.
11/5/ 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
11/5/ Delimiters Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. (a + b) * (c – d) / (e – f)
11/5/ 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.
11/5/ 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+
11/5/ Postfix Examples Infix = a + b * c Postfix = abc * + Infix = a * b + c Postfix = ab * c + Infix = (a + b) * (c – d) / (e + f) Postfix = ab + cd- * ef+ /
11/5/ Expression Tree
11/5/ Expression Tree
11/5/ Binary Tree Form a + b + ab - a - a
11/5/ Binary Tree Form (a + b) * (c – d) / (e + f) / + a b - c d + e f * /
11/5/ Expression Tree Infix Expression =?
11/5/ Constructing an Expression Tree a b + c d * - a b c d ab + ab + c d * ab + c d * - (a) (b) (c) (d)
11/5/ Binary Search Trees การประยุกต์ใช้ Tree
11/5/ Figure 8-1 Binary Search Tree
11/5/ Binary Search Trees
11/5/ Are these Binary Search Trees?
11/5/ Construct a Binary Search Tree เวลาที่ใช้ในการค้นหา ข้อมูล Worst case? Average case?
11/5/201544
11/5/ AVL Trees Balance Binary Search Tree
11/5/ AVL Trees Balanced binary tree structure, named after Adelson, Velski, and Landis An AVL tree is a height balanced binary search tree. |H L – H R | <= 1 where H L is the height of the left subtree and H R is the height of the left subtree
11/5/ Binary Search Trees (b) AVL Tree (a) An unbalanced BST
11/5/ Out 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
11/5/ Out of Balance (left of left)
11/5/ Out of Balance (left of left)
11/5/ Out of Balance (right of right)
11/5/ Out of Balance (right of right)
11/5/ Simple double rotation right
11/5/ Complex double rotation right
11/5/ Insert a node to AVL tree
11/5/ Balancing BST
11/5/ Deleting a node from AVL tree
11/5/ เวลาที่ใช้ในการค้นหาข้อมูลใน AVL Tree Worst case? Average case? Balance Binary Search Tree
11/5/201559
Priority Queue Nov-1560 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 Example Nov Root is the minimum element
Max Tree Example Nov Root is the maximum element
Min Heap Definition complete binary tree min tree Nov Complete binary tree with 9 nodes that is also a min tree
Max Heap With 9 Nodes Nov Complete binary tree with 9 nodes that is also a max tree.
Heap Height What is the height of an n node heap ? Nov-1565 Since a heap is a complete binary tree, the height of an n node heap is log 2 (n+1).
A Heap Is Efficiently Represented As An Array
Moving Up And Down A Heap
Putting An Element Into A Max Heap Complete binary tree with 10 nodes
Putting An Element Into A Max Heap New element is
Putting An Element Into A Max Heap New element is
Putting An Element Into A Max Heap New element is
Putting An Element Into A Max Heap New element is
Putting An Element Into A Max Heap New element is
Putting An Element Into A Max Heap Complete binary tree with 11 nodes
Putting An Element Into A Max Heap New element is
Putting An Element Into A Max Heap New element is
Putting An Element Into A Max Heap New element is
Complexity Of Put Complexity is O(log n), where n is heap size
Removing The Max Element Max element is in the root
Removing The Max Element After max element is removed
Removing The Max Element Heap with 10 nodes Reinsert 8 into the heap.
Removing The Max Element Reinsert 8 into the heap
Removing The Max Element Reinsert 8 into the heap
Removing The Max Element Reinsert 8 into the heap
Removing The Max Element Max element is
Removing The Max Element After max element is removed
Removing The Max Element Heap with 9 nodes
Removing The Max Element Reinsert
Removing The Max Element Reinsert
Removing The Max Element Reinsert
Complexity Of Remove Max Element 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
11/5/ Practical Complexities 10 9 instructions/second
11/5/ Impractical Complexities 10 9 instructions/second
11/5/ Summary nInsertion Sort O(n 2 ) Shellsort O(n 7/6 ) Heapsort O(n log n) Quicksort O(n log n) NA NA
11/5/ Faster Computer Vs Better Algorithm Algorithmic improvement more useful than hardware improvement. E.g. 2 n to n 3
5-Nov-1597