Starting Out with C++ from Control Structures to Objects (8th Edition)
8th Edition
ISBN: 9780133769395
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 20, Problem 6PC
Program Plan Intro
Tree Assignment Operator and Copy Constructor
Program Plan:
Main.cpp:
- Include required header files.
- Inside the “main ()” function,
- Create an object “BT” for “IntBinaryTree” class.
- Insert nodes into the binary tree by using the function “insert_Node ()”.
- Display those nodes by using the function “display_InOrder ()”.
- Display those nodes by using the function “display_PreOrder ()”.
- Display those nodes by using the function “display_PostOrder ()”.
- Delete two nodes from the binary tree by using the function “remove ()”.
- Display remaining nodes by using the function “display_InOrder ()”.
BinaryTree.h:
- Include required header files.
- Declare a class named “IntBinaryTree”. Inside the class,
- Inside the “public” access specifier,
- Give the structure declaration for the creation of node.
- Declare a variable “value”.
- Create two pointers named “left_Node” and “right_Node” to access the value left and right nodes respectively.
- 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 ()”, and “copyTree()”.
- Give the structure declaration for the creation of node.
- Inside “public” access specifier,
- Give the definition for constructor, copy constructor and destructor.
- Give function declaration.
- Inside the “public” access specifier,
- Give function definition for copy constructor.
- 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.
- 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”.
- 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
- 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.
- Give function definition for “remove ()”.
- Call the function “delete_Node ()”
- 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.
- Otherwise,
- Call the function “make_Deletion ()”.
- Check if the number is less than the node pointer value.
- 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,
- Otherwise,
- 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.
- 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.
- Give function definition for “display_PreOrder ()”.
- Print the value.
- Call the function “display_PreOrder ()” recursively.
- Call the function “display_PreOrder ()” recursively.
- Give function definition for “display_PostOrder ()”.
- Call the function “display_PostOrder ()” recursively.
- Call the function “display_PostOrder ()” recursively.
- Print value
- Give function definition for the assignment operator.
- Call the function “destroy_SubTree ()”.
- Call the function “copyTree ()”.
- Return the pointer “this”.
- The copy constructor function and assignment operator function calls the “copyTree ()” function.
- Create a new node
- Check if the pointer “nPtr” is not null
- Allocate memory for the new node.
- Assign the value of “nPtr” to the value “newNode”.
- Call the function “copyTree ()” by passing left node pointer.
- Call the function “copyTree ()” by passing right node pointer.
- Return the new node.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionChapter 20 Solutions
Starting Out with C++ from Control Structures to Objects (8th Edition)
Ch. 20.1 - Prob. 21.1CPCh. 20.1 - Prob. 21.2CPCh. 20.1 - Prob. 21.3CPCh. 20.1 - Prob. 21.4CPCh. 20.1 - Prob. 21.5CPCh. 20.1 - Prob. 21.6CPCh. 20.2 - Prob. 21.7CPCh. 20.2 - Prob. 21.8CPCh. 20.2 - Prob. 21.9CPCh. 20.2 - Prob. 21.10CP
Ch. 20.2 - Prob. 21.11CPCh. 20.2 - Prob. 21.12CPCh. 20 - Prob. 1RQECh. 20 - Prob. 2RQECh. 20 - Prob. 3RQECh. 20 - Prob. 4RQECh. 20 - Prob. 5RQECh. 20 - Prob. 6RQECh. 20 - Prob. 7RQECh. 20 - Prob. 8RQECh. 20 - Prob. 9RQECh. 20 - Prob. 10RQECh. 20 - Prob. 11RQECh. 20 - Prob. 12RQECh. 20 - Prob. 13RQECh. 20 - Prob. 14RQECh. 20 - Prob. 15RQECh. 20 - Prob. 16RQECh. 20 - Prob. 17RQECh. 20 - Prob. 18RQECh. 20 - Prob. 19RQECh. 20 - Prob. 20RQECh. 20 - Prob. 21RQECh. 20 - Prob. 22RQECh. 20 - Prob. 23RQECh. 20 - Prob. 24RQECh. 20 - Prob. 25RQECh. 20 - Prob. 1PCCh. 20 - Prob. 2PCCh. 20 - Prob. 3PCCh. 20 - Prob. 4PCCh. 20 - Prob. 5PCCh. 20 - Prob. 6PCCh. 20 - Prob. 7PCCh. 20 - Prob. 8PC
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.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