예제 #1
0
int main(int argc, char* argv[]) {
  BinarySearchTree BST;
  assert(BST.Empty() == true);
  assert(BST.Insert(4) == true);
  assert(BST.Insert(5) == true);
  assert(BST.Insert(2) == true);
  assert(BST.Insert(3) == true);
  assert(BST.Insert(1) == true);
  assert(BST.Empty() == false);
  assert(BST.Erase(0) == false);
  assert(BST.Erase(2) == true);
  assert(BST.Erase(2) == false);
  assert(BST.Insert(4) == false);
  // should output 4
  assert(BST.GetRootVal() == 4);
  cout << BST.GetRootVal() << endl;
  assert(BST.Erase(4) == true);
  // should output 5
  assert(BST.GetRootVal() == 5);
  cout << BST.GetRootVal() << endl;
  assert(BST.Erase(5) == true);
  // should output 3
  assert(BST.GetRootVal() == 3);
  cout << BST.GetRootVal() << endl;
  assert(BST.Erase(3) == true);
  // should output 1
  assert(BST.GetRootVal() == 1);
  cout << BST.GetRootVal() << endl;
  assert(BST.Erase(1) == true);
  assert(BST.Empty() == true);
  return 0;
}
예제 #2
0
int main() {
    BinarySearchTree<int> tree;
    tree.Insert(5);
    std::cout << std::endl;
    tree.PrintBySibling();
    tree.Insert(4);
    tree.Insert(3);
    tree.Insert(6);
    std::cout << std::endl;
    tree.PrintBySibling();
    tree.Insert(2);
    tree.Insert(7);
    std::cout << std::endl;
    tree.PrintBySibling();

    return 0;
}
예제 #3
0
int main() {
    BinarySearchTree<int> tree;
    tree.Insert(5);
    Test(tree);
    Test1(tree);
    tree.Insert(4);
    tree.Insert(3);
    tree.Insert(6);
    Test(tree);
    Test1(tree);
    tree.Insert(2);
    Test(tree);
    Test1(tree);
    tree.Insert(7);
    Test(tree);
    Test1(tree);
    tree.PrintByLevel();
    return 0;
}
예제 #4
0
TEST(Treap , InsertFind)
{
    Treap* tr = new Treap(false);
    BinarySearchTree* bst = new BinarySearchTree(false);

    size_t n=5000;
    uint32_t* A = (uint32_t*)malloc(n*sizeof(uint32_t));
    for (size_t i=0; i<n; i++) A[i] = rand() % 5001;
    for (size_t i=0; i<n; i++) {
        tr->Insert(A[i]);
        bst->Insert(A[i]);
    }
    for (size_t i=0; i<n; i++) {
        CHECK(tr->Find(A[i]) == bst->Find(A[i]));
        CHECK(tr->Find(A[i]+1) == bst->Find(A[i]+1));
    }
    delete tr;
    delete bst;
    free(A);
}
예제 #5
0
int main(int argc, const char *argv[]){
	cout << "BST Traversals: " << endl;
	BinarySearchTree<int> tree;
	tree.Insert(5);
	tree.Insert(7);
	tree.Insert(9);
	tree.Insert(3);
	tree.Insert(2);
	tree.Insert(1);

	cout << "In Order: ";
	InOrder(tree.get_root());
	cout << endl;
	cout << "Pre Order: ";
	PreOrder(tree.get_root());
	cout << endl;
	cout << "Post Order: ";
	PostOrder(tree.get_root());
	cout << endl;
	cout << "In Order Stack: ";
	InOrderStack(tree.get_root());
	cout << endl;
	cout << "In Order Morris: ";
	InOrderMorris(tree.get_root());
	cout << endl;
	cout << "Breadth First: ";
	BreadthFirst(tree.get_root());
	cout << endl;
	cout << "Level Order: ";
	LevelOrder(tree.get_root());
	cout << endl;
	cout << endl;

	cout << "AVL tree Traversals: " << endl;
	BinarySearchTree<int> avl;
	avl.Insert(5);
	avl.Insert(7);
	avl.Insert(9);
	avl.Insert(3);
	avl.Insert(2);
	avl.Insert(1);

	cout << "In Order: ";
	InOrder(avl.get_root());
	cout << endl;
	cout << "Pre Order: ";
	PreOrder(avl.get_root());
	cout << endl;
	cout << "Post Order: ";
	PostOrder(avl.get_root());
	cout << endl;
	cout << "In Order Stack: ";
	InOrderStack(avl.get_root());
	cout << endl;
	cout << "In Order Morris: ";
	InOrderMorris(avl.get_root());
	cout << endl;
	cout << "Breadth First: ";
	BreadthFirst(avl.get_root());
	cout << endl;
	cout << "Level Order: ";
	LevelOrder(avl.get_root());
	cout << endl;

 	return 0;
}