Ejemplo n.º 1
0
int main(int argc, const char * argv[]) {
    AVLTree b= create_tree();
    Insert(&b, 20, "jordan");
    Insert(&b, 42, "james");
    Delete(&b, 20);
    Insert(&b, 32, "jack");
    Insert(&b, 50, (void*)9);
    Insert(&b, 6, (void*)12);
    Insert(&b, 7, (void*)'j');
    Insert(&b, 4, (void*)'x');
    Insert(&b, 1, (void*)'v');
    Insert(&b, 100, "abby");
    Insert(&b, 90, "gabby");
    Insert(&b, 80, "grace");
    Insert(&b, 70, "david");
    Insert(&b, 39, "Chris");
    printf("pre-order\n");
    PrintPreOrder(&b);
    printf("in-order\n");
    PrintInOrder(&b);
    printf("post-order\n");
    PrintPostOrder(&b);
    printf("Distance from root %d\n",DistanceFromRoot(&b, 70));
    printf("tree height %d\n",TreeHeight(&b));
    printf("item: %s\n",Find(&b, 42));
    Delete(&b, 42);
    printf("item: %s\n",Find(&b, 39));
    printf("tree height %d\n",TreeHeight(&b));
    return 0;
}
Ejemplo n.º 2
0
int main(void)
{
    char* command = malloc(10);
    int item = 0;
    struct treeNode** root = malloc(sizeof(struct treeNode*));
    *root = NULL;

    for(;;)
    {
        printf("Command: ");
        scanf("%s", command);
        if(!strcmp(command, "insert"))
        {
            printf("Item: ");
            scanf("%d", &item);
            Insert(root, item);
        }
        else if(!strcmp(command, "print"))
        {
            int choice = 0;
            if(*root == NULL){printf("Cannot print an empty tree!\n");continue;}
            printf("Order (1-PreOrder; 2-InOrder; 3-PostOrder): ");
            scanf("%d", &choice);
            if(choice == 1)
            {
                if(PrintPreOrder(*root)){printf("Cannot print an empty tree!\n");}
            }
            else if(choice == 2)
            {
                if(PrintInOrder(*root)){printf("Cannot print an empty tree!\n");}
            }
            else if(choice == 3)
            {
                if(PrintPostOrder(*root)){printf("Cannot print an empty tree!\n");}
            }
            else
            {
                printf("Invalid Choice\n");
            }
        }
        else if(!strcmp(command, "find"))
        {
            printf("Item: ");
            scanf("%d", &item);

            struct treeNode* temp = FindItem(*root, item);

            if(temp == NULL)
            {
                printf("Item not found - not in tree\n");
            }
            else
            {
                printf("Item found - address of item is: [%p]\n", (void *)temp);
            }
        }
        else if(!strcmp(command, "delete"))
        {
            printf("Item: ");
            scanf("%d", &item);

            if(DeleteNode(root, item))
            {
                printf("Node not found - not deleted\n");
            }
            else
            {
                printf("Node deleted\n");
            }
        }
        else if(!strcmp(command, "quit"))
        {
            break;
        }
    }

    free(command);
    freeTree(*root);
    free(root);
}
Ejemplo n.º 3
0
	//two children
	if(temp->left != NULL && temp->right != NULL) {
		struct treeNode *dest = temp->left;
		struct treeNode *destPar = temp;
		while(dest->right != NULL) {