Purchase Solution

C++ Tree (Integers)

Not what you're looking for?

Ask Custom Question

Write a program containing a function that outputs the elements stored in the tree (integers) to the screen. The integers have to be output level by level in the order they appear in the tree.

For example, if the tree stores numbers 3 2 6 4 7 1 5 as shown below, then the tree will have the following form:

3
/
2 6
/ /
1 4 7

5

Your program should produce an output like this:

3
2 6
1 4 7
5

Purchase this Solution

Solution Summary

Using Java the solution writes a program containing a function that outputs the elements stored in the the tree (integers) to the screen. Attached is a Java file for more details.

Solution Preview

// TreeTest.cpp

#include <iostream>
#include <queue>
using namespace std;

// Define Tree class
class Tree {
public:
int value;
int level;
Tree *left;
Tree *right;
Tree(int); // Constructor
void insert(int); // Insert a node with value v
void traverseByLevel(); // Traverse the tree by levels
};

Tree::Tree(int v) {
value = v;
left = NULL;
right = NULL;
}

void ...

Purchase this Solution


Free BrainMass Quizzes
Inserting and deleting in a linked list

This quiz tests your understanding of how to insert and delete elements in a linked list. Understanding of the use of linked lists, and the related performance aspects, is an important fundamental skill of computer science data structures.

Java loops

This quiz checks your knowledge of for and while loops in Java. For and while loops are essential building blocks for all Java programs. Having a solid understanding of these constructs is critical for success in programming Java.

Word 2010: Table of Contents

Ever wondered where a Table of Contents in a Word document comes from? Maybe you need a refresher on the topic? This quiz will remind you of the keywords and options used when working with a T.O.C. in Word 2010.

Basic UNIX commands

Use this quiz to check your knowledge of a few common UNIX commands. The quiz covers some of the most essential UNIX commands and their basic usage. If you can pass this quiz then you are clearly on your way to becoming an effective UNIX command line user.

C++ Operators

This quiz tests a student's knowledge about C++ operators.