tree traversals
play

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


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

  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

  3. Complete binary trees It's not quite perfect, but almost • A binary tree is complete if – The leaves are on at most two A different levels, – The second to bottom level is B C completely filled in and – The leaves on the bottom level are as far to the left as possible D E F January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 3

  4. Binary tree traversal It's recursive! • 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 January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 4

  5. inOrder traversal algorithm void inOrder(Node* nd) { if (nd != nullptr) { inOrder(nd->leftchild); visit(nd); inOrder(nd->rightchild); } } The visit function would do whatever the purpose of the traversal is (e.g. print the data value of the node). January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 5

  6. preOrder traversal visit(nd); preOrder(nd->leftchild); 1 preOrder(nd->rightchild); 17 2 6 visit visit 13 27 preOrder(left) preOrder(left) preOrder(right) preOrder(right) 8 5 7 3 visit visit visit 16 20 39 9 preOrder(left) preOrder(left) preOrder(left) preOrder(right) preOrder(right) preOrder(right) visit preOrder(left) 4 preOrder(right) 11 visit preOrder(left) preOrder(right) January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 6

  7. postOrder traversal postOrder(nd->leftchild); postOrder(nd->rightchild); 8 visit(nd); 17 4 7 postOrder(left) postOrder(left) postOrder(right) 13 27 postOrder(right) visit visit 6 3 5 2 postOrder(left) postOrder(left) postOrder(left) 16 20 postOrder(right) 39 postOrder(right) 9 postOrder(right) visit visit visit postOrder(left) postOrder(right) 1 visit postOrder(left) 11 postOrder(right) visit January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 7

  8. Exercise • What will be printed by an in-order traversal of the tree? – preOrder? postOrder? inOrder(nd->leftchild); 17 visit(nd); inOrder(nd->rightchild); 9 27 6 16 20 31 12 39 Note to Geoff: show the trick with the dots! January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 8

  9. Not quite recursive Another type of tree traversal • 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 41 33 87 21 74 36 45 78 25 Use some ADT to support this? January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 9

  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) What is the height of • e.g. height this tree? What is the height of What is the this tree? height of this tree? int Height(Node* nd) { if (nd == nullptr) // empty tree return _____; else return _______________________; } Which type of traversal is this? Running time? January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 10

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend