trees
play

Trees Terminology (continued) Traversals February 10, 2020 Cinda - PowerPoint PPT Presentation

Trees Terminology (continued) Traversals February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1 Measuring trees The height of a node v is the length of the longest path from v to a leaf The height of the tree is the height of


  1. Trees Terminology (continued) Traversals February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 1

  2. Measuring trees • The height of a node v is the length of the longest path from v to a leaf – The height of the tree is the height of the root • The depth of a node v is the length of the path from v to the root – This is also referred to as the level of a node • Note that there is a slightly different formulation of the height of a tree – Where the height of a tree is said to be the number of different levels of nodes in the tree (including the root) February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 2

  3. Tree measurements explained A Height of tree is 3 B C Level 1 Height of node B is 2 Level 2 D E F G Depth of node E is 2 H I J Level 3 February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 3

  4. Perfect binary trees • A binary tree is perfect , if – No node has only one child – And all the leaves have the same depth • A perfect binary tree of height h has – 2 h +1 – 1 nodes, of which 2 h are leaves • Perfect trees are also complete February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 4

  5. Height of a perfect tree • Each level doubles the number of nodes – Level 1 has 2 nodes ( 2 1 ) – Level 2 has 4 nodes ( 2 2 ) or 2 times the number in Level 1 • Therefore a tree with ℎ levels has 2 ℎ+1 − 1 nodes – The root level has 1 node Bottom level has 2 ℎ 01 nodes, i.e. just over half of the nodes are leaves 12 11 22 24 21 23 31 32 33 34 35 36 37 38 February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 5

  6. 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 February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 6

  7. 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 February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 7

  8. 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). February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 8

  9. 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) February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 9

  10. 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 February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 10

  11. 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! February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 11

  12. 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? February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 12

  13. 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? February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 13

  14. 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) February 10, 2020 Cinda Heeren / Andy Roth / Geoffrey Tien 14

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