Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

(I need help with this code. The silver problem is what I need help on, so no need to do the gold problem, and I

(I need help with this code. The silver problem is what I need help on, so no need to do the gold problem, and I can't get it to work. I could really appreciate some help.)

driver.cpp:

#include

#include "tree.hpp"

using namespace std;

#define COUNT 10

int main()

{

Tree t;

t.createTree();

//cout

t.print2DUtil(t.root, 0);

// 1, 2, 4, 5, 3, 6, 7

cout << " Preorder traversal of binary tree is ";

t.preOrderTraverse(t.root);

cout<

/**********************

Silver problem

***********************/

cout << " Sum of all the nodes in tree is: "<

cout<

/**********************

Gold problem

***********************/

cout << " Printing leaf nodes of the tree ";

t.printLeafNode(t.root);

cout << " Deleting the tree ";

t.deleteTree(t.root);

t.root=NULL;

cout<

return 0;

}

tree.cpp:

#include

#include "tree.hpp"

using namespace std;

#define COUNT 10

/*

Constructor for a Tree object

*/

Tree::Tree()

{

this->root = NULL;

}

/*

Prints a binary tree in a 2D fashion.

Note: The image of the tree is left rotated by 90 degrees.

*/

void Tree::print2DUtil(Node *root, int space)

{

// Base case

if (root == NULL)

return;

// Increase distance between levels

space += COUNT;

// Process right child first

print2DUtil(root->right, space);

// Print current node after space

// count

printf(" ");

for (int i = COUNT; i < space; i++)

printf(" ");

printf("%d ", root->data);

// Process left child

print2DUtil(root->left, space);

}

void Tree::preOrderTraverse(Node *node)

{

if (node == NULL)

return;

/* first print data of node */

cout << node->data << " ";

/* then recur on left sutree */

preOrderTraverse(node->left);

/* now recur on right subtree */

preOrderTraverse(node->right);

}

void Tree::deleteTree(Node *node)

{

if (node == NULL) return;

/* first delete both subtrees */

deleteTree(node->left);

deleteTree(node->right);

/* then delete the node */

cout<<" Deleting node:"<< node->data;

delete node;

node = NULL;

}

/*

***************************************************************************

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Gold problem

Implement this function

This function prints all the leaf nodes of the tree

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

***************************************************************************

*/

void Tree::printLeafNode(Node *node)

{

// TODO print the leaf node of the tree

}

/*

***************************************************************************

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Silver problem

Implement this function

This function gives the sum of all the nodes in the tree.

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

***************************************************************************

*/

int Tree::sumNodes(Node *node)

{

//TODO Base case

//TODO Implement Sum of all nodes

return 0;

}

/*

Creates a tree of 7 elements

*/

void Tree::createTree()

{

Node *root = new Node(1);

root->left = new Node(2);

root->right = new Node(3);

root->left->left = new Node(4);

root->left->right = new Node(5);

root->right->left = new Node(6);

root->right->right = new Node(7);

this->root = root;

}

tree.hpp:

#include

using namespace std;

#define COUNT 10

/*

Each node in the tree has this structure

*/

struct Node

{

int data;

Node *left;

Node *right;

Node(int data)

{

this->data = data;

this->left = this->right = NULL;

}

};

/*

Variables and functions for Tree ADT

*/

class Tree

{

public:

Node *root;

Tree();

void createTree();

void printLeafNode(Node *root);

void deleteTree(Node *root);

int sumNodes(Node *root);

void preOrderTraverse(Node *root);

void print2DUtil(Node *root, int space);

};

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 Application Development And Administration

Authors: Mannino Michael

5th Edition

0983332401, 978-0983332404

More Books

Students also viewed these Databases questions

Question

How many Tables Will Base HCMSs typically have? Why?

Answered: 1 week ago

Question

What is the process of normalization?

Answered: 1 week ago