예제 #1
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;

}
예제 #2
0
파일: test.cpp 프로젝트: natemara/CSE-274
void TestBinaryTreeIsBalanced() {
  BinarySearchTree<BSTNode1<int>, int> b;

  b.add(4);
  b.add(2);
  b.add(5);
  b.add(1);
  b.add(3);
  b.add(6);

  assert(b.isBalanced());
}
예제 #3
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;
}
예제 #4
0
파일: test.cpp 프로젝트: nihy/cse274b
bool TestPartTwo() {
  BinarySearchTree<BSTNode1<int>, int> tree;
  tree.add(50);
  tree.add(25);
  tree.add(75);

  bool isTrue = tree.isBalanced();

  tree.add(10);
  tree.add(4);
  tree.add(1);

  bool isFalse = tree.isBalanced();

  return isTrue && !isFalse;
}
예제 #5
0
void testBst(){
    BinarySearchTree<int> *bst = new BinarySearchTree<int>();
    bst->add(8);
    bst->add(3);
    bst->add(10);
    bst->add(1);
    bst->add(6);
    bst->add(14);
    bst->add(13);
    bst->add(4);
    bst->add(7);

    bst->print();

    cout << bst->search(6) << endl;
    cout << bst->search(1337) << endl;
    cout << "Is BST? : " << bst->isBST(1, 14) << endl;
}
예제 #6
0
파일: test.cpp 프로젝트: natemara/CSE-274
void TestBinarySearchTreeGetLE() {
  BinarySearchTree<BSTNode1<int>, int> b;

  b.add(4);
  b.add(2);
  b.add(5);
  b.add(1);
  b.add(3);
  b.add(6);

  DLList<int> l = b.getLE(3);

  assert(l.contains(1));
  assert(l.contains(2));
  assert(l.contains(3));
  assert(!l.contains(4));
  assert(!l.contains(5));
  assert(!l.contains(6));
}
예제 #7
0
파일: test.cpp 프로젝트: natemara/CSE-274
void TestBinarySearchTreePostOrder() {
  BinarySearchTree<BSTNode1<int>, int> b;

  b.add(4);
  b.add(2);
  b.add(5);
  b.add(1);
  b.add(3);
  b.add(6);

  b.postOrderNumbers();

  assert(b.getNode(1)->post_order_ == 0);
  assert(b.getNode(3)->post_order_ == 1);
  assert(b.getNode(2)->post_order_ == 2);
  assert(b.getNode(6)->post_order_ == 3);
  assert(b.getNode(5)->post_order_ == 4);
  assert(b.getNode(4)->post_order_ == 5);
}
예제 #8
0
BinarySearchTree<BSTNode1<int>, int> treeMaker(int i){
	BinarySearchTree<BSTNode1<int>, int> tree;
	if(i == 1){
		tree.add(10);
		tree.add(5);
		tree.add(15);
		tree.add(3);
		tree.add(0);
		tree.add(2);
	}else{
		tree.add(20);
		tree.add(10);
		tree.add(30);
		tree.add(5);
		tree.add(15);
		tree.add(25);
		tree.add(35);
	}
	return tree;
}
예제 #9
0
파일: test.cpp 프로젝트: natemara/CSE-274
void TestBinaryTreeHeight2() {
  BinarySearchTree<BSTNode1<int>, int> b;
  int test = 0;

  for (int i = 0; i < 10000; i++) {
    test = rand() % 100000;
    b.add(test);
  }

  assert(b.height() - b.depth(b.getNode(test)) == b.height2(b.getNode(test)));
  assert(b.height2(b.getRoot()) == b.height());
}
예제 #10
0
파일: test.cpp 프로젝트: nihy/cse274b
bool TestPartThree() {
  BinarySearchTree<BSTNode1<int>, int> tree;
  tree.add(15);
  tree.add(10);
  tree.add(14);
  tree.add(17);
  tree.add(20);
  tree.add(16);
  tree.add(19);
  tree.add(26);
  tree.add(7);
  tree.add(9);
  tree.add(5);

  tree.preOrderNumber();
  tree.inOrderNumber();
  tree.postOrderNumbers();

  bool testPreOrder, testInOrder, testPostOrder;
  if ((tree.getNode(15)->pre_order == 1) && (tree.getNode(5)->pre_order == 4)
      && (tree.getNode(26)->pre_order == 11))
    testPreOrder = true;
  if ((tree.getNode(15)->in_order == 6) && (tree.getNode(5)->in_order == 1)
        && (tree.getNode(26)->in_order == 11))
      testInOrder = true;
  if ((tree.getNode(15)->pst_order == 11) && (tree.getNode(5)->pst_order == 1)
        && (tree.getNode(26)->pst_order == 8))
      testPostOrder = true;

  return testPreOrder && testInOrder && testPostOrder;
}
예제 #11
0
파일: test.cpp 프로젝트: rakhlei/cse274b
void testPart1() {
	cout << "Part 1" << endl;
	BinarySearchTree<BSTNode1<int>, int> tree;
	tree.add(15);
	tree.add(5);
	tree.add(16);
	tree.add(3);
	tree.add(12);
	tree.add(10);
	tree.add(13);
	tree.add(6);
	tree.add(7);
	tree.add(20);
	tree.add(18);
	tree.add(23);

	cout << "height of node: " << tree.height2(tree.getNode(5)) << endl;
	cout << "height of tree: " << tree.height() << endl;
	cout << endl;
}
예제 #12
0
int main()
{
	BinarySearchTree<Interval> tree;
	tree.add(Interval(1, 3)).add(Interval(6, 10)).add(Interval(4, 6))
		.add(Interval(10, 20)).add(Interval(6, 9)).add(Interval(1, 2));

	Interval span(MAX_DOUBLE, MIN_DOUBLE);
	for(Interval interval : tree)
	{
		span.start = min(span.start, interval.start);
		span.end = max(span.end, interval.end);
	}
	cout << span << endl;

	return 0;
}
예제 #13
0
bool readInput(ifstream& inputFile, const string& filename, vector<soundtrack>& cdVector, BinarySearchTree<ItemType>& BST)
{
	inputFile.open(filename);
	if (inputFile.fail())
		return false;
	else
	{
		while (!inputFile.eof())
		{
			soundtrack* cd = new soundtrack;
			inputFile >> *cd;
			cdVector.push_back(*cd);
			BST.add(cd->getDateReleased());
			delete cd;
			cd = nullptr;
		}
		inputFile.close();
	}
		return true;
} // end readInput
예제 #14
0
int main() {
  BinarySearchTree<BSTNode1<int>, int> tree;
   tree.add(15);
   tree.add(10);
   tree.add(14);
   tree.add(17);
   tree.add(20);
   tree.add(16);
   tree.add(19);
   tree.add(26);
   tree.add(7);
   tree.add(9);
   tree.add(5);

  return 0;
}
예제 #15
0
파일: test.cpp 프로젝트: nihy/cse274b
bool TestPartOne() {
  srand(time(0));
  BinarySearchTree<BSTNode1<int>, int> tree;
  tree.add(15);
  tree.add(10);
  tree.add(14);
  tree.add(17);
  tree.add(20);
  tree.add(16);
  tree.add(19);
  tree.add(26);
  tree.add(7);
  tree.add(9);
  tree.add(5);

  return (tree.height() == tree.Height2()) ? true : false;
}
예제 #16
0
파일: test.cpp 프로젝트: rakhlei/cse274b
void testPart4() {
	cout << "Part 4" << endl;
		BinarySearchTree<BSTNode1<int>, int> tree;
		tree.add(15);
		tree.add(5);
		tree.add(3);
		tree.add(12);
		tree.add(10);
		tree.add(6);
		tree.add(7);
		tree.add(13);
		tree.add(16);
		tree.add(20);
		tree.add(18);
		tree.add(23);
		cout << "Test 1:" << endl;
		DLList<int> treeTester1 = tree.getLE(5);
		cout << "Test 2:" << endl;
		DLList<int> treeTester2 = tree.getLE(12);
		cout << "Test 3:" << endl;
		DLList<int> treeTester3 = tree.getLE(18);
		cout << "Test 4:" << endl;
		DLList<int> treeTester4 = tree.getLE(23);
		cout << "Test 5:" << endl;
		DLList<int> treeTester5 = tree.getLE(50);

		BinarySearchTree<BSTNode1<int>, int> tree2;
		tree2.add(10);
		tree2.add(5);
		tree2.add(3);
		tree2.add(7);
		tree2.add(20);
		tree2.add(15);
		tree2.add(22);
		cout << "Test 6:" << endl;
		DLList<int> tree2Tester = tree2.getLE(20);

}
예제 #17
0
파일: test.cpp 프로젝트: nihy/cse274b
bool TestPartFour() {
  BinarySearchTree<BSTNode1<int>, int> tree;
    tree.add(15);
    tree.add(10);
    tree.add(14);
    tree.add(17);
    tree.add(20);
    tree.add(16);
    tree.add(19);
    tree.add(26);
    tree.add(7);
    tree.add(9);
    tree.add(5);

    DLList<int> list1 = tree.getLE(17);
    DLList<int> list2 = tree.getLE(9);

    return ((list1.size() == 8 && list2.size() == 3)) ? true : false;
}
예제 #18
0
파일: test.cpp 프로젝트: rakhlei/cse274b
void testPart3() {
	cout << "Part 3" << endl;
	BinarySearchTree<BSTNode1<int>, int> tree;
	tree.add(15);
	tree.add(5);
	tree.add(3);
	tree.add(12);
	tree.add(10);
	tree.add(6);
	tree.add(7);
	tree.add(13);
	tree.add(16);
	tree.add(20);
	tree.add(18);
	tree.add(23);
	cout << "pre order: " << endl;
	tree.preOrderNumber();
	cout << endl;
	cout << "in order: " << endl;
	tree.inOrderNumber();
	cout << "post order: " << endl;
	tree.postOrderNumber();

}
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;
		}
	}
}
예제 #20
0
파일: main.cpp 프로젝트: Hitesh-jaarwis/DFS
//-----------------------------END of BinarySearchTree Class---------------------------------//
// START of Main-----
int main(int argc, const char * argv[]) {
    
    BSTNode n1 (137);
    BSTNode n2 (122);
    //    n1.setLeft (&n2);
    BSTNode n3 (116);
    //    n2.setLeft (&n3);
    BSTNode n4 (120);
    //    n3.setRight (&n4);
    //    n1.print ();
    //    cout << endl;
    //    n2.print ();
    //    cout << endl;
    //    n3.print ();
    //    cout << endl;
    //    n4.print ();
    //    cout << endl;
    
    int arr[1000];
    arr[1000] = *storeRandomNumbersInArray(arr);
    
    //    int numberToSeed = 0;
    //    cout<<"Enter random number to seed:\n";
    //    cin>>numberToSeed;
    
    BinarySearchTree tree;
    
    //add array of random numbers to Binary Search Tree.
    for (int i = 0; i<1000; i++) {
        tree.add (arr[i]);
    }
    
    
    cout<<"i    BFS    DFS\n";
    for (int i = 1; i <= 10; i++) {
        int travCount = tree.breadthFirstTraversal (i*100);
        cout<<i<<"    "<<travCount<<endl;
    }
    
    //    cout<<"Traversal Count:"<<tree.breadthFirstTraversal (759)<<endl;
    
    return 0;
    
    
    /*
     //Working on UnorderedLinkedList & Stack and Queues.
     BSTNode *ptr = &n1;
     
     UnorderedLinkedList l1;
     l1.addAtHead (&n1);
     l1.print ();
     l1.addAtTail(&n2);
     l1.print ();
     l1.addAtTail(&n3);
     l1.print ();
     l1.addAtHead(&n4);
     l1.print ();
     cout<<endl;
     l1.removeFromHead(ptr);
     l1.print ();
     
     */
    
    return 0;
}//end main
예제 #21
0
#include "catch.hpp"
#include "../DataStructs/BinarySearchTree.h"
using namespace std;
using namespace dw;

