C++ How to Program (Early Objects Version)
10th Edition
ISBN: 9780134448824
Author: Paul Deitel; Harvey M. Deitel
Publisher: Pearson Education (US)
expand_more
expand_more
format_list_bulleted
Question
Chapter 19, Problem 19.19E
Program Plan Intro
Number of Levels that a Binary Tree Has
Program Plan:
1. Construct a tree with a root node, and 4 leaf nodes
2. Find the depth of the left subtree in a recursive manner if the tree is not null
3. Find the depth of the right subtree in a recursive manner if the tree is not null
4. Determine the maximum value of the depths of both subtrees
5. Add 1 to the resulting value for the current node
6. Return depth of the root node to get the number of levels in the given binary tree.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
Don't reject
Language/Type:
C++ binary trees pointers recursion
Write a function named hasPath that interacts with a tree of BinaryTreeNode structures representing an unordered binary tree. The function accepts three parameters: a pointer to the root of the tree, and two integers start and end, and returns true if a path can be found in the tree from start down to end. In other words, both start and end must be element data values that are found in the tree, and end must be below start, in one of start's subtrees; otherwise the function returns false. If start and end are the same, you are simply checking whether a single node exists in the tree with that data value. If the tree is empty, your function should return false.
For example, suppose a BinaryTreeNode pointer named tree points to the root of a tree storing the following elements. The table below shows the results of several various calls to your function:
67
88
52
1
21
16
99
45
Call
Result
Reason
hasPath(tree,
67, 99)
true
path exists…
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 thetree. (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:…
Chapter 19 Solutions
C++ How to Program (Early Objects Version)
Ch. 19 - Prob. 19.6ECh. 19 - Prob. 19.7ECh. 19 - Prob. 19.8ECh. 19 - Prob. 19.9ECh. 19 - Prob. 19.10ECh. 19 - Prob. 19.11ECh. 19 - (Infix-to-Post fix conversion) Stacks are used by...Ch. 19 - Prob. 19.13ECh. 19 - Prob. 19.14ECh. 19 - Prob. 19.15E
Ch. 19 - Prob. 19.16ECh. 19 - Prob. 19.17ECh. 19 - (Duplicate Elimination) In this chapter, we saw...Ch. 19 - Prob. 19.19ECh. 19 - Prob. 19.20ECh. 19 - Prob. 19.21ECh. 19 - Prob. 19.22ECh. 19 - Prob. 19.23ECh. 19 - Prob. 19.24ECh. 19 - Prob. 19.25ECh. 19 - Prob. 19.26ECh. 19 - Prob. 19.27ECh. 19 - Prob. 19.28E
Knowledge Booster
Similar questions
- c) An expression of A + B^3 – C/D is obtained using a rooted tree. i. Draw a rooted tree with the height of 2 to represent the postorder traversal. ii. Justify whether the rooted tree in 3-c(i) is balanced or not. iii. From tree in c(i), get the mathematical expression using the inorder traversal. d) Figure 3 represents a network of paths in a park. The number on each edge represents the length of the path in meters. The cost per meter is RM120. To gain as much profit, the contractor asked one of his staff to find the minimum network needed using Kruskal's algorithm. G 21 16 17 21 F 11 23 E 11 7 15 18 В 11 20 A Figure 3 i. Explain why the staff's work which is highlighted in red is incorrect. ii. Help the staff to find the correct minimum network using Kruskal's algorithm and states its length and total cost. iii. Is there any possibiliy, more than one distint MST obtained for the Figure 3?. If yes, justify your answer and show the network.arrow_forwardquick Solution pleasearrow_forwardexplain what is happening in each function.arrow_forward
- 1.) Design a Binary Search Tree ADT by using following functions. Include the function definitions & run the program (Kindly include header files as well). Paste the output as a screenshot and write the code in your answer sheet. insert(int val)find(int x)************************************************************************************ class btNode { public: int info; btNode *lLink; btNode *rLink; btNode(int e, btNode *l = NULL, btNode *r = NULL) { info = e; lLink = l; rLink = r; } btNode() { lLink = NULL; rLink = NULL; } }; class binarySTADT { private: btNode *root; int count = 0; public: binarySTADT() { root = NULL; } void insert(int val); // function to insert a given value in the tree.…arrow_forwardC in pythonarrow_forward(1 of 2 parts) Suppose you have a binary search tree with the following in-order traversal: ACEFHIJKMSW Then suppose we deleted a node with two children from that tree. The post- order traversal of the resulting tree is: AEHFCKW SMJ what is the level order traversal of the original treearrow_forward
- Which of the following statements are correct for the expression trees? (Select all that applies.) a. An expression tree is a binary tree. b. The root and internal nodes are operators. c. Each leaf is an operand. d. Subtrees are sub-expressions, with the root being an operator. 2. The height of a binary tree is the maximum number of edges in any root to leaf path. The maximum number of nodes in a binary tree of height h is: a. (2^h)-1 b. (2^(h+1))-1 c. 2*(h+1)-1 3. Which of the following statements are correct? (Select all that applies.) a. The maximum number of internal and external nodes in a proper binary tree with n nodes are (n-1)/2 and (n+1)/2, respectively. b. The maximum number of nodes at level k (k = 0,1,2,...) of a proper binary tree: 2^k c. Depth of a node refers to the number of ancestors. d. Height of a tree refers to the the maximum of the depths of its leaf positions.arrow_forward(Java) How can I implement a binary search tree in a program to store data, and use the delete method to trim the tree? The program should have a 'populate' button that obtains a string from the user, creates a sorted binary search tree of characters, and prints the tree using the DisplayTree class. It should also print the characters on one line in sorted order by traversing the tree in inorder fashion. The program should also have a 'Trim Tree' button that obtains a second line of input to delete characters from the tree, trimming the tree accordingly. It should ignore characters that are not in the tree, and only delete one character for each occurrence in the second line of input. When all characters from the second line have been deleted from the tree, the program should print the remaining characters in the tree using the DisplayTree class. The output should be labeled appropriately, and no spaces or commas should be used between tree elements in the inorder traversal.Here is the…arrow_forwardC using pythonarrow_forward
- Instructions. c++ binary search trees. 1. Implement a function that will build a binary search tree. 2. Using the function above create a binary search tree. 3. Implement a swapSubtrees() function that will take a pointer to a root node and a target value. Function will search for the target value in the tree and swap all left nodes with right nodes for all descendant nodes. 4. Pass the tree created in step 2 to test swapSubtrees() function. 5. You can use the inorder, postorder or preorder traversal implemented in class to display and confirm the logic. (trace it by hand as well) TreeNode{ int val; TreeNode* left = nullptr; TreeNode* right = nullptr; TreeNode(){} TreeNode(int val): val(val){} };arrow_forward[8] Instructions: Kindly provide the complete and correct solutions. I won't like it if it is incomplete and incorrect.arrow_forward*Please using JAVA only* Objective Program 3: Binary Search Tree Program The primary objective of this program is to learn to implement binary search trees and to combine their functionalities with linked lists. Program Description In a multiplayer game, players' avatars are placed in a large game scene, and each avatar has its information in the game. Write a program to manage players' information in a multiplayer game using a Binary Search (BS) tree for a multiplayer game. A node in the BS tree represents each player. Each player should have an ID number, avatar name, and stamina level. The players will be arranged in the BS tree based on their ID numbers. If there is only one player in the game scene, it is represented by one node (root) in the tree. Once another player enters the game scene, a new node will be created and inserted in the BS tree based on the player ID number. Players during the gameplay will receive hits that reduce their stamina. If the players lose…arrow_forward
arrow_back_ios
SEE MORE QUESTIONS
arrow_forward_ios
Recommended textbooks for you
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education
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)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education