예제 #1
0
파일: main.cpp 프로젝트: bdrillard/eecs-268
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;
}
예제 #2
0
파일: Driver.cpp 프로젝트: jmacvey/c_code
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;

}
예제 #3
0
파일: main.cpp 프로젝트: chrzhang/abc
int main() {
    srand(time(0));
    for (int iteration = 0; iteration < NUM_ITERATIONS; ++iteration) {
        std::vector<int> sortedArr;
        int randNumNodes = rand() % NUM_NODES;
        for (int i = 0; i < randNumNodes; ++i) {
            sortedArr.push_back(i);
        }
        {
            BinarySearchTree b;
            b.fromSortedArrayOpt(sortedArr);
            assert(b.getHeight() == heightNeededForNodes(b.size));
        }
        {
            BinarySearchTree b;
            b.fromSortedArray(sortedArr);
            assert(b.getHeight() == heightNeededForNodes(b.size));
        }
    }
    return 0;
}
int main () {
    vector<string> temp;
	BinarySearchTree<string>* treePtr = new BinarySearchTree<string>();
	string usr_input, input = "f,c,h,a,d,g,j";
	parse(input, temp, del);
	int tSize = tokenSize(input, del);
	cout << "Display the binary search tree as it is being built:\n	";
	for(int i =0; i<tSize; i++) {
		treePtr->add(temp[i]);
		cout << temp[i] << ( ( i < (tSize - 1) ) ? "," : "\n" );
	}
	treePtr->drawBinarySearchTree();
	menu();
	while (true) {
		cout << "\nTrace(" << ( (trace == 0) ? "OFF" : "ON" ) << ") -->YOUR CHOICE-> ";
		getline(cin,usr_input);
		if (usr_input.length() > 1) cout << "\nSingle Character ONLY! " << endl;
		else {
            char in = tolower(usr_input[0]);
			if (in == 'p') treePtr->drawBinarySearchTree();
			else if (in == 'e') cout << "Binary search tree is: " << (treePtr->isEmpty() ? "EMPTY\n": "NOT EMPTY\n");
			else if (in == 'h') cout << "Height: " << treePtr->getHeight() << endl;
			else if (in == 'n') cout << "Number of Nodes: " << treePtr->getNumberOfNodes() << endl;
			else if (in == 'i') {
				cout << "Enter token: ";
				getline(cin, input);
				treePtr->add(input);
				tSize++;
				cout << endl << input << " is inserted to the binary search tree.\n";
			}
			else if (in == 'b') {
				treePtr->clear();
				temp.clear();
				cout << "---- Enter a (\'" << del << "\' separated) string -> ";
				getline(cin, input);
				parse(input, temp, del);
				tSize = tokenSize(input,del);
				for (int i = 0; i< tSize; i++) {
					treePtr->add(temp[i]);
					if (trace == 1) treePtr->drawBinarySearchTree();
				}
			}
			else if (in == 's') {
                treePtr->clear();
				temp.clear();
				cout << "---- Enter a (\'" << del << "\' separated) string -> ";
				getline(cin, input);
				parse(input, temp, del);
				tSize = tokenSize(input,del);
				sort(temp.begin(), temp.end());sort(temp.begin(), temp.end());
                treePtr->sortedArray2BST(temp, 0, tSize-1);
			}
			else if (in == 'r') {
                cout << "Enter token: ";
				getline(cin, input);
				treePtr->remove(input);
			}
			else if (in == 't') trace = (trace + 1) % 2;
			else if (in == 'm') menu();
			else if (in == 'c') {
				treePtr->clear();
				tSize = 0;
			}
			else if (in == 'q') exit(0);
			else cout << "Invalid Input ! " << endl;
		}
	}
}
예제 #5
0
void heightAndNodes(const BinarySearchTree<ItemType>& BST, const string& treeName)
{
	cout << "\nHeight of " << treeName << " is " << BST.getHeight();
	cout << "\n\nNumber of nodes in " << treeName << " is " << BST.getNumberOfNodes();
}  // end heightAndNodes
예제 #6
0
파일: BSTDriver.cpp 프로젝트: jrkotun/Lab12
int main()
{
   //the unsorted ListArray of cds
   ListArray<CD>* cds = CD::readCDs("cds.txt");
   int numItems = cds->size();
   cout << numItems << endl;
   cout << endl;

   //test the binary search tree
   //insert all of the cds
   ListArrayIterator<CD>* iter = cds->iterator();
   BinarySearchTree<CD>* bst = new BinarySearchTree<CD>(&CD::compare_items, &CD::compare_keys);
   while(iter->hasNext())
   {
      CD* cd = iter->next();
      bst->insert(cd);
   }
   delete iter;

   BinaryTreeIterator<CD>* bst_iter = bst->iterator();
   bst_iter->setInorder();  //takes a snapshot of the data
   while(bst_iter->hasNext())
   {
      CD* cd = bst_iter->next();
      //cd->displayCD();
   }
   delete bst_iter;

   int height = bst->getHeight();
   bool balance = bst->isBalanced();

   if(balance == true)
   {
       cout << "\nThe height is " << height << " and it is balanced.\n";
   }
   else
   {
       cout << "\nThe height is " << height << " and it is not balanced.\n";
   }

   //create a minimum height binary search tree
   BinarySearchTree<CD>* min_bst = bst->minimize();
   bst_iter = min_bst->iterator();

   //make sure that an inorder traversal gives sorted order
   bst_iter->setInorder();  //takes a snapshot of the data
   while(bst_iter->hasNext())
   {
      CD* cd = bst_iter->next();
      //cd->displayCD();
   }
   delete bst_iter;

   height = min_bst->getHeight();
   balance = min_bst->isBalanced();

   if(balance == true)
   {
       cout << "\nThe minimum height is " << height << " and it is balanced.\n";
   }
   else
   {
       cout << "\nThe minimum height is " << height << " and it is not balanced.\n";
   }

   //create a complete binary search tree
   BinarySearchTree<CD>* complete_bst = bst->minimizeComplete();
   delete bst;

   //make sure that an inorder traversal gives sorted order
   bst_iter = complete_bst->iterator();										
   bst_iter->setInorder(); //takes a snapshot of the data
   while(bst_iter->hasNext())
   {
      CD* cd = bst_iter->next();
      //cd->displayCD();
   }
   delete bst_iter;

   height = complete_bst->getHeight();
   balance = complete_bst->isBalanced();

   if(balance == true)
   {
       cout << "\nThe minimum height is " << height << " and it is balanced.\n";
   }
   else
   {
       cout << "\nThe minimum height is " << height << " and it is not balanced.\n";
   }

   delete complete_bst;

   deleteCDs(cds);
   delete cds;
   return 0;
}