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

scientific programming part b
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Scientific Programming: Part B

Trees

Luca Bianco - Academic Year 2019-20 luca.bianco@fmach.it [credits: thanks to Prof. Alberto Montresor]

slide-2
SLIDE 2

Tree: examples

slide-3
SLIDE 3

Tree: examples

slide-4
SLIDE 4

Tree: examples

slide-5
SLIDE 5

Tree: examples

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

slide-7
SLIDE 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
  • nly 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;

slide-8
SLIDE 8

Recursive definition

slide-9
SLIDE 9

Terminology

slide-10
SLIDE 10

Terminology - 2

slide-11
SLIDE 11

Binary tree

slide-12
SLIDE 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.

slide-13
SLIDE 13

Binary tree: ADT

slide-14
SLIDE 14

Binary tree: the code

slide-15
SLIDE 15

A sample tree...

slide-16
SLIDE 16

A sample tree...

slide-17
SLIDE 17

A sample tree...

slide-18
SLIDE 18

A sample tree...

  • Exercise. write a print function that gets the root

node and prints the tree:

slide-19
SLIDE 19

A sample tree...

  • Exercise. write a print function that gets the root

node and prints the tree:

Tabs depend

  • n depth
slide-20
SLIDE 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

slide-21
SLIDE 21

Tree traversals

To store all unfinished calls to DFS(node)

slide-22
SLIDE 22

Tree traversals

Preorder:

Root

Recursively 1. visit Root 2. visit left 3. visit right

To store all unfinished calls to DFS(node)

slide-23
SLIDE 23

Tree traversals

Preorder:

Root 1

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

1 Root

slide-24
SLIDE 24

Tree traversals

Preorder:

Root 1 5a

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

5a 1 Root

slide-25
SLIDE 25

Tree traversals

Preorder:

Root 1 5a

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

1 Root

slide-26
SLIDE 26

Tree traversals

Preorder:

Root 1 5a 5b

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

5b 1 Root

slide-27
SLIDE 27

Tree traversals

Preorder:

Root 1 5a 5b 5c

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

5c 5b 1 Root

slide-28
SLIDE 28

Tree traversals

Preorder:

Root 1 5a 5b 5c

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

5b 1 Root

slide-29
SLIDE 29

Tree traversals

Preorder:

Root 1 5a 5b 5c

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

1 Root

slide-30
SLIDE 30

Tree traversals

Preorder:

Root 1 5a 5b 5c

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

Root

slide-31
SLIDE 31

Tree traversals

Preorder:

Root 1 5a 5b 5c 2

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

2 Root

slide-32
SLIDE 32

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3

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

3 2 Root

slide-33
SLIDE 33

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3 4

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

4 3 2 Root

slide-34
SLIDE 34

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3 4

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

3 2 Root

slide-35
SLIDE 35

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3 4 5

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

5 3 2 Root

slide-36
SLIDE 36

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3 4 5

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

3 2 Root

slide-37
SLIDE 37

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3 4 5

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

2 Root

slide-38
SLIDE 38

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3 4 5 6

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

6 2 Root

slide-39
SLIDE 39

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3 4 5 6

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

2 Root

slide-40
SLIDE 40

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3 4 5 6

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

Root

slide-41
SLIDE 41

Tree traversals

Preorder:

Root 1 5a 5b 5c 2 3 4 5 6

Recursively 1. visit Root 2. visit left 3. visit right Stack: (5c right of 5b!) empty! Done

slide-42
SLIDE 42

Tree traversals

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

slide-43
SLIDE 43

Tree traversals

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

slide-44
SLIDE 44

Tree traversals

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

slide-45
SLIDE 45

Tree traversals

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

slide-46
SLIDE 46

Tree traversals

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

slide-47
SLIDE 47

Tree traversals

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

slide-48
SLIDE 48

Tree traversals

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

slide-49
SLIDE 49

Tree traversals

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

slide-50
SLIDE 50

Tree traversals

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

slide-51
SLIDE 51

Tree traversals

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

slide-52
SLIDE 52

Tree traversals

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

slide-53
SLIDE 53

