Ejemplo n.º 1
0
 BinarySearchTree* find(int value) {
     if (data == value) {
         return this;
     } else if (value < data && left != NULL) {
         return left->find(value);
     } else if (data < value && right != NULL) {
         return right->find(value);
     } else {
         return NULL;
     }
 }
Ejemplo n.º 2
0
/**
 * PURPOSE:             Allows the user to search for their friends
 * PARAMETERS:          tree - the binary search tree that will be searched
 */
void findFriends(const BinarySearchTree& tree)
{
  cout << "Find Friends" << endl << endl;
  
  while (true)
  {
    cout << "Friend ID: " << flush;
    string friendId;
    getline(cin, friendId);
    
    const TreeNode* node = tree.find(friendId);
    if (node == 0)
    {
      cout << "Friend not found!" << endl;
    }
    else
    {
      const Friend* f = dynamic_cast<const Friend*>(node->element());
      cout << "Friend found!" << endl
           << (*f) << endl;
    }

    cout << endl << "Find another friend? [Y/n] " << flush;
    string answer;
    getline(cin, answer);
    if (answer.length() > 0 && tolower(answer[0]) == 'n')
    {
      break;
    }
  }
}
Ejemplo n.º 3
0
int main()
{
	{
		BinarySearchTree<int> t;
		t.insert(8);t.insert(3);t.insert(10);t.insert(1);
		t.remove(1);t.remove(3);
		t.insert(6);t.insert(14);t.insert(4);t.insert(7);t.insert(13);

        assert( t.find(13)->val == 13);
        assert( t.findMax()->val == 14);
        assert( t.findMin()->val == 4);

		t.traverse(printNode);
	}

	{
		BinarySearchTree<int> t;
		t.insert(8);t.insert(3);t.insert(10);t.insert(1);
		t.insert(6);t.insert(14);t.insert(4);t.insert(7);t.insert(13);

		t.traverse(printNode);
	}

	return 0;
}
int main()
{
    BinarySearchTree bst;
    int n;
    cin >> n;
    while(n){
        bst.insert(n);
        cin >> n;
    }
    bst.print();
    cin >> n;
    cout << bst.find(n);
    return 0;
}
Ejemplo n.º 5
0
bool test_bst()
{
	BinarySearchTree<int> bst;
	int lim = 1024, i;
	srand(time(NULL));
	for(i = 0; i < lim; i ++)
	{
		bst.insert(rand() % lim + 1);
	}
	if(bst.size() != lim)return false;
	int *sorted = bst.inorder();
	for(i = 1; i < lim; i++ )
	{
		if(sorted[i-1] > sorted[i]){return false;}
	}

	int n = sorted[rand() % (lim/2)];
	if(!bst.find(n)) return false;
	//bst.remove(n);
	sorted = bst.inorder();
	for(i = 1; i < bst.size(); i++ )
	{
		if(sorted[i-1] > sorted[i]){return false;}
	}
	BinarySearchTree<int> bst2;
	bst2.insert(6);
	bst2.insert(3);
	bst2.insert(8);
	bst2.insert(2);
	bst2.insert(0);
	bst2.insert(10);
	bst2.insert(5);
	bst2.insert(1);
	bst2.insert(4);
	bst2.insert(7);
	bst2.insert(9);


	i = 0;
	while(bst.size())
	{
		bst.remove(sorted[i++]);
	}
	return true;
}
int main()
{
    BinarySearchTree * bst = new BinarySearchTree();

    bst->insert(50, 5.0);
    bst->insert(25, 2.5);
    bst->insert(75, 7.5);
    bst->insert(12, 1.2);
    bst->insert(37, 3.7);
    bst->insert(43, 4.3);
    bst->insert(30, 3.0);
    bst->insert(33, 3.3);
    bst->insert(87, 8.7);
    bst->insert(93, 9.3);
    bst->insert(97, 9.7);

    cout << "\n--- traversal method with recursion ---\n";
    bst->traverse(1);
    bst->traverse(2);
    bst->traverse(3);

    cout << "\n--- traversal method without recursion ---\n";
    bst->travNoRecur(1);
    bst->travNoRecur(2);
    bst->travNoRecur(3);

    int key;
    while(cin >> key) {
	Node * pNode = bst->find(key);
	if(pNode != NULL) {
	    cout << "found node ";
	    pNode->displayNode();
	    cout << endl;
	}
	else
	    cout << "can't find node with key "
		 << key << endl;
    }

    bst->destroy();
    
    bst->traverse(2);

    return 0;
}
Ejemplo n.º 7
0
int main() {
    int n, v;
    char cmd[32];

    while (scanf("%d", &n) != EOF) {
        BinarySearchTree bst;
        for (int i = 0; i < n; ++i) {
            scanf("%s", cmd);
            if (!strcmp(cmd, "find")) {
                scanf("%d", &v);
                TreeNode *ptr = bst.find(v);
                if (!ptr) {
                    puts("null");
                } else {
                    printf("%x : %d\n", (unsigned)ptr, ptr->v);
                }
            } else if (!strcmp(cmd, "findMin")) {
                TreeNode *mini = bst.findMin();
                if (!mini) {
                    puts("null");
                } else {
                    printf("%x : %d\n", (unsigned)mini, mini->v);
                }
            } else if (!strcmp(cmd, "findMax")) {
                TreeNode *maxi = bst.findMax();
                if (!maxi) {
                    puts("null");
                } else {
                    printf("%x : %d\n", (unsigned)maxi, maxi->v);
                }
            } else if (!strcmp(cmd, "insert")) {
                scanf("%d", &v);
                bst.insert(v);
            } else if (!strcmp(cmd, "remove")) {
                scanf("%d", &v);
                bst.remove(v);
            } else if (!strcmp(cmd, "show")) {
                bst.show();
            }
        }
    }
    return 0;
}