Example #1
0
// Finds predecessor to a book
// Finds the book, gets the book with max value for the left sub-tree
Book* Library::predecessor(double price, Book *book)
{
    Book* tmp = findBook(price, book);
    if ( tmp ) {
        return max(tmp->getLeft(tmp));
    }
    return 0;
}
Example #2
0
//This function is much the the one above.
//It is non-recursive to traverse the tree of books creating a stack.
//As it comes back, it removes books from the stack in order of the sorted parameter (price) and outputs to the user.
//This produces a sorted output by price but cuts off at the max price the user is willing to pay.
void Library::printBargainBooks(Book *tmp, double aPrice) {
    
    cout << "SORTED BY PRICE LOW TO HIGH" << endl;
    //If there are no Books
    if (root == NULL) {
        cout << "There are no books in the library." << endl;
    }
    
    //If there is only one Book
    else if (tmp->getLeft() == 0 && tmp->getRight() == 0) {
        cout << "$" << tmp->getPrice() << " " << tmp->getTitle() << " by " << tmp->getAuthor() << endl;
    }
    
    else {
        Book *stack[200];
        int L = 0;
        Book *current = tmp;
        bool done = false;
        
        while (done == false) {
            
            if (current != NULL) {
                stack[L] = current;
                L++;
                current = current->getLeft(current);
            }
            
            else {
                if (L != 0 && current == NULL) {
                    L--;
                    current = stack[L];
                    if (current->getPrice() <= aPrice) {
                    cout << "$" << current->getPrice() << " " << current->getTitle() << " by " << current->getAuthor() << endl;
                    }
                    current = current->getRight(current);
                }
                else {
                    done = true;
                }
                
            }
        }
    }
    
    
}
Example #3
0
//See comment for function above. This function is essentially identical, simply takes a title instead of ISBN.
void Library::SellBookbyTitle(Book *tmp, string aTitle) {
    
    //If there are no Books
    if (root == NULL) {
        cout << "There are no books in the library." << endl;
    }
    
    //If there is only one Book
    else if (tmp->getLeft() == 0 && tmp->getRight() == 0) {
        cout << "Only one book in the library." << endl;
        cout << "$" << tmp->getPrice() << " " << tmp->getTitle() << " by " << tmp->getAuthor() << endl;
    }
    
    else {
        Book *stack[200];
        int L = 0;
        Book *current = tmp;
        bool done = false;
        
        while (done == false) {
            
            if (current != NULL) {
                stack[L] = current;
                L++;
                current = current->getLeft(current);
            }
            
            else {
                if (L != 0 && current == NULL) {
                    L--;
                    current = stack[L];
                    if (current->getTitle() == aTitle) {
                        if (current->getQuantity() == 1) {
                            Book* parent = stack[L-1];
                            cout << "Oh, looks like you are selling the last one. Removing from tree" << endl;
                            if (current->getLeft() == NULL && current->getRight() == NULL) {
                                if (current->getPrice() > parent->getPrice()) {
                                    parent->setRight(NULL);
                                }
                                else {
                                    parent->setLeft(NULL);
                                }
                            }
                            else if (current->getLeft() != NULL && current->getRight() == NULL) {
                                if (current->getPrice() > parent->getPrice()) {
                                    parent->setRight(current->getLeft());
                                }
                                else {
                                    parent->setLeft(current->getRight());
                                }
                            }
                            else if (current->getLeft() == NULL && current->getRight() != NULL) {
                                if (current->getPrice() > parent->getPrice()) {
                                    parent->setRight(current->getRight());
                                }
                                else {
                                    parent->setLeft(current->getLeft());
                                }
                            }
                            else if (current->getLeft() != NULL && current->getRight() != NULL) {
                                Book* prev = predecessor(current->getPrice(), current);
                                if (prev == NULL) {
                                    prev = successor(current->getPrice(), current);
                                }
                                if (prev->getPrice() > prev->getParent()->getPrice()) {
                                    prev->getParent()->setRight(prev->getRight());
                                }
                                else if (prev->getPrice() <= prev->getParent()->getPrice()) {
                                    prev->getParent()->setLeft(prev->getRight());
                                }
                                current->setPrice(prev->getPrice());
                                delete prev;
                            }
                        }
                        else {
                            int new_quant = current->getQuantity()-1;
                            current->setQuantity(new_quant);
                            cout << "$" << current->getPrice() << " " << current->getTitle() << " by " << current->getAuthor() << endl;
                            cout << "New quantity: " << current->getQuantity() << endl;
                        }
                    }
                    current = current->getRight(current);
                }
                else {
                    done = true;
                }
                
            }
        }
    }
    
    
}