Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please turn the following algorithm into a C++ program. Thanks. USE NODE A as the ROOT. In addition to the default output similar to above,

Please turn the following algorithm into a C++ program. Thanks.

USE NODE A as the ROOT. In addition to the default output similar to above, determine and print the LENGTH AND SEQUENCE OF THE LONGEST BRANCH in the resulting tree. There are several methods to do this:

Method A: Increment a global counter in the first line of traverse, and decrement the global counter on the last line of traverse. Also keep a separate global stack, push when incrementing the counter, pop when decrementing the counter. Also keep track of the max counter value. If a new max is created, print the global stack. This method will print several stacks, each one longer than the previous one, so the last one printed will be the longest path in the tree.

Method B: You should be able to omit the global stack if you use the data already pushed onto the function all stack. You can implement this in C using pointers, you just need to (somehow) determine the size of the stack frame, then you can iterate up the stack.

Please use method A if possible because it can be done in c++. Method B works for me as well.

// A recursive solution for a stack traversal of a graph

/* Each instance of traverse keeps track of one nodes adjacency list. The CALL to and RETURN from traverse, are similar to PUSH and POP.

FYI: This code began its life in C#, mkay? */ 
// the graph adjacency matrix 
static int[,] graph = { 
{0,1,0,0,0,0,1,1,0,1,1,1,0,1,1}, //A 
{1,0,0,0,1,0,1,1,1,1,0,1,0,1,0}, //B 
{0,0,0,0,1,0,1,0,0,1,0,1,1,1,1}, //C 
{0,0,0,0,1,0,1,1,1,0,0,0,0,0,1}, //D 
{0,1,1,1,0,1,0,1,1,1,0,1,0,0,1}, //E 
{0,0,0,0,1,0,0,0,0,0,0,0,1,1,1}, //F 
{1,1,1,1,0,0,0,0,1,0,1,1,0,0,1}, //G 
{1,1,0,1,1,0,0,0,1,1,1,0,1,1,1}, //H 
{0,1,0,1,1,0,1,1,0,0,1,1,0,1,1}, //I 
{1,1,1,0,1,0,0,1,0,0,1,0,0,0,1}, //J 
{1,0,0,0,0,0,1,1,1,1,0,1,0,0,0}, //K 
{1,1,1,0,1,0,1,0,1,0,1,0,0,0,1}, //L 
{0,0,1,0,0,1,0,1,0,0,0,0,0,0,0}, //M 
{1,1,1,0,0,1,0,1,1,0,0,0,0,0,1}, //N 
{1,0,1,1,1,1,1,1,1,1,0,1,0,1,0}, //O 
//A B C D E F G H I J K L M N O 

};

// where I've been 

static bool[] visited = {false, false, false, false, false, false, false, false};

// the resulting tree. Each node's parent is stored

static int[] tree = {-1, -1, -1, -1, -1, -1, -1, -1}; 
static void Main() { 

// "Push" C

 traverse(2); 
 printtree(); } 
void traverse(node) { 
 mark node as visited print(node) 

init target to 0 // index 0 is node A while(not at end of nodes list) // count thru cols in nodes row

{

if(target is nodes neighbor and target is unvisited)

{

 mark that targets parent is node 
 traverse(target) // this is like a push } 

next target

}

 return // this is like a pop } 

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

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions

Question

Briefly explain the various types of leadership ?

Answered: 1 week ago

Question

Explain the need for and importance of co-ordination?

Answered: 1 week ago

Question

Explain the contribution of Peter F. Drucker to Management .

Answered: 1 week ago

Question

What is meant by organisational theory ?

Answered: 1 week ago

Question

=+what information would you need about the compact disc industry?

Answered: 1 week ago