BinarySearchTree<int> emptyTree;
BinarySearchTree<int> intTree;
BinarySearchTree<string> stringTree;

TEST_CASE("Add item test.", "[BST]")
{
   bool isAdded = intTree.add(20);
   REQUIRE(isAdded == true);
   isAdded = intTree.add(30);
   REQUIRE(isAdded == true);
   isAdded = intTree.add(40);
   REQUIRE(isAdded == true);
   isAdded = intTree.add(15);
   REQUIRE(isAdded == true);
   isAdded = intTree.add(35);
   REQUIRE(isAdded == true);
   isAdded = intTree.add(25);
   REQUIRE(isAdded == true);
   isAdded = intTree.add(5);
   REQUIRE(isAdded == true);
   isAdded = intTree.add(2);
   REQUIRE(isAdded == true);
   isAdded = intTree.add(7);
   REQUIRE(isAdded == true);
   isAdded = intTree.add(17);
   REQUIRE(isAdded == true);
예제 #22
0
파일: test.cpp 프로젝트: ColeHoff7/cse274d
int main() {
	 BinarySearchTree<BSTNode1<int>, int>* tree = new BinarySearchTree<BSTNode1<int>, int>();
  tree->add(10);
  tree->add(12);
  tree->add(3);
  tree->add(9);
  tree->add(7);
  tree->add(2);
  tree->add(43);
  tree->add(4);
  tree->add(29);
  tree->add(37);

  testPart1(tree);
  cout << endl;

  BinarySearchTree<BSTNode1<int>, int>* tree2 = new BinarySearchTree<BSTNode1<int>, int>();
  tree2->add(5);
  tree2->add(4);
  tree2->add(6);
  tree2->add(10);
  tree2->add(11);

  testPart2(tree2);
  cout << endl;

  BinarySearchTree<BSTNode1<int>, int>* tree3 = new BinarySearchTree<BSTNode1<int>, int>();
  tree3->add(5);
  tree3->add(4);
  tree3->add(6);

  testPart2(tree2);
  cout << endl;

  BinarySearchTree<BSTNode1<int>, int>* tree4 = new BinarySearchTree<BSTNode1<int>, int>();
  tree4->add(5);
  tree4->add(12);
  tree4->add(3);
  tree4->add(4);
  tree4->add(9);
  tree4->add(7);
  tree4->add(2);
  tree4->add(13);

  testPart3(tree4);
  cout << endl;

  BinarySearchTree<BSTNode1<int>, int>* tree5 = new BinarySearchTree<BSTNode1<int>, int>();
  tree5->add(10);
  tree5->add(11);
  tree5->add(13);
  tree5->add(9);
  tree5->add(6);
  tree5->add(2);
  tree5->add(3);

  testPart4(tree5);

  delete tree;
  delete tree2;
  delete tree3;
  delete tree5;
  delete tree4;

  return 0;
}
예제 #23
0
파일: 137108.cpp 프로젝트: sac0/DS_sem_2_1
int main()
{
    BinarySearchTree bst;
    bstNode* t = bst.Root();
    hcnode *h = NULL;
    hcnode a, b, c;
    int g=0;
    int t1=1;
    cout<<"enter '.' "<<"to end input"<<endl;
    while(t1)
    {                                                  //loop for input values
        cout<<"Enter char: ";
        cin>>a.c;
        if(a.c=='.')
        break;
        cout<<"Enter freq: ";
        cin>>a.freq;
        a.left = NULL;
        a.right = NULL;
        bst.add(t,a);
        g++;
    }
    if(g==1)                                          //if only one node is entered there is no traversal as why a code there is only one char;
    {
    	cout<<"only one element "<<endl;
    	exit(1);
    }

    while(!bst.check(t))                              //check upon number of nodes and finally only one bstnode remains with the hcnode with all data
    {
        a = bst.minimum(t);                           //gets first minimum hcnode from bst
        bst.del(t,a);                                //deletes the mininmum node from bst as it is already accessed
        b = bst.minimum(t);                           //next min node
        bst.del(t,b);                                 //del last node ie it is in hcnode b
        c = bst.minimum(t);                           //next min node
        bst.del(t,c);                                 //del last node ie it is in hcnode c
        add(h,a,b,c);                                 //adds the three nodes ie create a combined node
        bst.add(t,*h);                                //adds combined node to bst
    }
    int count=0;
    inorderc(t,count);                                    //if count is two add function has only two hcnodes
    if(count==2)
    {
    	if(t->lchild!=NULL)
    	{
    		add(h,t->data,t->lchild->data);               //h=node;a=node with max freq;b=node with less freq(h,a,b)
    	}
    	if(t->rchild!=NULL)
    	{
    		add(h,t->rchild->data,t->data);
    	}
    }
    hcnode*f=h;
    cout<<endl<<"printing level order "<<endl;
    levelorder(h);
    cout<<endl;
    string s;
    cout<<endl<<"Enter string: ";
    cin>>s;
    for(int i=0;i<s.length();i++)
    {
        if(s[i]=='1')
        {
            f = f->mid;
        }
        else if(s[i]=='0')
        {
            f = f->left;
        }
        else
        {
        	f=f->right;
        }
        if(f->left==NULL && f->right==NULL&&f->mid==NULL)
        {
            cout<<f->c;
            f = h;
        }
    }
    return 0;
}