//The function PrintAll() recursively prints
 //all book records stored in the binary tree
 //Only the system directly interacts with this function
 private void PrintAll(BookRecord rt)
 {
     if (rt != null)
     {
         PrintAll(rt.getLeft());
         rt.printRecord();
         PrintAll(rt.getRight());
     }
 }
 //The function searchByCost recursively searches
 //for each book that falls within the given
 //price range.
 //If found, it prints out the book information
 //Only the system directly interacts with this function
 private void searchByCost(double min, double max, BookRecord rt)
 {
     if (rt != null)
     {
         //Checks to see if the cost of the book falls within $10 of the range (min & max).
         if ((rt.getCost() >= (min - 5)) && (rt.getCost() <= (max + 5)))
         {
             rt.printRecord();//If it does, the information is printed
         }
         searchByCost(min, max, rt.getLeft());
         searchByCost(min, max, rt.getRight());
     }
 }
 //The function searchByStockNumber searches the list for books
 //with the given stock number and prints their information.
 public BookRecord searchByStockNumber(long stockNum)
 {
     if (m_pRoot == null) //Checks if there is anything in the tree, prints message if the tree is empty
     {
         Console.Error.WriteLine("There are no contents in this tree!");
     }
     BookRecord temp = new BookRecord();
     temp = m_pRoot;
     //The function searches the tree until it reaches the end
     while ((temp != null) && temp.getStockNum() != stockNum)
     {
         if (stockNum < temp.getStockNum())
         {
             //Search key comes before this node, send temp to left child
             temp = temp.getLeft();
         }
         else
         {
             //Search key comes after this node, send temp to right child
             temp = temp.getRight();
         }
     }
     if (temp == null) //If the target is not found, return null
     {
         return null;
     }
     else //Target found!
     {
         temp.setLeft(null); //Safety purposes, prevents m_pLeft from being exposed to end user
         temp.setRight(null); //Safety purposes, prevents m_pRight from being exposed to end user
         return temp;
     }
     //Function won't ever get here, this is to stop the warning that "not all control paths return a value"
     //return null;
 }
 private void searchByClassification(int cl, BookRecord rt)
 {
     int tempCL;//Since BookRecord::getClassification() is a
     //pass by reference function, tempCL is used to
     //obtain the classification number of the book.
     if (rt != null)
     {
         tempCL = rt.getClassification();
         if (cl == tempCL)
         {
             rt.printRecord();
         }
         searchByClassification(cl, rt.getLeft());
         searchByClassification(cl, rt.getRight());
     }
 }
        //The function removeBook searches for the stock number
        //of a Book Record and removes that book from the list.
        public Boolean removeBook(long stockNum)
	{
		BookRecord back = new BookRecord();
		BookRecord temp = new BookRecord();
		BookRecord delParent = new BookRecord(); //delParent = parent of node to remove, delNode = node to remove
		BookRecord delNode = new BookRecord(); 
		String tempName = "";
		temp = m_pRoot;
		back = null;
		if (temp == null)//Case 1: Checks to see there is anything in the tree
		{
			return false;
		}
		//Finding the node to delete.
		while ((temp != null) && (stockNum != temp.getStockNum()))
		{
			back = temp; //makes back the parent of temp
			if (stockNum < temp.getStockNum())
			{
				temp = temp.getLeft(); //send temp to the next node to the left
			}
			else
			{
				temp = temp.getRight(); //send temp to the next node to the right
			}
		}
		//If tree has reached the end and no book is found, a message is printed
		if (temp == null) //System didnt find anything to remove
		{
			Console.WriteLine("Book Not Found. Nothing was removed.\n");
			return false;
		}
		else //Prepare for deletion
		{
			if (temp == m_pRoot)//If the book is at the root, set delNode to root and delParent to null
			{
				delNode = m_pRoot;
				delParent = null;
			}
			else //Node to remove comes after the root node
			{
				delNode = temp;
				delParent = back;
			}
		}
		//Case 2: Removing node with zero or one child on the left
		if (delNode.getRight() == null) //The delNode has no right child
		{
			if (delParent == null) //Removing the root
			{
				m_pRoot = delNode.getLeft(); //make the left child the new root
				delNode = null;
				return true;
			}
			else //Not at root, decide which side of delParent, delNode is on
			{
				if (delParent.getLeft() == delNode) //Node to delete is a left child
				{
					delParent.setLeft(delNode.getLeft());
				}
				else //Node to delete is a right child
				{
					delParent.setRight(delNode.getLeft());
				}
				delNode = null;
				return true;
			}
		}
		else //Case 3: Deleting node has at least one child on the right
		{
			if (delNode.getLeft() == null) //The delNode has no left child
			{
				if (delParent == null)//Deleting the root
				{
					m_pRoot = delNode.getRight(); //make the right child the new root
					delNode = null;
					return true;
				}
				else //Not at root, decide which side of delParent, delNode is on
				{
					if (delParent.getLeft() == delNode) //Node to remove is a left child
					{
						delParent.setLeft(delNode.getRight());
					}
					else //Node to remove is a right child
					{
						delParent.setRight(delNode.getRight());
					}
					delNode = null;
					return true;
				}
			}
			else //Case 4: Removing node with two children.
			{
				//Find the replacement value. Locate the node containing the largest value smaller
				//than the key of the node being deleted
				temp = delNode.getLeft();
				back = delNode;
				while (temp.getRight() != null)
				{
					back = temp;
					temp = temp.getRight();
				}
				//Copy the replacement values into the
				//node to be "removed"
				tempName = temp.getName();
				delNode.setName(tempName);
				delNode.setClassification(temp.getClassification());
				delNode.setCost(temp.getCost());
				delNode.setNumberInStock(temp.getNumberInStock());
				delNode.setStockNum(temp.getStockNum());

				//delNode = temp;
				//Remove the replacement node from the tree.
				if (back == delNode)
				{
					back.setLeft(temp.getLeft());
				}
				else
				{
					back.setRight(temp.getLeft());
				}
				temp = null;
				return true;
			}
		}
	}