Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof.

Similar presentations


Presentation on theme: "CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof."— Presentation transcript:

1

2 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 E-mail: Surasak.mu@spu.ac.th Nov-151

3 11/5/20152 Trees

4 11/5/20153 Introduction to Trees  General Trees  Binary Trees  Binary Search Trees  AVL Trees

5 11/5/20154 Tree

6 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.

7 11/5/20156 Sub-trees

8 11/5/20157 Tree

9 Nov-158 Level 3 Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException Level 4 Level 2 Level 1 height = depth = number of levels

10 Nov-159 Object NumberThrowable OutputStream IntegerDoubleException FileOutputStream RuntimeException 2 1 1 0 01 0 0 Node Degree = Number Of Children

11 11/5/201510 Binary Tree

12 11/5/201511 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.

13 11/5/201512 Binary Tree

14 11/5/201513 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.

15 11/5/201514 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.

16 11/5/201515 Forms of Binary Trees

17 11/5/201516 Complete Binary Trees

18 11/5/201517 Tree Traversal

19 11/5/201518 Processing and Walking Order

20 11/5/201519 Depth First Processing

21 11/5/201520 Preorder Traversal

22 11/5/201521 Breath First Processing

23 11/5/201522 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

24 11/5/201523

25 11/5/201524 Expression Tree การประยุกต์ใช้ Tree

26 11/5/201525 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 + 3.25

27 11/5/201526 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

28 11/5/201527 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.

29 11/5/201528 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

30 11/5/201529 Delimiters  Subexpression within delimiters is treated as a single operand, independent from the remainder of the expression.  (a + b) * (c – d) / (e – f)

31 11/5/201530 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.

32 11/5/201531 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+

33 11/5/201532 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+ /

34 11/5/201533 Expression Tree

35 11/5/201534 Expression Tree

36 11/5/201535 Binary Tree Form  a + b + ab - a - a

37 11/5/201536 Binary Tree Form  (a + b) * (c – d) / (e + f) / + a b - c d + e f * /

38 11/5/201537 Expression Tree Infix Expression =?

39 11/5/201538 Constructing an Expression Tree a b + c d * - a b c d ab + ab + c d * ab + c d * - (a) (b) (c) (d)

40 11/5/201539 Binary Search Trees การประยุกต์ใช้ Tree

41 11/5/201540 Figure 8-1 Binary Search Tree

42 11/5/201541 Binary Search Trees

43 11/5/201542 Are these Binary Search Trees?

44 11/5/201543 Construct a Binary Search Tree เวลาที่ใช้ในการค้นหา ข้อมูล Worst case? Average case?

45 11/5/201544

46 11/5/201545 AVL Trees Balance Binary Search Tree

47 11/5/201546 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

48 11/5/201547 Binary Search Trees (b) AVL Tree (a) An unbalanced BST

49 11/5/201548 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

50 11/5/201549 Out of Balance (left of left)

51 11/5/201550 Out of Balance (left of left)

52 11/5/201551 Out of Balance (right of right)

53 11/5/201552 Out of Balance (right of right)

54 11/5/201553 Simple double rotation right

55 11/5/201554 Complex double rotation right

56 11/5/201555 Insert a node to AVL tree

57 11/5/201556 Balancing BST

58 11/5/201557 Deleting a node from AVL tree

59 11/5/201558 เวลาที่ใช้ในการค้นหาข้อมูลใน AVL Tree Worst case? Average case? Balance Binary Search Tree

60 11/5/201559

61 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

62 Min Tree Example Nov-15 61 2 493 4 87 99 Root is the minimum element

63 Max Tree Example Nov-15 62 9 4 98 4 27 31 Root is the maximum element

64 Min Heap Definition complete binary tree min tree Nov-15 63 Complete binary tree with 9 nodes that is also a min tree. 2 4 6793 86 3

65 Max Heap With 9 Nodes Nov-15 64 9 8 6726 51 7 Complete binary tree with 9 nodes that is also a max tree.

66 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).

67 987672651 123456789100 A Heap Is Efficiently Represented As An Array 9 8 6726 51 7

68 Moving Up And Down A Heap 9 8 6726 51 7 1 23 4 56 7 89

69 Putting An Element Into A Max Heap Complete binary tree with 10 nodes. 9 8 6726 51 7 7

70 Putting An Element Into A Max Heap New element is 5. 9 8 6726 51 7 75

71 Putting An Element Into A Max Heap New element is 20. 9 8 6 7 26 51 7 7 7

72 Putting An Element Into A Max Heap New element is 20. 9 8 6 7 26 51 7 7 7

73 Putting An Element Into A Max Heap New element is 20. 9 86 7 26 51 7 7 7

74 Putting An Element Into A Max Heap New element is 20. 9 86 7 26 51 7 7 7 20

75 Putting An Element Into A Max Heap Complete binary tree with 11 nodes. 9 86 7 26 51 7 7 7 20

76 Putting An Element Into A Max Heap New element is 15. 9 86 7 26 51 7 7 7 20

77 Putting An Element Into A Max Heap New element is 15. 9 8 6 7 26 51 7 7 7 20 8

78 Putting An Element Into A Max Heap New element is 15. 8 6 7 26 51 7 7 7 20 8 9 15

79 Complexity Of Put Complexity is O(log n), where n is heap size. 8 6 7 26 51 7 7 7 20 8 9 15

80 Removing The Max Element Max element is in the root. 8 6 7 26 51 7 7 7 20 8 9 15

81 Removing The Max Element After max element is removed. 8 6 7 26 51 7 7 7 8 9 15

82 Removing The Max Element Heap with 10 nodes. 8 6 7 26 51 7 7 7 8 9 15 Reinsert 8 into the heap.

83 Removing The Max Element Reinsert 8 into the heap. 6 7 26 51 7 7 7 9 15

84 Removing The Max Element Reinsert 8 into the heap. 6 7 26 51 7 7 7 9 15

85 Removing The Max Element Reinsert 8 into the heap. 6 7 26 51 7 7 7 9 15 8

86 Removing The Max Element Max element is 15. 6 7 26 51 7 7 7 9 15 8

87 Removing The Max Element After max element is removed. 6 7 26 51 7 7 7 9 8

88 Removing The Max Element Heap with 9 nodes. 6 7 26 51 7 7 7 9 8

89 Removing The Max Element Reinsert 7. 626 51 79 8

90 Removing The Max Element Reinsert 7. 626 51 7 9 8

91 Removing The Max Element Reinsert 7. 626 51 7 9 8 7

92 Complexity Of Remove Max Element Complexity is O(log n). 626 51 7 9 8 7

93 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

94 11/5/201593 Practical Complexities 10 9 instructions/second

95 11/5/201594 Impractical Complexities 10 9 instructions/second

96 11/5/201595 Summary nInsertion Sort O(n 2 ) Shellsort O(n 7/6 ) Heapsort O(n log n) Quicksort O(n log n) 100.000440.000410.000570.00052 1000.006750.001710.004200.00284 10000.595640.029270.055650.03153 10000588640.429980.716500.36765 100000NA5.72988.85914.2298 1000000NA71.164104.6847.065

97 11/5/201596 Faster Computer Vs Better Algorithm Algorithmic improvement more useful than hardware improvement. E.g. 2 n to n 3

98 5-Nov-1597


Download ppt "CSE221/ICT221 Analysis and Design of Algorithms CSE221/ICT221 Analysis and Design of Algorithms Analysis of Algorithm using Tree Data Structure Asst.Prof."

Similar presentations


Ads by Google