Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ Breadth-First Traversal The C++ code below is a (almost complete) implementation of breadth-first traversal of a binary tree structure. The Queue data structure makes

image text in transcribedimage text in transcribedimage text in transcribedC++ Breadth-First Traversal

The C++ code below is a (almost complete) implementation of "breadth-first traversal" of a binary tree structure. The Queue data structure makes it very easy to code this type of stepping through the nodes of a graph. The binary tree that has been "hard-coded" into the program (see below) looks like this: 55 10 The int main() shown below is to traverse this graph "breadth-first" and prints out the value of each visited node. The output for this example will be: 55 46 10 35 19 101 12 The Algorithm: Initialize a queue with the root node. Then while the queue is not empty, print the value of the front node in the queue, remove that node from the queue and add to the back of the queue the nodes that are to the left (if there is) and right (if there is) of the node whose value was just printed. (For some nodes there are no nodes to their left or right, so nothing gets added to thequeue.) Fill in the missing pieces of code to complete this implementation of breadth-first tree traversal. template class NODE public: NODE() :left(nullptr), right(nullptr) {} NODE(T ) value(x), left(nullptr), right(nullptr) {} NODE(T X, NODE* lft, NODE* rgt) : value(x), left(lft), right(rgt) {} I value; NODE* left; NODE* right; int main() NODE n35(35); NODE int> n19(19); NODE n101(101); NODE n12(12); NODE n46(46, &n35, &n19); NODE n10(10, &n101, &n12); NODE root(55, &n46, &n10); -> theq; Queue next = theq. front(); [3] cout (b) List *> (c) List> (d) NODE Fill [2] with: (a) theq.empty() (b) Itheq.empty() Fill [3] with: (a) theq.erase(next); (b) theq.dequeue(); (c) theq.enqueue(); (d) theq.pop_back(); Fill (4) with: (a) *(next.left)); (b) next (c) *left (d) next.left Fill (5) with: (write out)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Entity Alignment Concepts Recent Advances And Novel Approaches

Authors: Xiang Zhao ,Weixin Zeng ,Jiuyang Tang

1st Edition

9819942527, 978-9819942527

More Books

Students also viewed these Databases questions