Starting Out with C++ from Control Structures to Objects (9th Edition)
9th Edition
ISBN: 9780134498379
Author: Tony Gaddis
Publisher: PEARSON
expand_more
expand_more
format_list_bulleted
Concept explainers
Question
Chapter 21, Problem 6PC
Program Plan Intro
Copy the binary tree
Program Plan:
- Declare the header file section.
- Create a template prefix and define the template class IntBinaryTree to perform the following functions:
- Declare the required variables.
- Declare the function prototypes.
- Define the no-argument generic constructor IntBinaryTree () to initialize the root value as null.
- Define destructor for IntBinaryTree () class and call the function destroySubTree() to destroy the sub tree.
- Call the functions insertNode(),displayInOrder(), and remove().
- Declare the overloaded assignment operator for binary tree.
- Define the generic function insert () to insert the node in pointed by the tree node pointer in a tree.
- Define the generic copy constructor for IntBinaryTree class to copy the binary tree using copyTree() function.
- Define the generic function insert () to insert the node in pointed by the tree node pointer in a tree.
- Define the generic function insertNode() to create a new node and it is passed inside the insert() function to insert a new node into the tree.
- Define the generic function destroySubTree() to delete the left and right subtree.
- Define the generic function remove()which calls deleteNode() to delete the node.
- Define the generic function deleteNode()which delete the node stored in the variable num and it calls the makeDeletion() function to delete a particular node passed inside the argument.
- Define the generic function makeDeletion()which takes the reference to a pointer to delete the node and the brances of the tree corresponding below the node are reattached.
- Define the generic function displayInOrder()to display the values in the subtree pointed by the node pointer.
- Define the generic function copyTree()to copy the entire binary tree.
- In main() function,
- Create two binary trees with integer data type to hold the integer values.
- Call the function insertNode() to insert some nodes into tree.
- Using assignment operator assign one tree into another tree using respective variables.
- Call the function displayInOrder() to display the nodes in order for tree1 and tree 2.
- Create another object for IntBinaryTree class and copy the tree1 to tree3 using copy constructor and display the tree using displayInOrder() function.
Expert Solution & Answer
Want to see the full answer?
Check out a sample textbook solutionStudents have asked these similar questions
In C++
Help with codes and using C++ I will appreciate it and thumbs up
// Templated binary search tree
template<typename Type>
class BST {
friend class DSA_TestSuite_Lab7; // Giving access to test code
struct Node {
Type data; // The value being stored
Node* left, * right; // The left and right nodes
Node* parent; // The parent node
// Constructor
// Always creates a leaf node
//
// In: _data The value to store in this node
// _parent The parent pointer (optional)
Node(const Type& _data, Node* _parent = nullptr) {
// TODO: Implement this method
}
};
// Data members
Node* mRoot; // The top-most Node in the tree
public:
// Default constructor
// Always creates an empty tree
BST() {
// TODO: Implement this method
}
// Destructor
// Clear all dynamic memory
~BST() {
// TODO: Implement this method
}
// Copy constructor
// Used to initialize one object to another
//
// In: _copy The object to copy from
BST(const BST<Type>& _copy) {
// TODO: Implement this…
Using discriminated union in F#, create a tree type that can be either an empty tree or a tree node of float * tree * tree
Chapter 21 Solutions
Starting Out with C++ from Control Structures to Objects (9th Edition)
Ch. 21.1 - Prob. 21.1CPCh. 21.1 - Prob. 21.2CPCh. 21.1 - Prob. 21.3CPCh. 21.1 - Prob. 21.4CPCh. 21.1 - Prob. 21.5CPCh. 21.1 - Prob. 21.6CPCh. 21.2 - Prob. 21.7CPCh. 21.2 - Prob. 21.8CPCh. 21.2 - Prob. 21.9CPCh. 21.2 - Prob. 21.10CP
Ch. 21.2 - Prob. 21.11CPCh. 21.2 - Prob. 21.12CPCh. 21 - Prob. 1RQECh. 21 - Prob. 2RQECh. 21 - Prob. 3RQECh. 21 - Prob. 4RQECh. 21 - Prob. 5RQECh. 21 - Prob. 6RQECh. 21 - Prob. 7RQECh. 21 - Prob. 8RQECh. 21 - Prob. 9RQECh. 21 - Prob. 10RQECh. 21 - Prob. 11RQECh. 21 - Prob. 12RQECh. 21 - Prob. 13RQECh. 21 - Prob. 14RQECh. 21 - Prob. 15RQECh. 21 - Prob. 16RQECh. 21 - Prob. 17RQECh. 21 - Prob. 18RQECh. 21 - Prob. 19RQECh. 21 - Prob. 20RQECh. 21 - Prob. 21RQECh. 21 - Prob. 22RQECh. 21 - Prob. 23RQECh. 21 - Prob. 24RQECh. 21 - Prob. 25RQECh. 21 - Prob. 1PCCh. 21 - Prob. 2PCCh. 21 - Prob. 3PCCh. 21 - Prob. 4PCCh. 21 - Prob. 5PCCh. 21 - Prob. 6PCCh. 21 - Prob. 7PCCh. 21 - 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.Similar questions
- Course: Data Structure and Algorithms Language: C++ Question is well explained Question #2Implement a class for Circular Doubly Linked List (with a dummy header node) which stores integers in unsorted order. Your class definitions should look like as shown below: class CDLinkedList;class DNode {friend class CDLinkedList;private int data;private DNode next;private DNode prev;};class CDLinkedList {private:DNode head; // Dummy header nodepublic CDLinkedList(); // Default constructorpublic bool insert (int val); public bool removeSecondLastValue (); public void findMiddleValue(); public void display(); };arrow_forwardBinary Trees (C++) You will create a class for Emploveelnfo that contains: • Employee ID Number – int Employee Name – string Implement a binary tree whose nodes hold an instance of your Emploveelnfo class. The nodes should be sorted on the Employee ID number. You will have a binary tree class header and implementation file. You can write your main to use the following information to test your tree: Employee Name James B W Bevis Employee ID Number 6702 Romney Wordsworth Revis Jacara Clegg Forbes Kalin Tros 1255 1071 2390 1558 7406 Archibald Beechcroft Penthor Mul Bartlet Finchley 7562 3004 4922 Latham Bine 8483 Jeff Myrtlebank Your program should read an employee ID and display a message telling the user whether it was found in the tree or not. Your main will have one binary tree object and all of the operations on the binary tree will be class methods. Other functions to add • Add function to display the tree • Allow user to add a new employee • Allow user to remove an employee NOTE –…arrow_forwardC programming I need help writing a code that uses a struct pointer into a binary tree and using the same pointer into an arrayarrow_forward
- A tree is implemented using a node structure defined as: struct node{ int data; struct node *left; strict node *right; }; Write a function whose prototype is: int smallest(struct node *); which accepts a tree (pointer to the root) and returns the smallest node in the right sub-tree of the root. If root is NULL, or if there is no right sub-tree, returm the number -999.arrow_forwardCreate a function that uses Node * pointing to root of AVL Tree as input and if valid, return true, and if false, return false.arrow_forwardProgramming language C++ problemarrow_forward
- T/F Suffix array is space efficient and faster than the suffix 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_forwardin C++, create a binary search tree class using the class templatr below. class BinarySearchTree{ public:BinarySearchTree(); ~BinarySearchTree(); const int & findMin() const; const int & findMax() const; bool contains(cons tint &x) const; bool isEmpty(); void insert(const int &x); void remove(const int &x); private: struct BinaryNode{ int element; BinaryNode *left; BinaryNode *right; BinaryNode(const int &el, BinaryNode *lt, BinaryNode *rt) :element(el), left(lt), right(rt){} }; BinaryNode *root; void insert(const int &x, BinaryNode *&t) const; void remove(const int &x, BinaryNode *&t) const; BinaryNode * findMin(BinaryNode *t) const; BinaryNode * findMax(BinaryNode *t) const; bool contains(cons tint &x, BinaryNode *t) const; };arrow_forward
- c++ code screenshot and output is mustarrow_forwardCreate a function that uses Node * pointing to root of AVL Tree as an input and returns the height of the AVL tree in O(log(n)).arrow_forwardstruct 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; };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