Complete the TODOs in C++ 1. Implement sumNodes function which sums the data of all the nodes of the tree. (Silver Problem) 2. Implement printLeafNode function which prints just the leaf nodes of the tree. (Gold Problem) (.hpp file in image) 1st code is driver.cpp #include #include "tree.hpp" using namespace std; #define COUNT 10 int main() { Treet; t.createTree(); //cout<data< #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("\n"); for (inti = COUNT; i < space; i++) printf(" "); printf("%d\n", root->data); // Process left child print2DUtil(root->left, space); } void Tree::preOrderTraverse(Node *node) { if (node == NULL) return; /* first print data of node */ cout<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<<"\n Deleting node:"<data; deletenode; 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 return0; } /* Creates a tree of 7 elements */ void Tree::createTree() { Node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); this->root = root; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
icon
Concept explainers
Question

Complete the TODOs in C++

1. Implement sumNodes function which sums the data of all the nodes of the tree. (Silver Problem)
2. Implement printLeafNode function which prints just the leaf nodes of the
tree. (Gold Problem)

(.hpp file in image)

1st code is driver.cpp

#include <iostream>
#include "tree.hpp"
using namespace std;
#define COUNT 10


int main()
{
Treet;
t.createTree();
//cout<<t.root->data<<endl;
t.print2DUtil(t.root, 0);


// 1, 2, 4, 5, 3, 6, 7
cout<<"\nPreorder traversal of binary tree is \n";
t.preOrderTraverse(t.root);
cout<<endl;

/**********************
Silver problem
***********************/
cout<<"\nSum of all the nodes in tree is: "<<t.sumNodes(t.root);
cout<<endl;

/**********************
Gold problem
***********************/
cout<<"\nPrinting leaf nodes of the tree \n";
t.printLeafNode(t.root);

cout<<"\nDeleting the tree \n";
t.deleteTree(t.root);
t.root=NULL;
cout<<endl;

return0;
}
 
2nd code: tree.cpp:
#include <iostream>
#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("\n");
for (inti = COUNT; i < space; i++)
printf(" ");
printf("%d\n", 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<<"\n Deleting node:"<<node->data;
deletenode;
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

return0;
}

/*
Creates a tree of 7 elements
*/
void Tree::createTree()
{
Node *root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->left = newNode(6);
root->right->right = newNode(7);
this->root = root;
}
EXPLORER
G+ tree.hpp X
V OPEN EDITORS
G- tree.hpp > .
X &+ tree.hpp
1
#include <iostream>
2
using namespace std;
v RECITATION 7 EXERCISE FILES
3
#define COUNT 10
G• driver.cpp
G+ tree.cpp
/*
G+ tree.hpp
6
Each node in the tree has this structure
7
*/
8.
struct Node
9
{
10
int data;
11
Node *left;
12
Node *right;
13
Node(int data)
14
{
15
this->data =
data;
16
this->left =
this->right = NULL;
17
}
18
};
19
20
21
/*
22
Variables and functions for Tree ADT
23
*/
24
class Tree
25
{
26
27
public:
28
Node *root;
29
Tree();
void createTree();
void printLeafNode(Node *root);
30
31
32
void deleteTree(Node *root);
int sumNodes (Node *root);
void preOrderTraverse(Node *root);
void print2DUtil(Node *root, int space);
33
34
35
36
};
37
Transcribed Image Text:EXPLORER G+ tree.hpp X V OPEN EDITORS G- tree.hpp > . X &+ tree.hpp 1 #include <iostream> 2 using namespace std; v RECITATION 7 EXERCISE FILES 3 #define COUNT 10 G• driver.cpp G+ tree.cpp /* G+ tree.hpp 6 Each node in the tree has this structure 7 */ 8. struct Node 9 { 10 int data; 11 Node *left; 12 Node *right; 13 Node(int data) 14 { 15 this->data = data; 16 this->left = this->right = NULL; 17 } 18 }; 19 20 21 /* 22 Variables and functions for Tree ADT 23 */ 24 class Tree 25 { 26 27 public: 28 Node *root; 29 Tree(); void createTree(); void printLeafNode(Node *root); 30 31 32 void deleteTree(Node *root); int sumNodes (Node *root); void preOrderTraverse(Node *root); void print2DUtil(Node *root, int space); 33 34 35 36 }; 37
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Depth First Search
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education