Example #1
0
int main()
{
//    LinkedList<int> *l = new LinkedList<int>;
//    l->prependElement(3);
//    l->prependElement(5);
//    l->prependElement(6);
//    l->insertElement(5, new LinkedListElement<int>(12));
//    l->print();
//    l->deleteElement(5);
//    l->print();
//    std::cout << "============" << std::endl;
//    RBTree<int> t(5);
    RBTree<int> t;
    srand (1);
    for (int i = 0; i < 18; i++) {
//        t.insert(rand());
        t.insert(i);
    }
//    t.insert(40);
//    t.insert(60);
//    t.insert(-10);
//    t.insert(2);
//    t.insert(91);
//    t.insert(4);
//    t.insert(6);
//    t.insert(18);
//    t.insert(17);
//    t.insert(50);
    printPretty(&t, 1, 0, std::cout);
//    std::cout << t.min()->data() << std::endl;
//    std::cout << t.max()->data() << std::endl;
    t.deleteNode(t.find(11));
    printPretty(&t, 1, 0, std::cout);
//    t.print();
//    t.deleteNode(t.find(40), 0);
//    std::cout << "==" << std::endl;
//    t.print();

    return 0;
}
Example #2
0
void main()
{
	RBTree *t = new RBTree();
	int n;
	RBNode *node;
	char c;

	do
	{
		print_commands();
		c = getch();
		if (c == 0)
			c = getch();
		if (c != 27)
			printf("%c\n\n", c);
		switch (c)
		{
			case 'i':
				printf("Node to insert: ");
				scanf("%d", &n);
				t->addNode(new RBNode(n));
				printf("Node inserted.");
				break;
			case 's':
				printf("Node to search for: ");
				scanf("%d", &n);
				if (t->findNode(n, t->getRoot()) == NULL)
					printf("Can't find node with key %d.", n);
				else
					printf("Found a node with key %d.", n);
				break;
			case 'd':
				printf("Node to delete: ");
				scanf("%d", &n);
				node = t->findNode(n, t->getRoot());
				if (node == NULL)
					printf("Can't find node with key %d.", n);
				else
				{
					t->deleteNode(node);
					printf("Node %d deleted.", n);
				}
				break;
			case 'p':
				clrscr();
				printf("Tree is: \n\n");
				t->prettyPrint(t->getRoot());
				break;
			case 27:
				break;
			default:
				printf("Unknown command.");
				break;
		}
		if (c != 27)
		{
			printf("\n\nPress any key to continue.");
			if (getch() == 0)
				getch();
		}
	}
	while (c != 27);

	delete t;
}