1 / 97

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

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. Trees. Introduction to Trees. General Trees Binary Trees Binary Search Trees AVL Trees. Tree. Definition.

schweitzer
Download Presentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE221/ICT221Analysis and Design of AlgorithmsAnalysis of Algorithm using Tree Data Structure Asst.Prof. Dr.SurasakMungsing E-mail: Surasak.mu@spu.ac.th

  2. Trees

  3. Introduction to Trees • General Trees • Binary Trees • Binary Search Trees • AVL Trees

  4. Tree

  5. Definition • A tree tisa 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.

  6. Sub-trees

  7. Tree

  8. Level 1 Object OutputStream Number Throwable Level 3 Level 2 FileOutputStream Integer Double Exception Level 4 RuntimeException height = depth = number of levels

  9. Object OutputStream Number Throwable FileOutputStream Integer Double Exception RuntimeException Node Degree = Number Of Children 1 2 1 0 0 1 0 0

  10. Binary Tree

  11. Binary Tree • Finite (possibly empty) collection of elements. • Anonemptybinary tree has a rootelement. • The remaining elements (if any) are partitioned into two binary trees. • These are called the leftandright subtrees of the binary tree.

  12. Binary Tree

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

  14. a a b b A Tree vs A Binary Tree • The subtrees of a binary tree are ordered; those of a tree are not ordered. • Are different when viewed as binary trees. • Are the same when viewed as trees.

  15. Forms of Binary Trees

  16. Complete Binary Trees

  17. Tree Traversal

  18. Processing and Walking Order

  19. Depth First Processing

  20. Preorder Traversal

  21. Breath First Processing

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

  23. การประยุกต์ใช้Tree Expression Tree

  24. 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 ((, )).

  25. Infix Form • Normal way to write an expression. • Binary operators comeinbetween their left and right operands. • a * b • a + b * c • a * b / c • (a + b) * (c + d) + e – f/g*h + 3.25

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

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

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

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

  30. 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 afterthe postfix form of their operands. • Infix= a + b • Postfix = ab+

  31. Postfix Examples a b c + • Infix = a + b * c • Postfix= * • Infix = a * b + c • Postfix = a b c + * • Infix = (a + b) * (c – d) / (e + f) • Postfix = / a b c d - e f + + *

  32. Expression Tree

  33. Expression Tree

  34. + a b - a Binary Tree Form • a + b • - a

  35. / / * + e f + - a b c d Binary Tree Form • (a + b) * (c – d) / (e + f)

  36. Expression Tree Infix Expression =?

  37. + a c a b Constructing an Expression Tree a b + c d * - (a) (b) b d (c) (d) + * - a b c d + * a b c d

  38. การประยุกต์ใช้Tree Binary Search Trees

  39. Figure 8-1 Binary Search Tree

  40. Binary Search Trees

  41. Are these Binary Search Trees?

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

  43. Balance Binary Search Tree AVL Trees

  44. AVL Trees • Balancedbinary tree structure, named after Adelson, Velski, and Landis • An AVL tree is a height balanced binary search tree. • |HL – HR| <= 1 whereHLis the height of the left subtree and HRis the height of the left subtree

  45. Binary Search Trees (b) AVL Tree (a) An unbalanced BST

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

  47. Out of Balance (left of left)

  48. Out of Balance (left of left)

More Related