Question

The goal of this lab is to reinforce binary tree concepts. Specifically, this lab is to...

The goal of this lab is to reinforce binary tree concepts. Specifically, this lab is to do construct a function (and construct a test program using a non-trivial tree) which will display a binary tree by levels showing “vacant spots”. The display for the following tree should be:

M

H T

E null P W

A null null null null Q null null

The function header should be:

template <typename T>

void display_complete_tree (const binary_tree_node<T>* t)

The files "bintree.h" and "bintree.template" are to be used, which are linked here:

https://www.cs.colorado.edu/~main/chapter10/bintree.h

https://www.cs.colorado.edu/~main/chapter10/bintree.template

PLEASE do not use struct node, use the provided template and header and incorporate the function header into the problem. thanks

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Complete Test Program:



Sample Output:

CODE TO COPY:

File: main.cpp

// Header files section
#include "bintree.h"
#include <queue>
using namespace std;
using namespace main_savitch_10;

// function prototypes
binary_tree_node<char>* create_tree();
template<class T>
int getHeight(binary_tree_node<T>* node);
template<class T>
void display_complete_tree(binary_tree_node<T>* t);

// start main function
int main()
{
   binary_tree_node<char>* t = create_tree();
   display_complete_tree(t);
  
   return EXIT_SUCCESS;
} // end of main function

// create_tree function implementation
binary_tree_node<char>* create_tree()
{
   binary_tree_node<char>* t8 = new binary_tree_node<char>('Q');
   binary_tree_node<char>* t7 = new binary_tree_node<char>('A');  
   binary_tree_node<char>* t6 = new binary_tree_node<char>('W');
   binary_tree_node<char>* t5 = new binary_tree_node<char>('P', NULL, t8);  
   binary_tree_node<char>* t4 = new binary_tree_node<char>('E', t7, NULL);
   binary_tree_node<char>* t3 = new binary_tree_node<char>('T', t5, t6);
   binary_tree_node<char>* t2 = new binary_tree_node<char>('H', t4, NULL);
   binary_tree_node<char>* t1 = new binary_tree_node<char>('M', t2, t3);
  
   return t1;
} // end of create_tree function

// getHeight function implementation
template<class T>
int getHeight(binary_tree_node<T>* node)
{
   if (node == NULL)
   {
       return 0;
   }
   else
   {
       return (1 + max(getHeight(node->left()), getHeight(node->right())));
   }
} // end of getHeight function

// display_complete_tree function implementation
template<class T>
void display_complete_tree(binary_tree_node<T>* t)
{
   queue<binary_tree_node<T> *> q1;
   binary_tree_node<T> *current;

   bool isEmptyRow;

   q1.push(t);
   int height = getHeight(t);
   isEmptyRow = false;

   while (isEmptyRow == false)
   {
       queue<binary_tree_node<T> *> q2;
       isEmptyRow = true;

       while (q1.empty() == false)
       {
           current = q1.front();
           q1.pop();
           if (current != NULL)
           {
               cout << current->data() << " ";
               q2.push(current->left());
               q2.push(current->right());
               if (current->left() != NULL || current->right() != NULL)
                   isEmptyRow = false;
           }
           else
           {
               cout << "null ";
               q2.push(NULL);
               q2.push(NULL);
           }
       }

       cout << endl;

       while (q2.empty() == false)
       {
           q1.push(q2.front());
           q2.pop();
       }
   }
} // end of display_complete_tree function

Add a comment
Know the answer?
Add Answer to:
The goal of this lab is to reinforce binary tree concepts. Specifically, this lab is to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • The goal of this assignment is to reinforce the tree data structure in C++. Specifically, the...

    The goal of this assignment is to reinforce the tree data structure in C++. Specifically, the assignment is to do the following: Binary search trees have their best performance when they are balanced, which means that at each node, n, the height of the left subtree of n is within one of the height of the right subtree of n. Write a function (and a test program) which takes a sorted list of entries and produces a balanced binary search...

  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary...

    Consider the class specifications for the Binary Tree class and BinarySearch Tree class below: // Binary Tree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType *right; }; //Definition of class Binary Tree template <class elemType> class Binary Tree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); -Binary Tree(): bool is Empty() const; virtual bool search(const elemTypes searchItem) const = 0; virtual void insert(const elemTypek insertItem) =...

  • Consider the class specifications for the Binary Tree class and Binary Search Tree class in the...

    Consider the class specifications for the Binary Tree class and Binary Search Tree class in the attached files // BinaryTree.h #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct TreeNode { elemType data; TreeNode<elemType> *left; TreeNode<elemType> *right; }; //Definition of class Binary Tree template <class elemType> class BinaryTree { protected: TreeNode<elemType> *root; public: BinaryTree(); BinaryTreel const BinaryTree<elemType>& otherTree); BinaryTree(); bool is Empty() const; virtual boot search(const elemType& searchItem) const = 0; virtual void insert(const elemType& insertItem)...

  • Binary Tree Template Write your own version of a class template that will create a binary...

    Binary Tree Template Write your own version of a class template that will create a binary tree that can hold values of any data type. Demonstrate the class with a driver program. Place your binary tree template in it's own header file, Btree.h. Include methods for the following: inserting new values into the tree removing nodes from the tree searching the tree returning the number of nodes in the tree displaying the contents of the tree using preorder traversal Your...

  • /* * struct for a single node in a binary tree. data contains the int *...

    /* * struct for a single node in a binary tree. data contains the int * stored in this node. left and right contain pointers to the left and * right subtrees respectively. * * All of the ints stored in the left subtree is smaller than data. * All of the ints stored in the right subtree is larger than data. */ struct node { int data; struct node *left; struct node *right; }; typedef struct node node; Write...

  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing...

    Using C Please comment Part 1: BST Create a link based Binary Search tree composed of a Node and a Tree struct. You should have a header file, BST.h, with the following: o Node struct containing left, right, and parent pointers, in addition to holding an Data struct value Tree struct containing a pointer to the root of the tree A function declaration for a function that allocates a tree, and initializes the root to NULL o o o A...

  • The goal of this task is to reinforce the implementation of container class concepts using linked...

    The goal of this task is to reinforce the implementation of container class concepts using linked lists. Specifically, the task is to create an implementation file using a linked list. You need to use the header files, set3.h and node1.h, and the test program, test_set3.cpp. Your documentation must include the efficiency of each function. Please make your program as efficient and reusable as possible. set3.h #ifndef _SET_H #define _SET_H #include <cstdlib> #include <iostream> class set { public: typedef int value_type;...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT