Question
I am getting an error no suitable conversion function from orderedLinkedList to const int exists. As well as error code C2664 'void bSearchTreeType ::createList(const
I am getting an error "no suitable conversion function from "orderedLinkedList
Here is the problem, (this main function MUST be used):
Write a function that inserts the nodes of a binary tree into an ordered linked list. Also write a program to test your function.
Do not change anything in the supplied Ch19_Ex9.cpp except to add documentation and your name.
Please use the file names listed below since your file will have the following components: Ch19_Ex9.cpp shown below
//Data //68 43 10 56 77 82 61 82 33 56 72 66 99 88 12 6 7 21 -999
#include
using namespace std; int main() { bSearchTreeType
cout << "Enter numbers ending with -999" << endl; cin >> num;
while (num != -999) { treeRoot.insert(num); cin >> num; }
cout << endl << "Tree nodes in inorder: "; treeRoot.inorderTraversal(); cout << endl; cout << "Tree Height: " << treeRoot.treeHeight() << endl; treeRoot.createList(newList);
cout << "newList: "; newList.print(); cout << endl; system("pause");
return 0; }
binarySearchTree.h binaryTree.h linkedList.h orderedLinkedList.h
Here are my headers:
*****linkedList.h*****
#ifndef H_LinkedListType #define H_LinkedListType
#include
using namespace std;
//Definition of the node /* template
linkedListIterator(const nodeType
Type operator*(); //Function to overload the dereferencing operator *. //Postcondition: Returns the info contained in the node.
linkedListIterator
bool operator==(const linkedListIterator
bool operator!=(const linkedListIterator
private: nodeType
template
template
template
template
return *this; }
template
template
//***************** class linkedListType ****************
template
void initializeList(); //Initialize the list to an empty state. //Postcondition: first = nullptr, last = nullptr, count = 0;
bool isEmptyList() const; //Function to determine whether the list is empty. //Postcondition: Returns true if the list is empty, // otherwise it returns false.
void print() const; //Function to output the data contained in each node. //Postcondition: none
int length() const; //Function to return the number of nodes in the list. //Postcondition: The value of count is returned.
void destroyList(); //Function to delete all the nodes from the list. //Postcondition: first = nullptr, last = nullptr, count = 0;
Type front() const; //Function to return the first element of the list. //Precondition: The list must exist and must not be // empty. //Postcondition: If the list is empty, the program // terminates; otherwise, the first // element of the list is returned.
Type back() const; //Function to return the last element of the list. //Precondition: The list must exist and must not be // empty. //Postcondition: If the list is empty, the program // terminates; otherwise, the last // element of the list is returned.
virtual bool search(const Type& searchItem) const = 0; //Function to determine whether searchItem is in the list. //Postcondition: Returns true if searchItem is in the // list, otherwise the value false is // returned.
virtual void insertFirst(const Type& newItem) = 0; //Function to insert newItem at the beginning of the list. //Postcondition: first points to the new list, newItem is // inserted at the beginning of the list, // last points to the last node in the list, // and count is incremented by 1.
virtual void insertLast(const Type& newItem) = 0; //Function to insert newItem at the end of the list. //Postcondition: first points to the new list, newItem // is inserted at the end of the list, // last points to the last node in the list, // and count is incremented by 1.
virtual void deleteNode(const Type& deleteItem) = 0; //Function to delete deleteItem from the list. //Postcondition: If found, the node containing // deleteItem is deleted from the list. // first points to the first node, last // points to the last node of the updated // list, and count is decremented by 1.
linkedListIterator
linkedListIterator
linkedListType(); //default constructor //Initializes the list to an empty state. //Postcondition: first = nullptr, last = nullptr, count = 0;
linkedListType(const linkedListType
~linkedListType(); //destructor //Deletes all the nodes from the list. //Postcondition: The list object is destroyed.
protected: int count; //variable to store the number of //elements in the list nodeType
private: void copyList(const linkedListType
template
template
template
template
template
current = first; //set current so that it points to //the first node while (current != nullptr) //while more data to print { cout << current->info << " "; current = current->link; } }//end print
template
template
return first->info; //return the info of the first node }//end front
template
return last->info; //return the info of the last node }//end back
template
return temp; }
template
return temp; }
template
if (first != nullptr) //if the list is nonempty, make it empty destroyList();
if (otherList.first == nullptr) //otherList is empty { first = nullptr; last = nullptr; count = 0; } else { current = otherList.first; //current points to the //list to be copied count = otherList.count;
//copy the first node first = new nodeType
first->info = current->info; //copy the info first->link = nullptr; //set the link field of //the node to nullptr last = first; //make last point to the //first node current = current->link; //make current point to //the next node
//copy the remaining list while (current != nullptr) { newNode = new nodeType
template
template
//overload the assignment operator template
return *this; }
#endif
*****orderedLinkedList.h*****
#ifndef H_orderedListType #define H_orderedListType
#include "linkedList.h"
using namespace std;
template
void insert(const Type& newItem); //Function to insert newItem in the list. //Postcondition: first points to the new list, newItem // is inserted at the proper place in the // list, and count is incremented by 1.
void insertFirst(const Type& newItem); //Function to insert newItem in the list. //Because the resulting list must be sorted, newItem is //inserted at the proper in the list. //This function uses the function insert to insert newItem. //Postcondition: first points to the new list, newItem is // inserted at the proper in the list, // and count is incremented by 1.
void insertLast(const Type& newItem); //Function to insert newItem in the list. //Because the resulting list must be sorted, newItem is //inserted at the proper in the list. //This function uses the function insert to insert newItem. //Postcondition: first points to the new list, newItem is // inserted at the proper in the list, // and count is incremented by 1.
void deleteNode(const Type& deleteItem); //Function to delete deleteItem from the list. //Postcondition: If found, the node containing // deleteItem is deleted from the list; // first points to the first node of the // new list, and count is decremented by 1. // If deleteItem is not in the list, an // appropriate message is printed. };
template
current = first; //start the search at the first node
while (current != nullptr && !found) if (current->info >= searchItem) found = true; else current = current->link;
if (found) found = (current->info == searchItem); //test for equality
return found; }//end search
template
bool found;
newNode = new nodeType
if (first == nullptr) //Case 1 { first = newNode; last = newNode; count++; } else { current = first; found = false;
while (current != nullptr && !found) //search the list if (current->info >= newItem) found = true; else { trailCurrent = current; current = current->link; }
if (current == first) //Case 2 { newNode->link = first; first = newNode; count++; } else //Case 3 { trailCurrent->link = newNode; newNode->link = current;
if (current == nullptr) last = newNode;
count++; } }//end else }//end insert
template
template
template
if (first == nullptr) //Case 1 cout << "Cannot delete from an empty list." << endl; else { current = first; found = false;
while (current != nullptr && !found) //search the list if (current->info >= deleteItem) found = true; else { trailCurrent = current; current = current->link; }
if (current == nullptr) //Case 4 cout << "The item to be deleted is not in the " << "list." << endl; else if (current->info == deleteItem) //the item to be //deleted is in the list { if (first == current) //Case 2 { first = first->link;
if (first == nullptr) last = nullptr;
delete current; } else //Case 3 { trailCurrent->link = current->link;
if (current == last) last = trailCurrent;
delete current; } count--; } else //Case 4 cout << "The item to be deleted is not in the " << "list." << endl; } }//end deleteNode
#endif
*****binaryTree.h*****
//Header File Binary Search Tree #ifndef H_binaryTree #define H_binaryTree
#include
using namespace std;
//Definition of the Node template
//Definition of the class template
bool isEmpty() const; //Function to determine whether the binary tree is empty. //Postcondition: Returns true if the binary tree is empty; // otherwise, returns false.
void inorderTraversal() const; //Function to do an inorder traversal of the binary tree. //Postcondition: Nodes are printed in inorder sequence.
void preorderTraversal() const; //Function to do a preorder traversal of the binary tree. //Postcondition: Nodes are printed in preorder sequence.
void postorderTraversal() const; //Function to do a postorder traversal of the binary tree. //Postcondition: Nodes are printed in postorder sequence.
int treeHeight() const; //Function to determine the height of a binary tree. //Postcondition: Returns the height of the binary tree.
int treeNodeCount() const; //Function to determine the number of nodes in a //binary tree. //Postcondition: Returns the number of nodes in the // binary tree.
int treeLeavesCount() const; //Function to determine the number of leaves in a //binary tree. //Postcondition: Returns the number of leaves in the // binary tree.
void destroyTree(); //Function to destroy the binary tree. //Postcondition: Memory space occupied by each node // is deallocated. // root = nullptr;
virtual bool search(const elemType& searchItem) const = 0; //Function to determine if searchItem is in the binary //tree. //Postcondition: Returns true if searchItem is found in // the binary tree; otherwise, returns // false.
virtual void insert(const elemType& insertItem) = 0; //Function to insert insertItem in the binary tree. //Postcondition: If there is no node in the binary tree // that has the same info as insertItem, a // node with the info insertItem is created // and inserted in the binary search tree.
virtual void deleteNode(const elemType& deleteItem) = 0; //Function to delete deleteItem from the binary tree //Postcondition: If a node with the same info as // deleteItem is found, it is deleted from // the binary tree. // If the binary tree is empty or // deleteItem is not in the binary tree, // an appropriate message is printed.
virtual void createList(const elemType& newList) = 0;
binaryTreeType(const binaryTreeType
binaryTreeType(); //Default constructor
~binaryTreeType(); //Destructor
protected: nodeType
private: void copyTree(nodeType
void destroy(nodeType
void inorder(nodeType
void preorder(nodeType
void postorder(nodeType
int height(nodeType
int max(int x, int y) const; //Function to determine the larger of x and y. //Postcondition: Returns the larger of x and y.
int nodeCount(nodeType
int leavesCount(nodeType
};
//Definition of member functions
template
template
template
template
template
template
template
template
template
template
template
template
//Overload the assignment operator template
if (otherTree.root == nullptr) //otherTree is empty root = nullptr; else copyTree(root, otherTree.root); }//end else
return *this; }
template
template
//copy constructor template
//Destructor template
template
template
template
return 0; }
template
return 0; }
#endif
*****binarySearchTree.h*****
//Header File Binary Search Tree
#ifndef H_binarySearchTree #define H_binarySearchTree #include
using namespace std;
template
void insert(const elemType& insertItem); //Function to insert insertItem in the binary search tree. //Postcondition: If there is no node in the binary search // tree that has the same info as // insertItem, a node with the info // insertItem is created and inserted in the // binary search tree.
void deleteNode(const elemType& deleteItem); //Function to delete deleteItem from the binary search tree //Postcondition: If a node with the same info as deleteItem // is found, it is deleted from the binary // search tree. // If the binary tree is empty or deleteItem // is not in the binary tree, an appropriate // message is printed.
void createList(const elemType& addItem);
private: void deleteFromTree(nodeType
template
if (root == nullptrptr) cout << "Cannot search an empty tree." << endl; else { current = root;
while (current != nullptrptr && !found) { if (current->info == searchItem) found = true; else if (current->info > searchItem) current = current->lLink; else current = current->rLink; }//end while }//end else
return found; }//end search
template
newNode = new nodeType
if (root == nullptrptr) root = newNode; else { current = root;
while (current != nullptrptr) { trailCurrent = current;
if (current->info == insertItem) { cout << "The item to be inserted is already "; cout << "in the tree -- duplicates are not " << "allowed." << endl; return; } else if (current->info > insertItem) current = current->lLink; else current = current->rLink; }//end while
if (trailCurrent->info > insertItem) trailCurrent->lLink = newNode; else trailCurrent->rLink = newNode; } }//end insert
template
if (root == nullptr) cout << "Cannot delete from an empty tree." << endl; else { current = root; trailCurrent = root;
while (current != nullptr && !found) { if (current->info == deleteItem) found = true; else { trailCurrent = current;
if (current->info > deleteItem) current = current->lLink; else current = current->rLink; } }//end while
if (current == nullptr) cout << "The item to be deleted is not in the tree." << endl; else if (found) { if (current == root) deleteFromTree(root); else if (trailCurrent->info > deleteItem) deleteFromTree(trailCurrent->lLink); else deleteFromTree(trailCurrent->rLink); } else cout << "The item to be deleted is not in the tree." << endl; } } //end deleteNode
template
if (p == nullptr) cout << "Error: The node to be deleted does not exist." << endl; else if (p->lLink == nullptr && p->rLink == nullptr) { temp = p; p = nullptr; delete temp; } else if (p->lLink == nullptr) { temp = p; p = temp->rLink; delete temp; } else if (p->rLink == nullptr) { temp = p; p = temp->lLink; delete temp; } else { current = p->lLink; trailCurrent = nullptr;
while (current->rLink != nullptr) { trailCurrent = current; current = current->rLink; }//end while
p->info = current->info;
if (trailCurrent == nullptr) //current did not move; //current == p->lLink; adjust p p->lLink = current->lLink; else trailCurrent->rLink = current->lLink;
delete current; }//end else } //end deleteFromTree
template
newNode = new nodeType
if (root == nullptr) { root = newNode; }
}
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started