Exemplo n.º 1
0
void BSTtester() {
	using std::string; using std::cout; using std::endl;
	BinarySearchTree<string> myBST;
	cout << "Beginning BinarySearchTree Tests..." << endl;
	cout << "isEmpty() returns: " << myBST.isEmpty() << "; should be 1 (true)." << endl;
	cout << "Adding item to tree..." << endl;
	std::string myString = "A"; myBST.add(myString);
	cout << "isEmpty() returns: " << myBST.isEmpty() << "; should be 0 (false)." << endl;
	cout << "getHeight() returns: " << myBST.getHeight() << "; should be 1." << endl;
	cout << "Adding three more items to the tree..." << endl;
	std::string myNextString[] = { "B", "C", "D", "E" };
	for (int i = 0; i < 4; i++)
	{
		myBST.add(myNextString[i]);
	}
	cout << "inOrder Traversal of tree yields: "; myBST.inorderTraverse(display);
	cout << "; should be A, B, C, D, E" << endl;
	cout << "preOrder Traversal of tree yields: "; myBST.preorderTraverse(display);
	cout << "; should be A, B, C, D, E" << endl;
	cout << "postOrder Traversal of tree yields: "; myBST.postorderTraverse(display);
	cout << "; should be E, D, C, B, A" << endl;
	cout << "getMaxValue() returns: " << myBST.getMaxValue() << "; should be \"E\"" << endl;
	cout << "getMinValue() returns: " << myBST.getMinValue() << "; should be \"A\"" << endl;
	// checking if the tree is a bst
	cout << "isBST() returns: " << myBST.isBST() << "; should be 1 (true)." << endl;
	cout << "Removing item \"C\" from the tree." << endl; myBST.remove("C");
	cout << "inOrder Traversal of tree yields: "; myBST.inorderTraverse(display);
	cout << "; should be A, B, D, E" << endl;
	cout << "isBST() returns: " << myBST.isBST() << "; should be 1 (true)." << endl;
	BinarySearchTree<string> myBST2;

}
Exemplo n.º 2
0
void traverse(const BinarySearchTree<ItemType>& BST, const int& version)
{
	switch (version)
	{
	case 1:
		cout << "\nPreorder:\n";
		BST.preorderTraverse(display);
		break;
	case 2:
		cout << "\nInorder:\n";
		BST.inorderTraverse(display);
		break;
	case 3:
		cout << "\nPostorder:\n";
		BST.postorderTraverse(display);
		break;
	case 4:
		cout << "\nPreorder:\n";
		BST.preorderTraverse(display);

		cout << "\nInorder:\n";
		BST.inorderTraverse(display);

		cout << "\nPostorder:\n";
		BST.postorderTraverse(display);

		break;
	default:
		break;
	}  // end switch
}  // end traverse
Exemplo n.º 3
0
int main(int argc, char** argv) {
    BinarySearchTree<int, int> intTree;
    
    intTree.add(40);
    intTree.add(5);
    intTree.add(10);
    intTree.add(30);
    intTree.add(20);
    intTree.add(35);
    intTree.add(25);
    intTree.add(15);

    cout << intTree.getHeight() << endl;
    cout << intTree.getNumberOfNodes() << endl;

    int item;

    cout << "Inorder\n";
    intTree.inorderTraverse(print);

    cout << "Get entry for 5 " << intTree.getEntry(5) << endl;
    cout << "Get entry for 25 " << intTree.getEntry(25) << endl;
    try {
        cout << "Get entry for 65 " << intTree.getEntry(65) << endl;
    } catch(NotFoundException e) {
        cout << "Error " << e.what() << endl;
    }

    cout << "Does it contain 5 " << intTree.contains(5) << endl;
    cout << "Does it contain 25 " << intTree.contains(25) << endl;
    cout << "Does it contain 65 " << intTree.contains(65) << endl;

    try {
        intTree.remove(45);
    } catch(NotFoundException e) {
        cout << "Error " << e.what() << endl;
    }
    intTree.inorderTraverse(print);
    cout << endl;
    intTree.remove(30);
    intTree.inorderTraverse(print);
    cout << endl;
    intTree.remove(5);
    intTree.inorderTraverse(print);
    cout << endl;
    intTree.remove(15);
    intTree.inorderTraverse(print);
    cout << endl;
    cout << intTree.getHeight() << endl;
    cout << intTree.getNumberOfNodes() << endl;
}
Exemplo n.º 4
0
/*
Function Name: Print
Purpose: Visit to print all remaining suspects
in-order.
Restriction: Only useable in an inquiry.
Pre-condition: printVisit() defined
*/
void PRINT(BinarySearchTree tree)
{
  int suspectsLeft = 0;
  tree.countSuspects(countRemainingSuspects, suspectsLeft);
  if (suspectsLeft == 0){
    cout << "There are currently no suspects." << endl;
  } else {
    cout << "The following suspects remain:" << endl;
    tree.inorderTraverse(printVisit);
    cout << endl;
  }
}
Exemplo n.º 5
0
/*
Function Name: INQUIRY
Purpose: to begin a search for a suspect.
Allows the use of Tip, Check and Print
*/
string INQUIRY(BinarySearchTree tree)
{
    //Define loop and inquiry vars
    string sessionName, command, exitCommand;
    bool inquiryEnd = false;
    int suspectsLeft;
    //Initialize exitCommand to a specific value to
      //be read by the main function in case of
      //improper session name.
    exitCommand = "IMPROPER";
    //Make new tree and copy so I'm not constantly
      //referencing the tree.
    BinarySearchTree inquiryTree;
    inquiryTree = tree;
    //Begin prompting user
    cout << "Enter a name for this inquiry:" << endl;
    getline (cin, sessionName);
    //If sessionName is of appropriate length
    if(sessionName.length() > 0 & sessionName.length() < 31) {
      while (inquiryEnd == false){
        cout << "Now doing inquiry: " << sessionName;
        //line split because of character limit
        cout << ". You can use CHECK, PRINT or TIP." << endl;
        cout << "(ADD, INQUIRY or QUIT will break ";
        cout << "you out of the inquiry.)" << endl;
        cout << endl;
        getline (cin, command);
        upCase(command);
        if (command == "TIP") {
          TIP(inquiryTree);
          //Always reset suspectsLeft to 0 before calling
            //countSuspects because it is an incrementer.
          suspectsLeft = 0;
          inquiryTree.countSuspects(countRemainingSuspects, 
                   suspectsLeft);
          if(suspectsLeft == 1) {
            cout << "ALERT! That leaves only one" << endl;
            cout << "suspect in the " << sessionName;
            cout << " inquiry: ";
            //Since there's only one suspect, just call
              //the traverse function used in the PRINT
              //function.
            inquiryTree.inorderTraverse(printVisit);
          } else if (suspectsLeft == 0) {
            cout << "ALERT! That means there are no suspects" << endl;
            cout << "left in the " << sessionName;
            cout << " inquiry." << endl;
          }
        } else if (command == "CHECK"){
          CHECK(inquiryTree);
        } else if (command == "PRINT"){
          PRINT(inquiryTree);
        } else if (command == "ADD" | command == "INQUIRY"
                                  | command == "QUIT") {
            exitCommand = command;
            inquiryEnd = true;
            //destroy the inquiry tree to save memory
            inquiryTree.~BinarySearchTree();
            cout << endl;
            cout << "Ending inquiry: " << sessionName << endl;
            cout << endl;
        }
      }
    }
    return exitCommand;
}
Exemplo n.º 6
0
void inorder(const BinarySearchTree<ItemType>& BinSearchTree)
{
	cout << endl << "Inorder:" << endl;
	BinSearchTree.inorderTraverse(display);
}