0

I need help with the assignment operator and destructor, and the deleting by copy. Can someone help me with this? and when i compile just this i get an error on line 219 saying term does not evaluate to a function taking 0 arguments. Does anyone know what this means

#include <iostream>
using namespace std;

// Node used in BST
struct BSTNode {
    int key;
    BSTNode* left;
    BSTNode* right;
};

// Binary Search Tree
class BST {
private:
    BSTNode* root;
    int size;

    bool insert (BSTNode* newNode);
    BSTNode** find (int key);
    void preOrderTraversal (BSTNode* subRoot);
    void inOrderTraversal (BSTNode* subRoot);
    void postOrderTraversal (BSTNode* subRoot);

public:
    // Managers
    BST ();
    BST (BST& bst);
    ~BST ();
    BST& operator = (BST& bst);

    // Accessors
    bool isEmpty ();
    bool isFull ();
    void preOrderTraversal ();
    void inOrderTraversal ();
    void postOrderTraversal ();

    // Mutators
    bool insert (int key);
    bool deleteByMerge (int key);
    bool deleteByCopy (int key);
};

// default Constructor
BST::BST () {  
    root = NULL;
    size = 0;
}
// Copy Constructor
BST::BST (BST& bst) {
    root = bst.root;
    size = bst.size;
}

/*************************************/
// Destructor
BST::~BST () {
    // delete root until tree is empty
}

// Assignment Operator
BST& BST::operator = (BST& bst) {
    // ???
    return *this;
}
/***************************************/
// isEmpty: returns if tree is empty
bool BST::isEmpty () { 
    return !root;
}

// isFull: returns if memory is available
bool BST::isFull () {
    BSTNode* bogusNode = NULL;
    bogusNode = new BSTNode;
    if (bogusNode) {
        delete bogusNode;
        return false;
    }
    return true; // Note: nothing allocated
}

// insert (public): adds a node to tree given key data
bool BST::insert (int key) {
    if (isFull())
        return false;

    BSTNode* newNode = new BSTNode;
    newNode->key = key;
    newNode->left = NULL;
    newNode->right = NULL;
    if (insert (newNode))
        return true;
    delete newNode;
    return false;
}

// insert (private): adds a node to tree given new-node
bool BST::insert (BSTNode* newNode) {
    BSTNode** walker = &root;
    while (*walker) {
        if ((*walker)->key > newNode->key)
            walker = &((*walker)->left);
        else if ((*walker)->key < newNode->key)
            walker = &((*walker)->right);
        else    // Duplicate of key found
            return false;
    }
    *walker = newNode;
    return true;
}

// find (private): returns dbl-pointer to target
BSTNode** BST::find (int key) {
    BSTNode** walker = &root;
    while (*walker) {
        if ((*walker)->key == key)
            break;
        else if ((*walker)->key > key)
            walker = &((*walker)->left);
        else
            walker = &((*walker)->right);
    }
    return walker; // Note: if *walker is NULL, key not found
}

// delete (public): using by-merge method: delete node containing key data
bool BST::deleteByMerge (int key) { 
    // find pointer to target deletion
    BSTNode** walker = find(key);

    // Determine whether key is found and deletion is to occur
    if (!(*walker)) 
        return false; // deletion failed - target not found
    // keep pointer to node for deletion
    BSTNode*  eliminationNode = *walker; 

    //CASE I - left-subtree exists
    if (eliminationNode->left) {
        // find right-most child of left subtree
        BSTNode** rtMostOfLeftSubtree = &(eliminationNode->left);
        while (*rtMostOfLeftSubtree) 
            rtMostOfLeftSubtree = &((*rtMostOfLeftSubtree)->right);

        // link right-most child of left subtree to right of target deletion
        *rtMostOfLeftSubtree = eliminationNode->right;

        // bypass target deletion
        *walker = eliminationNode->left;
    }
    // CASE II - no left-subtree
    else {
        // bypass target deletion
        *walker = eliminationNode->right;
    }

    // delete target
    delete eliminationNode;

    // return success of deletion
    return true; // deletion successful
}

/****************************/
// delete (public): using by-copy method: delete node containing key data
bool BST::deleteByCopy (int key) {
    // Use code from deleteByMerge where appropriate

    // CASE I   - target has no children
    // CASE II  - target has left child, but no right child
    // CASE III - target has right child, but no left child
    // CASE IV  - target has two children

    // return success of deletion
    return true; // deletion successful
}

// preOrderTraversal (public): calls private preOrderTraversal
void BST::preOrderTraversal () {
    cout << "Pre-Order Traversal" << endl;
    preOrderTraversal(root);
    cout << "\b\b  " << endl << endl;
}

// preOrderTraversal (private): traverses tree by preOrder 
void BST::preOrderTraversal (BSTNode* subRoot) {
    // base case
    if (!subRoot)
        return;

    // recursive method
    // process node
    cout << subRoot->key << ", ";
    // move left
    preOrderTraversal(subRoot->left);
    // move right
    preOrderTraversal(subRoot->right);
}

// inOrderTraversal (public): calls private inOrderTraversal
void BST::inOrderTraversal () { 
    cout << "In-Order Traversal" << endl;
    inOrderTraversal(root);
    cout << "\b\b  " << endl << endl;
}
// inOrderTraversal (private): traverses tree by inOrder
void BST::inOrderTraversal (BSTNode* subRoot) {
        //base case
    if (subRoot == NULL)
        return;
    //rec case

    //go right
    inOrderTraversal(subRoot->left());
    //go left
    cout << subRoot->key << ", ";
    //process
    inOrderTraversal(subRoot->right());
}

// postOrderTraversal (public): calls private postOrderTraversal
void BST::postOrderTraversal () {
    cout << "Post-Order Traversal" << endl;
    postOrderTraversal(root);
    cout << "\b\b  " << endl << endl;
}
// postOrderTraversal (private): traverses tree by postOrder
void BST::postOrderTraversal (BSTNode* subRoot) {
        //base case
    if (subRoot == NULL)
        return;
    //rec case

    //go left
    postOrderTraversal(subRoot->left());
    //go right
    postOrderTraversal(subRoot->right());

    cout << subRoot->key << ", ";

}

/****************************/
void main () {
    BST bst;
    bst.insert(5);
    bst.insert(10);
    bst.insert(3);
    bst.insert(4);
    bst.insert(9);
    bst.insert(7);
    bst.insert(8);
    bst.preOrderTraversal();
    bst.inOrderTraversal();
    bst.postOrderTraversal();

}

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by Ancient Dragon
0

line 235 (as posted here): left is not a function or a function pointer so you have to remove the parentheses postOrderTraversal(subRoot->left);

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.