Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

1 - Accept simple entries such as name, and phone number 2 - Store the address book entries in a form of a Binary tree

1 - Accept simple entries such as name, and phone number 2 - Store the address book entries in a form of a Binary tree by inserting the entries, you will have to modify the current implementation to accept strings 3 - Upon startup, load the stored phone info from the phonebook file 4 - Display phone numbers info in a sorted order, limit the entries to few

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

image text in transcribed

BinaryTree,h: \#ifndef BINARYTREE_H \#define BINARYTREE_H \#include using namespace std; // Stack template template class BinaryTree \{ private: struct TreeNode \{ T value: // The value in the node TreeNode left; // Pointer to left child node TreeNode right; // Pointer to right child node \} TreeNode root; // Pointer to the root node // Private member functions void insert(TreeNode &, TreeNode & ); void destroySubTree(TreeNode * ); void deleteNode(T, TreeNode *\&); void makeDeletion(TreeNode *\&); void displaylnOrder(TreeNode ) const; void displayPreOrder(TreeNode * * const; void displayPostOrder(TreeNode * ) const; public: // Constructor BinaryTree() \{ root = nullptr; } // Destructor BinaryTree() { destroySubTree(root); } // Binary tree operations void insertNode(T); bool searchNode(T); void remove(T); void displaylnOrder() const \{ displaylnOrder(root); } void displayPreOrder() const { displayPreOrder(root); } void displayPostOrder() const { displayPostOrder(root); } 3; template else if (newNode->value > value) insert(nodePtr->left, newNode); // Search the left branch else insert(nodePtr->right, newNode); // Search the right branch 3 // insertNode creates a new node to hold num as its value, and passes it to the insert function. template void BinaryTree :-insertNode(T item) \{ TreeNode newNode = nullptr; // Pointer to a new node. // Create a new node and store num in it. newNode = new TreeNode; newNode->value = item; newNode > left = newNode > right = nullptr; // Insert the node. insert(root, newNode); \} // destroysubTree is called by the destructor. It deletes all nodes in the tree. template class T> if (nodePtr->left) destroySubTree(nodePtr->left); if (nodePtr->right) destroySubTree(nodePtr->right); delete nodePtr; \} // searchNode determines if a value is present in // the tree. If so, the function returns true. // Otherwise, it returns false. template bool BinaryTree : searchNode(T item) \{ TreeNode *nodePtr = root; while (nodePtr) \{ if (nodePtr->value == item) return true; else if (item value) nodePtr = nodePtr->left; else nodePtr = nodePtr->right; \} return false; \} template void BinaryTree> T *remove(T item) \{ deleteNode(item, root); \} template void BinaryTree - deleteNode(T item, TreeNode * \&nodePtr) \{ if (item value) deleteNode(item, nodePtr->left); else if (item > nodePtr->value) deleteNode(item, nodePtr->right); else makeDeletion(nodePtr); \} template void BinaryTree : makeDeletion(TreeNode * \&nodePtr) \{ // Define a temporary pointer to use in reattaching I/ the left subtree. TreeNode *tempNodePtr = nullptr, if (nodePtr == nullptr) squt "Cannot delete empty node. nn; else if (nodePtr->right == nullptr) \{ tempNodePtr = nodePtr; nodePtr = nodePtr->left: // Reattach the left child delete tempNodePtr; \} else if (nodePtr->left == nullptr) tempNodePtr = nodePtr; nodePtr = nodePtr-> ight; // Reattach the right child delete tempNodePtr; \} // If the node has two children. else \{ // Move one node the right. tempNodePtr = nodePtr->right; // Go to the end left node. while (tempNodePtr->left) tempNodePtr = tempNodePtr->left; // Reattach the left subtree. tempNodePtr > left = nodePtr > left; tempNodePtr = nodePtr // Reattach the right subtree. nodePtr = nodePtr->right; delete tempNodePtr; \} \} template class T> Here is IntBinarytreeh: \#ifndef INTBINARYTREE_H \#define INTBINARYTREE_H class IntBinaryTree \{ private: struct TreeNode Llas minudyyret \{ private: struct TreeNode \{ int value; // The value in the node TreeNode left: // Pointer to left child node TreeNode * right; // Pointer to right child node \} TreeNode root: // Pointer to the root node // Private member functions void insert(TreeNode &, TreeNode & ); void destroySubTree(TreeNode *); void deleteNode(int, TreeNode *\&); void makeDeletion(TreeNode & ); void displaulnOrder(TreeNode ) const; void displavPreQrder(TreeNode ) const; void displaxPestQrder(TreeNode ) const; public: // Constructor IntBinaryTree() root= nulletr; } // Destructor intinaryTree() \{ destroySubTree(root); \} void displayPreOrder(TreeNode ) const; void displayPostorder(TreeNode ) const; public: // Constructor IntBinaryTree() \{ root = nulletr; ; // Destructor \{ displayPostorder(root);\} \{ destroySubTree(root); I/ Binary tree operations void insertNode(int); bool searchNode(int); void remove(int); IntBinaryTree.h: \#ifndef INTBINARYTREE_H \#define INTBINARYTREE_H class IntBinaryTree \{ private: struct TreeNode \{ int value; // The value in the node TreeNode *left; // Pointer to left child node TreeNode right; // Pointer to right child node 3 TreeNode root: // Pointer to the root node // Private member functions void insert(TreeNode &, TreeNode & ); void destroySubTree(TreeNode * ); void deleteNode(int, TreeNode * 8 ); void makeDeletion(TreeNode *\&); void displaylnQrder(TreeNode *) const; void displaxPreQrder(TreeNode * * const; void displayPostOrder(TreeNode ) const; public: // Constructor public: // Constructor IntBinary Tree() root= nullptr; } // Destructor IntBinaryTree() \{ destroySubTree(root); } // Binary tree operations void insertNode(int); bool searchNode(int); void remove(int); void displaylnOrder() const \{ displavloQrder(root); } void displayPreQrder() const \{ displayPreQrder(root); } void displayPostQrder() const \{ displayPostarder(root); \} \} \#endif main.cpp: int main() IntBinaryTree tree; // Insert some nodes. cout

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

Step: 3

blur-text-image

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

Logistics Lifeline Supply Chain Strategies

Authors: Ehsan Sheroy

1st Edition

7419377502, 978-7419377503

More Books

Students also viewed these Databases questions

Question

b. Where did they come from?

Answered: 1 week ago