Tree traversals

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

slide-54
SLIDE 54

Tree traversals

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

slide-55
SLIDE 55

Tree traversals

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

slide-56
SLIDE 56

Tree traversals

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

slide-57
SLIDE 57

Tree traversals

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

slide-58
SLIDE 58

Tree traversals

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

slide-59
SLIDE 59

Tree traversals

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

slide-60
SLIDE 60

Tree traversals

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

slide-61
SLIDE 61

Tree traversals

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

slide-62
SLIDE 62

Tree traversals

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

slide-63
SLIDE 63

Tree traversals

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

slide-64
SLIDE 64

Tree traversals

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

slide-65
SLIDE 65

Tree traversals

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

slide-66
SLIDE 66

Tree traversals

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

  • empty. Done!
slide-67
SLIDE 67

Tree traversals

Postorder:

5a 5c (right of 5b) 5b 1 4 5 3 6 2 Root

Recursively 1. visit left 2. visit right 3. visit Root Stack: Exercise!

slide-68
SLIDE 68

DFS: the code

Preorder:

Root 1 5a 5b 5c 2 3 4 5 6

visit means “print” Inorder:

5a 1 5b 5c Root 4 3 5 2 6

Postorder:

5a 5c 5b 1 4 5 3 6 2 Root

implicit stack

slide-69
SLIDE 69

Tree traversals

slide-70
SLIDE 70

Tree traversals

Visit order

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue Root

slide-71
SLIDE 71

Tree traversals

Visit order Root

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue 1 , 2

slide-72
SLIDE 72

Tree traversals

Visit order Root 1

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue 2, 5a, 5b

slide-73
SLIDE 73

Tree traversals

Visit order Root 1 2

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue 5a, 5b, 3, 6

slide-74
SLIDE 74

Tree traversals

Visit order Root 1 2 5a

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue 5b, 3, 6

slide-75
SLIDE 75

Tree traversals

Visit order Root 1 2 5a 5b

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue 3, 6, 5c

slide-76
SLIDE 76

Tree traversals

Visit order Root 1 2 5a 5b 3

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue 6, 5c, 4, 5

slide-77
SLIDE 77

Tree traversals

Visit order Root 1 2 5a 5b 3 6

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue 5c, 4, 5

slide-78
SLIDE 78

Tree traversals

Visit order Root 1 2 5a 5b 3 6 5c

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue 4, 5

slide-79
SLIDE 79

Tree traversals

Visit order Root 1 2 5a 5b 3 6 5c 4

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue 5

slide-80
SLIDE 80

Tree traversals

Visit order Root 1 2 5a 5b 3 6 5c 4 5

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Queue

  • Empty. Done
slide-81
SLIDE 81

Tree traversals

Visit order Root 1 2 5a 5b 3 6 5c 4 5

  • 0. Add root to the queue Q

Recursively 1. get node from Q 2. visit the node 3. add all children to Q Level 1 1 2 2 2 2 3 3

3

slide-82
SLIDE 82

Tree traversals: BFS

BFS visit: Root 1 2 5a 5b 3 6 5c 4 5

slide-83
SLIDE 83

Tree traversals: complexity

slide-84
SLIDE 84

Generic trees

Generic Trees are like binary trees, but each node can have more than 2 children. One possible implementation is that each node (that is a subtree in itself) has a value, a link to its parent and a list of children. Another implementation is that each node has a value, a link to its parent, a link to its next sibling and a link to its first child.

slide-85
SLIDE 85

Generic trees

Exercise!

slide-86
SLIDE 86

Exercise

Root-Left-Right Left-Right-Root Left-Root-Right

slide-87
SLIDE 87

Exercise

where I is on the right of D and H is on the left of I

Preorder visit A E B F G C D I H Inorder visit B E G F C A D H I Postorder visit B G C F E H I D A

slide-88
SLIDE 88

Exercises

Width: 3 Minimal height: 2 k = 2 → output: 3

slide-89
SLIDE 89

Exercise: width

similar to BFS but we need to explicitly store the level... Min Height and nodes at level k are similar...