Tree traversals January 30, 2019 Cinda Heeren / Will Evans / - - PowerPoint PPT Presentation

tree traversals
SMART_READER_LITE
LIVE PREVIEW

Tree traversals January 30, 2019 Cinda Heeren / Will Evans / - - PowerPoint PPT Presentation

Tree traversals January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1 Announcements Attention: If you borrowed a Macbook charger in Monday lab section 14:00- 16:00, please return it! See Piazza for details Section 201 (that's


slide-1
SLIDE 1

Tree traversals

January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1

slide-2
SLIDE 2

Announcements

  • Attention:

– If you borrowed a Macbook charger in Monday lab section 14:00- 16:00, please return it! See Piazza for details

  • Section 201 (that's us!) Midterm 1:

– Monday, Feb.04, 19:00 – 21:00, WESB 100

January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 2

slide-3
SLIDE 3

Complete binary trees

  • A binary tree is complete if

– The leaves are on at most two different levels, – The second to bottom level is completely filled in and – The leaves on the bottom level are as far to the left as possible

Cinda Heeren / Will Evans / Geoffrey Tien 3

It's not quite perfect, but almost

A C F B D E

January 30, 2019

slide-4
SLIDE 4

Binary tree traversal

  • A traversal algorithm for a binary tree visits each node in the

tree

– Typically, it will do something while visiting each node!

  • Traversal algorithms are naturally recursive
  • There are three traversal methods

– inOrder – preOrder – postOrder

Cinda Heeren / Will Evans / Geoffrey Tien 4

It's recursive!

January 30, 2019

slide-5
SLIDE 5

inOrder traversal algorithm

Cinda Heeren / Will Evans / Geoffrey Tien 5

void inOrder(Node* nd) { if (nd != nullptr) { inOrder(nd->leftchild); visit(nd); inOrder(nd->rightchild); } }

The visit function would do whatever the purpose

  • f the traversal is (e.g. print the data value of the

node).

January 30, 2019

slide-6
SLIDE 6

preOrder traversal

Cinda Heeren / Will Evans / Geoffrey Tien 6

visit(nd);

1 2 3 4 5 6 7 8

preOrder(nd->leftchild); preOrder(nd->rightchild);

visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right) visit preOrder(left) preOrder(right)

17 13 9 11 16 27 20 39

January 30, 2019

slide-7
SLIDE 7

postOrder traversal

Cinda Heeren / Will Evans / Geoffrey Tien 7

visit(nd);

8 4 2 1 3 7 5 6

postOrder(nd->leftchild); postOrder(nd->rightchild);

visit postOrder(left) postOrder(right)

17 13 9 11 16 27 20 39

visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right) visit postOrder(left) postOrder(right)

January 30, 2019

slide-8
SLIDE 8

Exercise

  • What will be printed by an in-order traversal of the tree?

– preOrder? postOrder?

January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 8

visit(nd);

17 9 27 6 16 20 31 12

inOrder(nd->leftchild); inOrder(nd->rightchild);

39 Note to Geoff: show the trick with the dots!

slide-9
SLIDE 9

Not quite recursive

  • We have seen pre-order, in-order, post-order traversals
  • What about a traversal that visits every node in a level before

working on the next level?

– level-order traversal

January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 9

Another type of tree traversal

41 33 87 21 74 36 45 78 25 Use some ADT to support this?

slide-10
SLIDE 10

What is "visiting"?

  • Some operation to be done at the current node

– counting, or arithmetic – creating a node (e.g. for a copy constructor) – deleting a node (e.g. for a destructor)

January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 10

  • e.g. height

What is the height of this tree? What is the height of this tree? What is the height of this tree?

int Height(Node* nd) { if (nd == nullptr) // empty tree return _____; else return _______________________; }

Which type of traversal is this? Running time?

slide-11
SLIDE 11

Readings for this lesson

  • Carrano & Henry:

– Chapter 15.1 – 15.2 (Trees, tree traversals)

  • Next class:

– Carrano & Henry: Chapter 15.2, 16.1 – 16.2 (Tree traversals, implementations)

January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 11