1 breadth first traversal. 2 objectives you will be able to do a breadth first traversal of a binary...
TRANSCRIPT
![Page 1: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/1.jpg)
1
Breadth First Traversal
![Page 2: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/2.jpg)
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.
![Page 3: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/3.jpg)
3
Getting Started
Download http://www.cse.usf.edu/~turnerr/Data_Structures/Down
loads/2011_03_09_BST_Display/ File BST_Demo_2.zip
Expand Build and run
![Page 4: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/4.jpg)
4
Program in Action
![Page 5: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/5.jpg)
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.
![Page 6: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/6.jpg)
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
![Page 7: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/7.jpg)
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
![Page 8: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/8.jpg)
8
genBST3.h
#pragma once;
#include <iostream>
#include <iomanip>
#include <queue>
using namespace std;
template<class T>
class Queue : public queue<T>
{
public:
T dequeue()
{
T tmp = front();
queue<T>::pop();
return tmp;
}
void enqueue(const T& el)
{
push(el);
}
};
![Page 9: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/9.jpg)
9
Adding Breadth First Traversal
Add to public section of class BST template:
void breadthFirst();
![Page 10: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/10.jpg)
10
Adding Breadth First Traversaltemplate<class T>
void BST<T>::breadthFirst()
{
Queue<BSTNode<T>*> queue;
BSTNode<T> *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);
}
}
}
}
![Page 11: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/11.jpg)
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
![Page 12: 1 Breadth First Traversal. 2 Objectives You will be able to Do a breadth first traversal of a binary tree. Display a binary tree in its normal (vertical)](https://reader031.vdocuments.net/reader031/viewer/2022020800/56649ccf5503460f9499b502/html5/thumbnails/12.jpg)
12
Breadth First Traversal