Tree traversals
January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1
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
January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 1
– If you borrowed a Macbook charger in Monday lab section 14:00- 16:00, please return it! See Piazza for details
– Monday, Feb.04, 19:00 – 21:00, WESB 100
January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 2
– 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
– Typically, it will do something while visiting each node!
– inOrder – preOrder – postOrder
Cinda Heeren / Will Evans / Geoffrey Tien 4
It's recursive!
January 30, 2019
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
node).
January 30, 2019
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
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
– 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!
– 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?
– 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
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?
– Chapter 15.1 – 15.2 (Trees, tree traversals)
– Carrano & Henry: Chapter 15.2, 16.1 – 16.2 (Tree traversals, implementations)
January 30, 2019 Cinda Heeren / Will Evans / Geoffrey Tien 11