Exemple #1
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;
}
void Menu::Undo(BinarySearchTree<DataRecord>& g)
{
	if (DataStack.isEmpty())
		//Nothing to undo
		cout << "nothing to undo";
	else
	{
		//add to Hash Table and BST
		DataRecord Temp;
		DataRecord topStack;
		DataStack.peek(topStack);
		DataStack.pop();
		if (g.getEntry(topStack, Temp))
		{
			//data already exists
			cout << "already exists";
			return;
		}
		else
		{
			g.insert(topStack);
			//successful
			cout << "undo was successful";
			return;
		}
	}
}
Exemple #3
0
//Added by Jose Sepulveda
//This search simplifies it by printing the item if found.
bool EmployeeTree::search(int num){
			Employee target(num, "null");
			Employee result;
			if(empTree.getEntry(target, result)){
				cout << "\nFound!: " << result << "\n";
				return true;
			}
			cout << "\nNot found.\n";
			return false;
			
		}
void Menu::Add(BinarySearchTree<DataRecord>& a)
{
	DataRecord NewRecord;
	//Asks user for data to insert. Similar to lab 4.
	NewRecord.setKey(123); 
	//Now checking to see if data already exists
	DataRecord Temp;//Data Temp needed to access getEntry function of BST. Should be similar to hash fucntion
	if (a.getEntry(NewRecord, Temp))
	{
		//data already exists
		cout << "already exists\n";
		return;
	}
	//prompt for rest of data.
	NewRecord.setRadius(3.14);
	a.insert(NewRecord);
}
void Menu::Delete(BinarySearchTree<DataRecord>& b)
{
	//Asks user for which record to delete.

	//pass that key to the hash function to see if data exists.
	DataRecord LookingFor(5, 14.8);
	DataRecord Temp;
	if (b.getEntry(LookingFor, Temp))
	{
		DataStack.push(Temp);
		b.remove(Temp);
		return;
	}
	//can't be found
	cout << "cant be found\n";
	return;

}
void Menu::Display(const BinarySearchTree<DataRecord>& c)
{	
	//Asks user for which Record they are looking for
	DataRecord LookingFor;
	LookingFor.setKey(123);
	DataRecord Found;
	if (c.getEntry(LookingFor, Found))
	{
		//print what is found.
		cout << Found;
		return;
	}
	else
	{
		//print cant be found.
		cout << "cant be found";
		return;
	}
}