Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ Trees Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th Feb-19
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. 2/24/2019
Introduction to Trees General Trees Binary Trees Binary Search Trees AVL Trees 2/24/2019
Tree 2/24/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. 2/24/2019
Sub-trees 2/24/2019
Tree 2/24/2019
height = depth = number of levels Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException Object Level 4 Level 2 Level 1 Feb-19
Node Degree = Number Of Children 3 Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 1 2 1 1 Feb-19
Tree Degree = Max Node Degree 3 Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 1 2 1 1 Feb-19
Binary Tree 2/24/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. 2/24/2019
Binary Tree 2/24/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. 2/24/2019
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. 2/24/2019
Forms of Binary Trees 2/24/2019
Complete Binary Trees 2/24/2019
Tree Traversal 2/24/2019
Processing and Walking Order 2/24/2019
Depth First Processing 2/24/2019
Preorder Traversal 2/24/2019
Breath First Processing 2/24/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 2/24/2019
2/24/2019
การประยุกต์ใช้ Tree Expression Tree 2/24/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 ((, )). 2/24/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 2/24/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. 2/24/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 2/24/2019
Delimiters Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression. (a + b) * (c – d) / (e – f) 2/24/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. 2/24/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+ 2/24/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 + / 2/24/2019
Expression Tree 2/24/2019
Expression Tree 2/24/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. 2/24/2019
Binary Tree Form / + a b - c d e f * (a + b) * (c – d) / (e + f) 2/24/2019
Expression Tree Infix Expression =? 2/24/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 2/24/2019
การประยุกต์ใช้ Tree Binary Search Trees 2/24/2019
Binary Search Tree 2/24/2019
Binary Search Trees 2/24/2019
Are these Binary Search Trees? 2/24/2019
Construct a Binary Search Tree เวลาที่ใช้ในการค้นหาข้อมูล Worst case? Average case? 2/24/2019
2/24/2019
Balance Binary Search Tree AVL Trees 2/24/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 2/24/2019
Binary Search Trees (b) AVL Tree (a) An unbalanced BST 2/24/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 2/24/2019
Out of Balance (left of left) 2/24/2019
Out of Balance (left of left) 2/24/2019
Out of Balance (right of right) 2/24/2019
Out of Balance (right of right) 2/24/2019
Simple double rotation right 2/24/2019
Complex double rotation right 2/24/2019
Insert a node to AVL tree 2/24/2019
Balancing BST 2/24/2019
Deleting a node from AVL tree 2/24/2019
Balance Binary Search Tree เวลาที่ใช้ในการค้นหาข้อมูลใน AVL Tree Worst case? Average case? 2/24/2019
ประยุกต์ใช้ Tree กับปัญหาต้องตัดสินใจ Next Lecture: ประยุกต์ใช้ Tree กับปัญหาต้องตัดสินใจ 24-Feb-19