Starting Out with C++: Early Objects (9th Edition)
9th Edition
ISBN: 9780134400242
Author: Tony Gaddis, Judy Walters, Godfrey Muganda
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 19, Problem 3PC
Program Plan Intro
Leaf Counter
Program Plan:
Main.cpp:
- Include required header files.
- Inside the “main ()” function,
- Display the number of leaf nodes by calling the function “num_LeafNodes ()”.
- Insert nodes into the binary tree by using the function “insert_Node ()”.
- Display those nodes by using the function “display_InOrder ()”.
- Now, display the number of leaf nodes by calling the function “num_LeafNodes ()”.
- Delete two nodes from the binary tree by using the function “remove ()”.
- Display remaining nodes by using the function “display_InOrder ()”.
- Finally, display the number of leaf nodes by calling the function “num_LeafNodes ()”.
BinaryTree.h:
- Include required header files.
- Create a class template.
- Declare a class named “BinaryTree”. Inside the class,
- Inside the “private” access specifier,
- Give the structure declaration for the creation of node.
- Create an object for the template.
- Create two pointers named “left_Node” and “right_Node” to access the value left and right nodes respectively.
- Declare a variable “leafCount”.
- Create a pointer named “root” to access the value of root node.
- Give function declaration for “insert ()”, “destroy_SubTree ()”, “delete_Node ()”, “make_Deletion ()”, “display_InOrder ()”, “display_PreOrder ()”, “display_PostOrder ()”, “count_Nodes ()”, “count_Leaves ()”.
- Give the structure declaration for the creation of node.
- Inside “public” access specifier,
- Give the definition for constructor and destructor.
- Give function declaration.
- Inside the “private” access specifier,
- Declare template class.
- Give function definition for “insert ()”.
- Check if “nodePtr” is null.
- If the condition is true then, insert node.
- Check if value of new node is less than the value of node pointer
- If the condition is true then, Insert node to the left branch by calling the function “insert ()” recursively.
- Else
- Insert node to the right branch by calling the function “insert ()” recursively.
- Check if “nodePtr” is null.
- Declare template class.
- Give function definition for “insert_Node ()”.
- Create a pointer for new node.
- Assign the value to the new node.
- Make left and right node as null
- Call the function “insert ()” by passing parameters “root” and “newNode”.
- Declare template class.
- Give function definition for “destroy_SubTree ()”.
- Check if the node pointer points to left node
- Call the function recursively to delete the left sub tree.
- Check if the node pointer points to the right node
- Call the function recursively to delete the right sub tree.
- Delete the node pointer.
- Check if the node pointer points to left node
- Declare template class.
- Give function definition for “search_Node ()”.
- Assign false to the Boolean variable “status”.
- Assign root pointer to the “nodePtr”.
- Do until “nodePtr” exists.
- Check if the value of node pointer is equal to “num”.
- Assign true to the Boolean variable “status”
- Check if the number is less than the value of node pointer.
- Assign left node pointer to the node pointer.
- Else
- Assign right node pointer to the node pointer.
- Check if the value of node pointer is equal to “num”.
- Return the Boolean variable.
- Declare template class.
- Give function definition for “remove ()”.
- Call the function “delete_Node ()”
- Declare template class.
- Give function definition for “delete_Node ()”
- Check if the number is less than the node pointer value.
- Call the function “delete_Node ()” recursively.
- Check if the number is greater than the node pointer value.
- Call the function “delete_Node ()” recursively.
- Else,
- Call the function “make_Deletion ()”.
- Check if the number is less than the node pointer value.
- Declare template class.
- Give function definition for “make_Deletion ()”
- Create pointer named “tempPtr”.
- Check if the nodePtr is null.
- If the condition is true then, print “Cannot delete empty node.”
- Check if right node pointer is null.
- If the condition is true then,
- Make the node pointer as the temporary pointer.
- Reattach the left node child.
- Delete temporary pointer.
- If the condition is true then,
- Check is left node pointer is null
- If the condition is true then,
- Make the node pointer as the temporary pointer.
- Reattach the right node child.
- Delete temporary pointer.
- If the condition is true then,
- Else,
- Move right node to temporary pointer
- Reach to the end of left-Node using “while” condition.
- Assign left node pointer to temporary pointer.
- Reattach left node sub tree.
- Make node pointer as the temporary pointer.
- Reattach right node sub tree
- Delete temporary pointer.
- Declare template class.
- Give function definition for “display_InOrder ()”.
- Check if the node pointer exists.
- Call the function “display_InOrder ()” recursively.
- Print the value
- Call the function “display_InOrder ()” recursively.
- Check if the node pointer exists.
- Declare template class.
- Give function definition for “display_PreOrder ()”.
- Print the value.
- Call the function “display_PreOrder ()” recursively.
- Call the function “display_PreOrder ()” recursively.
- Declare template class.
- Give function definition for “display_PostOrder ()”.
- Call the function “display_PostOrder ()” recursively.
- Call the function “display_PostOrder ()” recursively.
- Print value
- Declare template class.
- Give function definition for “numNodes ()”.
- Call the function “count_Nodes ()”.
- Declare template class.
- Give function definition for “count_Nodes ()”.
- Declare a variable named “count”.
- Check if the node pointer is null
- Assign 0 to count.
- Else,
- Call the function “count_Nodes ()” recursively.
- Return the variable “count”.
- Declare template class.
- Give function definition for “num_LeafNodes()”.
- Assign 0 to “leafCount”
- Call the function “count_Leaves ()”
- Return the variable.
- Declare template class.
- Give function definition for “count_Leaves()”.
- Call the function “count_Leaves ()” recursively by passing left node pointer as the parameter.
- Call the function “count_Leaves ()” recursively by passing right node pointer as the parameter.
- Check if left and right node pointers are null.
- Increment the variable “leafCount”.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
quick Solution please
struct insert_into_bst {
// Function takes a constant Book as a parameter, inserts that book indexed by
// the book's ISBN into a binary search tree, and returns nothing.
void operator()(const Book& book) {
// // TO-DO (7) |||
/////
// Write the lines of code to insert the key (book's ISBN) and value
// ("book") pair into "my_bst".
END-TO-DO (7) |
}
std::map& my_bst;
};
C Programming language
Part 1: You need to define a data structure for a doubly linked list and a binary search tree. Also, you need to implement the following functions:
Insert Sorted
LINKEDLIST insertSorted(LINKEDLIST head, int num): head points to the first node in the sorted linked list; num is a number to be inserted in in correct place in the linked list pointed at “head”. The linked list should be sorted after inserting “num”. This function returns the head of the modified head.
BSTREE insert(BSTREE root, int num): root points to a node in a binary search tree; num is a number to be inserted in the tree rooted at “root”. This function returns the root of the modified tree.
Find an element
LINKEDLIST find(LINKEDLIST head,int num): head points to the first node of a linked list; num is a number to be searched for in the linked list started at “head”. This function returns a pointer to the node containing “num” or NULL if num is not found
BSTREE find(BSTREE root,int…
Chapter 19 Solutions
Starting Out with C++: Early Objects (9th Edition)
Ch. 19.1 - Prob. 19.1CPCh. 19.1 - Prob. 19.2CPCh. 19.1 - Prob. 19.3CPCh. 19.1 - Prob. 19.4CPCh. 19.1 - Prob. 19.5CPCh. 19.1 - Prob. 19.6CPCh. 19.2 - Prob. 19.7CPCh. 19.2 - Prob. 19.8CPCh. 19.2 - Prob. 19.9CPCh. 19.2 - Prob. 19.10CP
Ch. 19.2 - Prob. 19.11CPCh. 19.2 - Prob. 19.12CPCh. 19 - Prob. 1RQECh. 19 - Prob. 2RQECh. 19 - Prob. 3RQECh. 19 - Prob. 4RQECh. 19 - Prob. 5RQECh. 19 - Prob. 6RQECh. 19 - Prob. 7RQECh. 19 - Prob. 8RQECh. 19 - Prob. 9RQECh. 19 - Prob. 10RQECh. 19 - Prob. 11RQECh. 19 - Prob. 12RQECh. 19 - Prob. 13RQECh. 19 - Prob. 14RQECh. 19 - Prob. 15RQECh. 19 - Prob. 16RQECh. 19 - Prob. 17RQECh. 19 - Prob. 18RQECh. 19 - Prob. 19RQECh. 19 - Prob. 20RQECh. 19 - Prob. 1PCCh. 19 - Prob. 2PCCh. 19 - Prob. 3PCCh. 19 - Prob. 4PCCh. 19 - Prob. 5PCCh. 19 - Prob. 6PCCh. 19 - Prob. 7PCCh. 19 - Prob. 8PCCh. 19 - Prob. 9PCCh. 19 - Prob. 10PC
Knowledge Booster
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
- Ex1. DLL2BST Add a constructor that constructs the binary search tree from the given DLList. BST<T>::BST(const DLL<T>& list)arrow_forwardComputer Science JAVA Write a program that maintains the names of your friends and relatives and thus serves as a friends list. You should be able to enter, delete, modify, or search this data. You should assume that the names are unique. use a class to represent the names in the friends list and another class to represent the friends list itself. This class should contain a Binary Search Tree of names as a data field. (TreeNode Class BinarySearchTree Class FriendsList Class)arrow_forwardCO LL * Question Completion Status: QUESTION 3 Write a recursive function, OnlyChild(..), that returns the number of nodes in a binary tree that has only one child. Consider binaryTreeNode structure is defined as the following. struct binaryTreeNode int info; binaryTreeNode *llink: binaryTreeNode *rlink; The function is declared as the following. You must write the function as a recursive function. You will not get any credits if a non-recursive solution is used. int OnlyChild(binaryTreeNode *p); For the toolbar, press ALT+F10 (PC) or ALT+FN+F10 (Mac). Paragraph Arial 10pt B. ^三へ三 三山 三Ex? X2 = E E E 9 Click Save and Submit to save and submit. Click Save All Answers to save all ansuwers. Is E English (United States) Focus || 15 stv MacBook Air D00 O00 F4 F5 F8 64arrow_forward
- Using discriminated union in F#, create a tree type that can be either an empty tree or a tree node of float * tree * treearrow_forwardin c++ Assume that you have a binary search tree. Write a function that counts the number ofelements in the tree that are less than 15 and prints the result. You write only the func-tion, you do not need to test it.arrow_forwardI have an assignment that requires me to read strings from a text file and insert them into a binary search tree. I am having a hard time reading the data from the file and passing it through a function. This is the code I have right now: struct tree_node {char *data;tree_node *left;tree_node *right;}; char **Read();tree_node *create_node(char **val); int main(){tree_node *root= NULL, *temp_node;char **val = (char**)malloc(sizeof(char*)*17);for (int i = 0; i < 17; i++) {val[i] = (char*)malloc(sizeof(char)*strlen(*val));}temp_node = create_node(val);root = insert(root, temp_node);printf("In Order: ");inorder(root);printf("\n");return 0;} char **Read() {int size;FILE *fp = fopen("in.txt", "r");fscanf(fp, "%d", &size);char** value = (char**)malloc(sizeof(char*));for(int i = 0; i < size; i++) {fscanf(fp, "%s", &value[i]);} fclose(fp);return value;} tree_node *create_node(char **val) {tree_node* temp = (tree_node*)malloc(sizeof(tree_node));for(int i = 0; i < 17; i++)for(int…arrow_forward
- C++ DATA STRUCTURES Implement the TNode and Tree classes. The TNode class will include a data item name of type string,which will represent a person’s name. Yes, you got it right, we are going to implement a family tree!Please note that this is not a Binary Tree. Write the methods for inserting nodes into the tree,searching for a node in the tree, and performing pre-order and post-order traversals.The insert method should take two strings as input. The second string will be added as a child node tothe parent node represented by the first string. Hint: The TNode class will need to have two TNode pointers in addition to the name data member:TNode *sibling will point to the next sibling of this node, and TNode *child will represent the first child ofthis node. You see two linked lists here??? Yes! You’ll need to use the linked listsarrow_forwardC++ please Please complete Programming Exercise 4, from pages 1402, from chapter 19 from your textbook. Write a function, singleParent, that returns the number of nodes in abinary tree that have only one child. Add this function to the classbinaryTreeType and create a program to test this function. (Note: Firstcreate a binary search tree.)arrow_forwardB. Project descriptionFor this project, you will create two ADTs:• A generic ADT Binary Search Tree• An ADT Class database: This ADT contains a list of class sections. Therefore, a class representing a class sectionmust be created.arrow_forward
- How nodes are defined (struct node (value count left right) #:mutable #:transparent) Write in Racket (traverse n) A traversal of a BST is an algorithm for “visiting” all node in the BST. The traversal must visit each node exactly once. In the case of a linked list, a traversal is trivial since the structure is linear: start at the head, move to the next node, and stop when you reach the tail. In the case of a BST, traversal must account for multiple child nodes and keep track of which subtrees have already been visited and which have not. There are three types of traversal: in-order, pre-order, and post-order. We will only implement in-order. The in-order traversal of a BST has the property that the node values will display in ascending or sorted order. The function can be defined either recursively or iteratively. Recursion is much simpler, so we’ll stick to that. Recursive Algorithm for In-Order Traversal of BST parameter: node n, the root of the tree…arrow_forwardGraphs: Depth First Traversal Starting with the same graph program as last assignment, implement a depth first traversal method. Test iy on nodes 1, 2, and 3 as start nodes. Graph program: #include <iostream>#include <vector>#include <string>using namespace std; class Edge;//-------------------------------------------------------------////class Node{public:Node(string iname){name = iname;}string name;int in_count = 0;bool visited = false; vector<Edge *> out_edge_list;};//-------------------------------------------------------------////class Edge{public:Edge(string iname, double iweight, Node *ifrom, Node *ito){name = iname;weight = iweight;from = ifrom;to = ito;} string name;double weight;Node *from;Node *to;bool visited = false;}; //-------------------------------------------------------------////class Graph{public:vector<Node *> node_list;vector<Edge *> edge_list; //----------------------------------------------------------//Node*…arrow_forwardMultiple choice in data structures void doo(node<int>*root){ if(root !=0) { node<int>*p=root; while(root->next!=0) root=root->next; p->data=root->data; } What is this code do? a. swap the first item with the last item in the linked list b. set the first item in the linked list as the last item c. doesn't do anything because the root parameter is passed by value d. change the root item in the binary tree with the farthest leaf itemarrow_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