tree traversal Oct. 25/26, 2017 1 2 Tree Traversal How to visit - - PowerPoint PPT Presentation

tree traversal
SMART_READER_LITE
LIVE PREVIEW

tree traversal Oct. 25/26, 2017 1 2 Tree Traversal How to visit - - PowerPoint PPT Presentation

COMP 250 Lecture 20 tree traversal Oct. 25/26, 2017 1 2 Tree Traversal How to visit (enumerate, iterate through, traverse ) all the nodes of a tree ? 3 depthfirst (root){ // preorder if (root is not empty){ visit root for each


slide-1
SLIDE 1

1

COMP 250

Lecture 20

tree traversal

  • Oct. 25/26, 2017
slide-2
SLIDE 2

2

slide-3
SLIDE 3

Tree Traversal

3

How to visit (enumerate, iterate through, traverse… ) all the nodes

  • f a tree ?
slide-4
SLIDE 4

depthfirst (root){ // “preorder” if (root is not empty){ visit root for each child of root depthfirst( child ) } }

1

4

slide-5
SLIDE 5

depthfirst (root){ // “preorder” if (root is not empty){ visit root for each child of root depthfirst( child ) } }

1

2 3

5

slide-6
SLIDE 6

depthfirst (root){ // “preorder” if (root is not empty){ visit root for each child of root depthfirst( child ) } }

1

2 3 4 5

6

slide-7
SLIDE 7

depthfirst (root){ // “preorder” if (root is not empty){ visit root for each child of root depthfirst( child ) } }

1

2 7 8 11 3 4 10 9 5 6

7

slide-8
SLIDE 8

Preorder Traversal

e.g. Printing a directory (visit = print)

8

Documents (directory) Music (directory) Eminem (directory) Lose Yourself (file) Raffi (directory) Shake My Sillies Out (file) Baby Beluga (file) Videos (directory) : (file) Work (directory) COMP250 (directory) : Research (directory) :

My Documents

Music Videos Work Raffi Eminem

COMP 250 Lose Yourself Baby Beluga Shake my sillies out

Research

slide-9
SLIDE 9

“Visit” implies that you do something at that node. Analogy: you aren’t visiting London UK if you just fly through Heathrow.

9

slide-10
SLIDE 10

depthfirst (root){ // “postorder” if (root is not empty){ for each child of root depthfirst( child ) visit root } }

10

Q: Which node is visited first?

slide-11
SLIDE 11

depthfirst (root){ // “postorder” if (root is not empty){ for each child of root depthfirst( child ) visit root } }

1

11

slide-12
SLIDE 12

depthfirst (root){ // “postorder” if (root is not empty){ for each child of root depthfirst( child ) visit root } }

1 2

12

slide-13
SLIDE 13

depthfirst (root){ // “postorder” if (root is not empty){ for each child of root depthfirst( child ) visit root } }

5 1 4 2 3

13

slide-14
SLIDE 14

depthfirst (root){ // “postorder” if (root is not empty){ for each child of root depthfirst( child ) visit root } }

11 5 6 10 9 1 4 8 7 2 3

14

slide-15
SLIDE 15

height(v){

if (v is a leaf) return 0 else{ h = 0 for each child w of v h = max(h, height(w)) return 1 + h } }

4

2 1 3 1 1 2 1

Example 1 postorder: recall last lecture

15

visit = return value of height

slide-16
SLIDE 16

Example 2 Postorder: What is the total number of bytes in all files in a directory?

16

slide-17
SLIDE 17

numBytes(root){ if root is a leaf return number of bytes at root else { sum = 0 for each child of root{ sum += numBytes(child) } return sum } }

By ‘visit’ here, we mean determining the number of bytes for a node, e.g. If we were to store ‘sum’ at the node.

17

slide-18
SLIDE 18

a

b g h k c d j i e f NOTE: Same call sequence occurs for preorder vs postorder. Letter order corresponds to depthfirst() call order

18

slide-19
SLIDE 19

a

b g h k c d j i e f

Call stack for depthfirst()

e f c d d d d d i j k b b b b b b b b b g h h h h h h h a a a a a a a a a a a a a a a a a a a a a

19

slide-20
SLIDE 20

a

b g h k c d j i e f

Call stack for depthfirst()

e f c d d d d d i j k b b b b b b b b b g h h h h h h h a a a a a a a a a a a a a a a a a a a a a

20

slide-21
SLIDE 21

a

b g h k c d j i e f

Call stack for depthfirst()

e f c d d d d d i j k b b b b b b b b b g h h h h h h h a a a a a a a a a a a a a a a a a a a a a

21

slide-22
SLIDE 22

a

b g h k c d j i e f

Call stack for depthfirst()

e f c d d d d d i j k b b b b b b b b b g h h h h h h h a a a a a a a a a a a a a a a a a a a a a

22

slide-23
SLIDE 23

Tree traversal

Recursive

  • depth first (pre- versus post-order)

Non-Recursive

  • using a stack
  • using a queue

23

slide-24
SLIDE 24

treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur // moving ‘visit cur’ to be for each child of cur // after for loop s.push(child) // changes nothing } }

24

slide-25
SLIDE 25

treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur } }

25

slide-26
SLIDE 26

treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

26

slide-27
SLIDE 27

27

What is the order of nodes visited ?

slide-28
SLIDE 28

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

28

slide-29
SLIDE 29

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

29

slide-30
SLIDE 30

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

30

slide-31
SLIDE 31

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

31

slide-32
SLIDE 32

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

32

slide-33
SLIDE 33

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

33

slide-34
SLIDE 34

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

34

slide-35
SLIDE 35

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

35

slide-36
SLIDE 36

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

36

slide-37
SLIDE 37

a

b g h k c d j i e f

k j j j h i i i i i f g g g g g g g g g d e e e a _ b b b b b b b b b b b _ c c c c c c c _ treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

37

slide-38
SLIDE 38

a

b g h k c d j i e f

recursive abcdefghijk non-recursive (stack) ahkjigbdfec

Stack based method is depth first, but visits children from right to left

38

slide-39
SLIDE 39

Pre- or post order?

39

treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) visit cur } } Moving the visit does not make it post order. Why not?

slide-40
SLIDE 40

treeTraversalUsingQueue(root){ initialize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for each child of cur q.enqueue(child) } } treeTraversalUsingStack(root){ initialize empty stack s s.push(root) while s is not empty { cur = s.pop() visit cur for each child of cur s.push(child) } }

What if we use a queue instead?

40

slide-41
SLIDE 41

a

b c d i e f h g j k

a

Queue state at start of the while loop

treeTraversalUsingQueue(root){ initialize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for each child of cur q.enqueue(child) } }

41

slide-42
SLIDE 42

a

b c d i e f h g j k

a b c d

Queue state at start of the while loop

treeTraversalUsingQueue(root){ initialize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for each child of cur q.enqueue(child) } }

42

slide-43
SLIDE 43

a

b c d i e f h g j k

a b c d c d e f

Queue state at start of the while loop

treeTraversalUsingQueue(root){ initialize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for each child of cur q.enqueue(child) } }

43

slide-44
SLIDE 44

a

b c d i e f h g j k

a b c d c d e f d e f

Queue state at start of the while loop

treeTraversalUsingQueue(root){ initialize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for each child of cur q.enqueue(child) } }

44

slide-45
SLIDE 45

a

b c d i e f h g j k

a b c d c d e f d e f e f g h i

Queue state at start of the while loop

treeTraversalUsingQueue(root){ initialize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for each child of cur q.enqueue(child) } }

45

slide-46
SLIDE 46

a

b c d i e f h g j k

a b c d c d e f d e f e f g h i f g h i g h i j k h i j k i j k j k k

treeTraversalUsingQueue(root){ initialize empty queue q q.enqueue(root) while q is not empty { cur = q.dequeue() visit cur for each child of cur q.enqueue(child) } }

46

slide-47
SLIDE 47

breadth first traversal

a

b c d i e f h g j k

for each level i visit all nodes at level i

  • rder visited: abcdefghijk

47

slide-48
SLIDE 48

Implementation Details Recall: ‘first child, next sibling’

class TreeNode<T>{ T element; TreeNode<T> firstChild; TreeNode<T> nextSibling; : : } class Tree<T>{ TreeNode<T> root; : : }

48

slide-49
SLIDE 49

49

for each child{ … } means: child = cur.firstChild while (child != null){ ….. child = child.nextsibling }