Download presentation
Presentation is loading. Please wait.
Published byBenny Darmadi Modified over 6 years ago
1
Dr.Surasak Mungsing E-mail: Surasak.mu@spu.ac.th
CSE 221/ICT221 การวิเคราะห์และออกแบบขั้นตอนวิธี Lecture 07: การวิเคราะห์ขั้นตอนวิธีที่ใช้ในโครงสร้างข้อมูลแบบ Trees Dr.Surasak Mungsing Feb-19
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. 2/24/2019
3
Introduction to Trees General Trees Binary Trees Binary Search Trees
AVL Trees 2/24/2019
4
Tree 2/24/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. 2/24/2019
6
Sub-trees 2/24/2019
7
Tree 2/24/2019
8
height = depth = number of levels
Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException Object Level 4 Level 2 Level 1 Feb-19
9
Node Degree = Number Of Children
3 Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 1 2 1 1 Feb-19
10
Tree Degree = Max Node Degree
3 Object Number Throwable OutputStream Integer Double Exception FileOutputStream RuntimeException 1 2 1 1 Feb-19
11
Binary Tree 2/24/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. 2/24/2019
13
Binary Tree 2/24/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. 2/24/2019
15
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
16
Forms of Binary Trees 2/24/2019
17
Complete Binary Trees 2/24/2019
18
Tree Traversal 2/24/2019
19
Processing and Walking Order
2/24/2019
20
Depth First Processing
2/24/2019
21
Preorder Traversal 2/24/2019
22
Breath First Processing
2/24/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 2/24/2019
24
2/24/2019
25
การประยุกต์ใช้ Tree Expression Tree 2/24/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 ((, )). 2/24/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 2/24/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. 2/24/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 2/24/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) 2/24/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. 2/24/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+ 2/24/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 + / 2/24/2019
34
Expression Tree 2/24/2019
35
Expression Tree 2/24/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. 2/24/2019
37
Binary Tree Form / + a b - c d e f * (a + b) * (c – d) / (e + f)
2/24/2019
38
Expression Tree Infix Expression =? 2/24/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 2/24/2019
40
การประยุกต์ใช้ Tree Binary Search Trees 2/24/2019
41
Binary Search Tree 2/24/2019
42
Binary Search Trees 2/24/2019
43
Are these Binary Search Trees?
2/24/2019
44
Construct a Binary Search Tree
เวลาที่ใช้ในการค้นหาข้อมูล Worst case? Average case? 2/24/2019
45
2/24/2019
46
Balance Binary Search Tree
AVL Trees 2/24/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 2/24/2019
48
Binary Search Trees (b) AVL Tree (a) An unbalanced BST 2/24/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 2/24/2019
50
Out of Balance (left of left)
2/24/2019
51
Out of Balance (left of left)
2/24/2019
52
Out of Balance (right of right)
2/24/2019
53
Out of Balance (right of right)
2/24/2019
54
Simple double rotation right
2/24/2019
55
Complex double rotation right
2/24/2019
56
Insert a node to AVL tree
2/24/2019
57
Balancing BST 2/24/2019
58
Deleting a node from AVL tree
2/24/2019
59
Balance Binary Search Tree
เวลาที่ใช้ในการค้นหาข้อมูลใน AVL Tree Worst case? Average case? 2/24/2019
60
ประยุกต์ใช้ Tree กับปัญหาต้องตัดสินใจ
Next Lecture: ประยุกต์ใช้ Tree กับปัญหาต้องตัดสินใจ 24-Feb-19
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.