Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Complete source.cpp source.cpp #include #include #include Node.h using namespace std; //This method creates the tree which is presented in the slide. I did the hard

Complete source.cpp

source.cpp

#include  #include  #include "Node.h" using namespace std; //This method creates the tree which is presented in the slide. I did the hard work :) Node* create_tree_from_slides() { Node* root = new Node(6); root->left = new Node(2); root->right = new Node(12); root->left->left = new Node(-1); root->left->right = new Node(4); root->right->left = new Node(9); root->right->right = new Node(16); root->left->left->left = new Node(-3); root->left->left->right = new Node(1); root->left->right->left = new Node(3); root->left->right->right = new Node(5); root->right->left->left = new Node(8); root->right->left->right = new Node(11); root->right->right->left = new Node(13); root->right->right->right = new Node(20); return root; } /* Given a tree and a sum, return true if there is a path from the root down to a leaf, such that adding up all the values along the path equals the given sum. */ bool hasPathSum(Node* node, int sum) { //**************TO DO **************************** } /* Change a tree so that the roles of the left and right pointers are swapped at every node. So the tree... 4 / \ 2 5 / \ 1 3 is changed to... 4 / \ 5 2 / \ 3 1 Hint: Go all the way down (recursively) and start swaping right and left child. */ void mirror( Node* node) { //**************TO DO **************************** } void print_all(Node* root) { // Base case if (root == nullptr) return; // Recursive case print_all(root->left); cout << root->key << ' '; print_all(root->right); } int main() { Node* root = create_tree_from_slides(); if (hasPathSum(root, 15)) cout << "Well Done!" << endl; else cout << "Something is wrong with your algorithm. THere is a path sum up to 15." << endl; if (!hasPathSum(root, 18)) cout << "Well Done!" << endl; else cout << "Something is wrong with your algorithm. There is no path that sum up to 18." << endl; print_all(root); cout << endl; cout << " Mirror Tree (The order should be opposite) " << endl; mirror(root); print_all(root); cout << endl; system("pause"); return 0; }

node.h

#ifndef NODE_H #define NODE_H class Node { public: // We actually implement the constructor here, // in the header file (it's too little to "earn" a .cpp) Node(int n) { this->key = n; left = right = nullptr; } int key; Node* left; Node* right; }; #endif 

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_2

Step: 3

blur-text-image_3

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

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago