Download presentation

Presentation is loading. Please wait.

Published byFlora Butler Modified over 2 years ago

1
1 Breadth First Traversal

2
2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical) orientation. Root at the top. Leaves at the bottom.

3
3 Getting Started Download http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_03_09_BST_Display/ http://www.cse.usf.edu/~turnerr/Data_Structures/Downloads/ 2011_03_09_BST_Display/ File BST_Demo_2.zip Expand Build and run

4
4 Program in Action

5
5 Breadth First Traversal Let's add a Breadth First Traversal Textbook, page 226 Top down, left to right traversal Simple to implement with a queue.

6
6 Breadth First Traversal Start by putting root node into the queue. While queue is not empty: Dequeue first element. Visit that node. Add its children to the queue

7
7 Implementing the Queue Drozdek uses the Queue template from the Standard Template Library. Adds a thin wrapper to provide conventional names for methods: Enqueue Dequeue Rename the template file genBST3.h

8
8 genBST3.h #pragma once; #include using namespace std; template class Queue : public queue { public: T dequeue() { T tmp = front(); queue ::pop(); return tmp; } void enqueue(const T& el) { push(el); } };

9
9 Adding Breadth First Traversal Add to public section of class BST template: void breadthFirst();

10
10 Adding Breadth First Traversal template void BST ::breadthFirst() { Queue *> queue; BSTNode *p = root; if (p != 0) { queue.enqueue(p); while (!queue.empty()) { p = queue.dequeue(); visit(p); if (p->left != 0) { queue.enqueue(p->left); } if (p->right != 0) { queue.enqueue(p->right); }

11
11 Using the Breadth First Traversal Add at end of main(): cout << endl << endl << "Breadth First traversal: " << endl; my_BST.breadthFirst(); cout << endl; Comment out other traversals. Update the #include for genBST

12
12 Breadth First Traversal

Similar presentations

OK

Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.

Chapter 12 C Data Structures Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on bluetooth download Games we play ppt on website Ppt on density based traffic light control Ppt on product advertising cartoons Ppt on oxygen cycle for class 9 Chapter 1 introduction to human anatomy and physiology ppt on cells Ppt on voice recognition system using matlab Ppt on water our lifeline Ppt on cardiac arrest Ppt on economic growth and development in india