Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement deleteTree function which deletes all the nodes of the tree //tree.cpp #include #include tree.hpp using namespace std; #define COUNT 10 /* Constructor for a

Implement deleteTree function which deletes all the nodes of the tree

//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); }

/* *************************************************************************** ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Implement this function This function deletes all the nodes in the tree ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *************************************************************************** */ void Tree::deleteTree(Node *node) { if (node == NULL) return;

/*TODO first delete both subtrees */

/* TODO then delete the node */ cout<<" Deleting node:"<< node->data; }

/* *************************************************************************** ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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 deleteTree(Node *root); int sumNodes(Node *root); void preOrderTraverse(Node *root); void print2DUtil(Node *root, int space); };

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

//driver.cpp

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

int main() { Tree t; t.createTree(); //cout<data<

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

/********************** Gold - Find the sum of all the nodes in the tree ***********************/ cout << " Sum of all the nodes in tree is:"<

/********************** Silver - Delete all the nodes from the tree ***********************/ cout << " Deleting tree "; t.deleteTree(t.root); t.root=NULL; cout<

return 0; }

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

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

3rd Edition

0128012757, 978-0128012758

More Books

Students also viewed these Databases questions

Question

unit costs Verify the daim in footnote 3. AppendixLO1

Answered: 1 week ago