#include <iostream>
using namespace std;
// structs/Classes
/*********************************************************************/
struct Key {
char* data; // string
Key () {this->data = NULL;}
Key(char* data) { this->data = new char[strlen(data)];
strcpy(this->data,data);}
bool operator== (Key& key) { return 0 == strcmp(this->data,key.data);} // is this == to that key
bool operator< (Key& key) { return -1 == strcmp(this->data,key.data);}// is this < that key
bool operator> (Key& key) { return 1 == strcmp(this->data,key.data);} // is this > that key
void print() { cout << data << endl; }
void setData(char* data);
};
void Key::setData(char* data) {
if (data != NULL) { delete data;}
this->data = new char[strlen(data)];
strcpy(this->data,data);
}
/*********************************************************************/
class BST_Node {
private:
Key key; // key holds the data
BST_Node* left; // ptr to left subtree
BST_Node* right; // ptr to right subtree
public:
// Managers
BST_Node(); //default constructor
BST_Node(Key key); // Construct given key-data
BST_Node(BST_Node& node); // Copy Constructor
~BST_Node(); // Destruct node
// Operators
BST_Node& operator= (BST_Node& node); // Assignment
// Accessors
Key getKey() {return key;} // get Key Data
BST_Node*& getLeft() {return left;} // get root of left subtree
BST_Node*& getRight() {return right;} // get root of right subtree
};
BST_Node::BST_Node() {
left = NULL;
right = NULL;
}
BST_Node::BST_Node(Key key) { // Construct given key-data
this->key = key;
left = NULL;
right = NULL;
}
BST_Node::BST_Node(BST_Node& node) { // Copy Constructor
this->key = node.key;
left = NULL;
right = NULL;
}
BST_Node::~BST_Node(){ // Destruct node
// BST will handle deletions
//delete left;
//delete right;
}
// Operators
BST_Node& BST_Node::operator= (BST_Node& node) { // Assignment
this->key = node.key;
left = node.left;
right = node.right;
return *this;
}
/*********************************************************************/
class BST {
private:
BST_Node* root; // root of tree
int size; // number of elements in tree
// Mutators - called by public methods
bool insert(BST_Node* &subRoot, BST_Node* &node); // insert to subtree (recursive)
void preOrder(BST_Node* subRoot); // preOrder-Traversal of tree (recursive)
void inOrder(BST_Node* subRoot); // inOrder-Traversal of tree (recursive)
void postOrder(BST_Node* subRoot); // postOrder-Traversal of tree (recursive)
public:
// Managers
BST(); // Construct Empty Tree
BST(BST& bst); // Copy Constructor
~BST(); // Destruct tree
// Operators
BST& operator= (BST& bst); // Assignment
// Accessors
int getSize(); // returns number of elements in tree
bool empty(); // is tree empty?
bool full(); // is memory available?
void preOrder(); // preOrder-Traversal of tree (recursive)
void inOrder(); // inOrder-Traversal of tree (recursive)
void postOrder(); // postOrder-Traversal of tree (recursive)
bool findKey(); // take input from user-keyboard
bool findKey(Key key, BST_Node* &parent, BST_Node* &foundNode); // given key-data, find node
bool findKey(Key key);
// Mutators
bool insert(); // take input from user-keyboard
bool insert(Key key); // insert key-data into tree
bool delNode(); // take input from user-keyboard
bool delNode(Key key); // delete node containing key-data from tree
void destroySubTree(BST_Node* subRoot);
};
BST::BST() { //construct empty tree
root=NULL;
size=0;
}
BST::BST(BST& bst) { //copy constructor
root = bst.root;
size = bst.size;
}
BST::~BST() { //destruct tree
delete root->getLeft();
delete root->getRight();
}
int BST::getSize() { //returns number of elements in tree
return size;
}
bool BST::empty() { //is tree empty?
return !size; //or return root==NULL;
}
bool BST::full() { //is memory available?
BST_Node* value = new BST_Node;
if (value) {
delete value;
return true;
}
else {
return false;
}
}
void BST::preOrder() { // preOrder-Traversal of tree (recursive)
//print header for preorder listing
cout << "Preorder Listing" << endl;
if (!root)
cout<<"tree is empty"<<endl;
preOrder(root);
cout << endl << endl;
}
void BST::preOrder(BST_Node* subRoot) {
//base case
if (subRoot == NULL)
return;
//rec case
//process
subRoot->getKey().print();
//go left
preOrder(subRoot->getLeft());
//go right
preOrder(subRoot->getRight());
}
void BST::inOrder() { // inOrder-Traversal of tree (recursive)
//print header for inOrder listing
cout << "Inorder Listing" << endl;
if (!root)
cout<<"tree is empty"<<endl;
inOrder(root);
cout << endl << endl;
}
void BST::inOrder(BST_Node* subRoot) { // inOrder-Traversal of tree (recursive)
//base case
if (subRoot == NULL)
return;
//rec case
//go right
inOrder(subRoot->getLeft());
//go left
//process
subRoot->getKey().print();
inOrder(subRoot->getRight());
}
void BST::postOrder() { // postOrder-Traversal of tree (recursive)
//print header for postOrder listing
cout << "Postorder Listing" << endl;
if (!root)
cout<<"tree is empty"<<endl;
postOrder(root);
cout << endl << endl;
}
void BST::postOrder(BST_Node* subRoot) { // postOrder-Traversal of tree (recursive)
//base case
if (subRoot == NULL)
return;
//rec case
//go right
postOrder(subRoot->getRight());
//go left
postOrder(subRoot->getLeft());
//process
subRoot->getKey().print();
}
bool BST::findKey() {
Key key;
cout << "What would you like to find" << endl;
cin >> key.data;
return findKey(key);
}
bool BST::findKey(Key key) {
if(root == NULL)
return false;
return findKey(key, root, foundNode);
}
bool findKey(Key key, BST_Node* &parent, BST_Node* &foundNode) {
if(foundNode == NULL)
return false;
else if(key.data == foundNode->getKey().data)
return true;
else if(key.data < foundNode->getKey().data) {
return findKey(key, foundNode->getLeft());
}
else if(key.data > foundNode->getKey().data) {
return findKey(key, foundNode->getRight());
}
else
return false;
}
bool BST::insert() { // take input from user-keyboard
char* string = new char[20];
// prompt & input string
cout << "Enter a value: ";
cin >> string;
Key key(string);
return insert(key);
}
bool BST::insert(Key key) { // insert key-data into tree
BST_Node* node = new BST_Node(key);
return insert(root,node);
}
bool BST::insert(BST_Node* &subRoot, BST_Node* &node) { // insert to subtree (recursive)
//root = new BST_Node;
if (!subRoot) {
subRoot = node;
return true;
}
else if(subRoot->getKey() == node->getKey()){ //already there
cout << "Duplicate \n";
return false;
}
else if(subRoot->getKey() > node->getKey()) { //go left
return insert(subRoot->getLeft(), node);
}
else if(subRoot->getKey() < node->getKey()) { //go right
return insert(subRoot->getRight(), node);
}
}
bool BST::delNode() {
Key key;
cout << "What do you want to delete?" << endl;
cin >> key.data;
return delNode(key);
}
bool BST::delNode(Key key) {
BST_Node* node = root;
if(findKey(key,root)) {
if(root == NULL)
return false;
if(root->getLeft() == NULL) {
root = root->getRight();
delete node;
return false;
}
else if(root->getRight() == NULL) {
root = root->getLeft();
delete node;
return false;
}
else {
node = root->getLeft();
while(node->getRight() != NULL)
node = node->getRight();
return true;
}
root->getKey() = node->getKey();
delNode(key);
}
else
return false;
}
void destroySubTree(BST_Node* subRoot) {
return;
}
/*********************************************************************/
void main () {
// Example of Key - delete this when understood
Key k("Paul"), k2("Sue");
cout << k.data << ' ' << k2.data << endl;
BST bst;
bst.insert();
bst.insert();
bst.insert();
bst.insert();
bst.preOrder();
bst.postOrder();
bst.inOrder();
}
i get the following errors and i'm lost with how to fix them
--------------------Configuration: bst - Win32 Debug--------------------
Compiling...
bst.cpp
error C2065: 'foundNode' : undeclared identifier
error C2660: 'findKey' : function does not take 2 parameters
error C2660: 'findKey' : function does not take 2 parameters
error C2661: 'findKey' : no overloaded function takes 2 parameters