scientific programming part b
play

Scientific Programming: Part B Trees Luca Bianco - Academic Year - PowerPoint PPT Presentation

Scientific Programming: Part B Trees Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor] Tree: examples Tree: examples Tree: examples Tree: examples Definitions Trees are data structures


  1. Scientific Programming: Part B Trees Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor]

  2. Tree: examples

  3. Tree: examples

  4. Tree: examples

  5. Tree: examples

  6. Definitions Trees are data structures composed of two elements: nodes and edges . Nodes represent things and edges represent relationships (typically non-symmetric) among two nodes.

  7. Definitions Facts ● One node called the root is the top level of the tree and is connected to one or more other nodes; ● If the root is connected to another node by means of one edge, then it is said to be the parent of the node (and that node is the child of the root); ● Any node can be parent of one or more other nodes, the only important thing is that all nodes have only one parent ; ● The root is the only exception as it does not have any parent . Some nodes do not have children and they are called leaves ;

  8. Recursive definition

  9. Terminology

  10. Terminology - 2

  11. Binary tree

  12. Binary tree: Node When implementing a tree we can define a node object and then a tree object that stores nodes. We will use the more compact way which is to use the recursive definition of a tree .

  13. Binary tree: ADT

  14. Binary tree: the code

  15. A sample tree...

  16. A sample tree...

  17. A sample tree...

  18. A sample tree... Exercise. write a print function that gets the root node and prints the tree:

  19. A sample tree... Exercise. write a print function that gets the root node and prints the tree: Tabs depend on depth

  20. A sample tree... OUTPUT Root (r)-> 2 Root (l)-> 1 1 (r)-> 5b 1 (l)-> 5a 5b (r)-> 5c 2 (l)-> 3 3 (r)-> 5 3 (l)-> 4 5 (l)-> child of 5

  21. Tree traversals To store all unfinished calls to DFS(node)

  22. Tree traversals Recursively Preorder: 1. visit Root Root 2. visit left 3. visit right To store all unfinished calls to DFS(node)

  23. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 1 2. visit left 1 Root 3. visit right

  24. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 5a 2. visit left 1 1 5a Root 3. visit right

  25. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 1 2. visit left 1 Root 5a 3. visit right

  26. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 5b 2. visit left 1 1 5a Root 3. visit right 5b

  27. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 5c 2. visit left 1 5b 5a 1 3. visit right 5b Root 5c

  28. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 5b 2. visit left 1 1 5a Root 3. visit right 5b 5c

  29. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 1 2. visit left 1 Root 5a 3. visit right 5b 5c

  30. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root Root 2. visit left 1 5a 3. visit right 5b 5c

  31. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 2 2. visit left 1 Root 5a 3. visit right 5b 5c 2

  32. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 3 2. visit left 1 2 5a Root 3. visit right 5b 5c 2 3

  33. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 4 2. visit left 1 3 5a 2 3. visit right 5b Root 5c 2 3 4

  34. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 3 2. visit left 1 2 5a Root 3. visit right 5b 5c 2 3 4

  35. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 5 2. visit left 1 3 5a 2 3. visit right 5b Root 5c 2 3 4 5

  36. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 3 2. visit left 1 2 5a Root 3. visit right 5b 5c 2 3 4 5

  37. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 2 2. visit left 1 Root 5a 3. visit right 5b 5c 2 3 4 5

  38. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 6 2. visit left 1 2 5a Root 3. visit right 5b 5c 2 3 4 5 6

  39. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 2 2. visit left 1 Root 5a 3. visit right 5b 5c 2 3 4 5 6

  40. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root Root 2. visit left 1 5a 3. visit right 5b 5c 2 3 4 5 6

  41. Tree traversals Recursively Preorder: Stack: (5c right of 5b!) 1. visit Root Root 2. visit left 1 5a 3. visit right empty! Done 5b 5c 2 3 4 5 6

  42. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left Root 2. visit Root 3. visit right

  43. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 1 2. visit Root Root 3. visit right

  44. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 5a 2. visit Root 1 3. visit right Root

  45. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 1 2. visit Root Root 3. visit right

  46. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 1 2. visit Root Root 1 3. visit right

  47. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 5b 2. visit Root 1 1 3. visit right Root

  48. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 5b 2. visit Root 1 1 3. visit right Root 5b

  49. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 5c 2. visit Root 5b 1 3. visit right 1 5b Root 5c

  50. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 5b 2. visit Root 1 1 3. visit right Root 5b 5c

  51. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 1 2. visit Root Root 1 3. visit right 5b 5c

  52. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a Root 2. visit Root 1 3. visit right 5b 5c

  53. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a Root 2. visit Root 1 3. visit right 5b 5c Root

  54. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 2 2. visit Root Root 1 3. visit right 5b 5c Root

  55. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 3 2. visit Root 2 1 3. visit right Root 5b 5c Root

  56. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 4 2. visit Root 3 1 3. visit right 2 5b Root 5c Root 4

  57. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 3 2. visit Root 2 1 3. visit right Root 5b 5c Root 4

  58. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 3 2. visit Root 2 1 3. visit right Root 5b 5c Root 4 3

  59. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 5 2. visit Root 3 1 3. visit right 2 5b Root 5c Root 4 3 5

  60. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 3 2. visit Root 2 1 3. visit right Root 5b 5c Root 4 3 5

  61. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 2 2. visit Root Root 1 3. visit right 5b 5c Root 4 3 5

  62. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 2 2. visit Root Root 1 3. visit right 5b 5c Root 4 3 5 2

  63. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 6 2. visit Root 2 1 3. visit right Root 5b 5c Root 4 3 5 2 6

  64. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 2 2. visit Root Root 1 3. visit right 5b 5c Root 4 3 5 2 6

  65. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a Root 2. visit Root 1 3. visit right 5b 5c Root 4 3 5 2 6

  66. Tree traversals Recursively Inorder: Stack: (5c right of 5b!) 1. visit left 5a 2. visit Root empty . Done! 1 3. visit right 5b 5c Root 4 3 5 2 6

  67. Tree traversals Recursively Postorder: Stack: Exercise! 1. visit left 5a 2. visit right 5c (right of 5b) 5b 3. visit Root 1 4 5 3 6 2 Root

  68. DFS: the code visit means “print” implicit stack Preorder: Inorder: Postorder: Root 5a 5a 1 1 5c 5a 5b 5b 5b 5c 1 5c Root 4 2 4 5 3 3 3 4 5 6 5 2 2 6 6 Root

  69. Tree traversals

  70. Tree traversals 0. Add root to the queue Q Visit order Queue Recursively Root 1. get node from Q 2. visit the node 3. add all children to Q

Recommend


More recommend