本文整理汇总了C++中Book::getLeft方法的典型用法代码示例。如果您正苦于以下问题:C++ Book::getLeft方法的具体用法?C++ Book::getLeft怎么用?C++ Book::getLeft使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Book
的用法示例。
在下文中一共展示了Book::getLeft方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: predecessor
// 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;
}
示例2: printBargainBooks
//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;
}
}
}
}
}
示例3: SellBookbyTitle
//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;
}
}
}
}
}