int main(void)
{
    initializeTree();
    while(1)
    {
        printf("1. Insert item. 2. Delete item. 3. Search item. \n");
        printf("4. Print height of tree. 5. Print height of an item. \n");
        printf("6. PrintInOrder. 7. Range Search.\n");

        int ch;
        scanf("%d",&ch);
        if(ch==1)
        {
            int item;
            scanf("%d", &item);
            insertItem(root, item);
        }
        else if(ch==2)
        {
            int item;
            scanf("%d", &item);
            deleteItem(root, item);
        }
        else if(ch==3)
        {
            int item;
            scanf("%d", &item);
            struct treeNode * res = searchItem(root, item);
            if(res!=0) printf("Found.\n");
            else printf("Not found.\n");
        }
        else if(ch==4)
        {
            int height = calcNodeHeight(root);
            printf("Height of tree = %d\n", height);
        }
        else if(ch==5)
        {
            int item;
            scanf("%d", &item);
            int height = calcHeight(item);
            printf("Height of %d = %d\n", item, height);
        }
        else if(ch==6)
        {
            int h = calcNodeHeight(root);
            printf("\n--------------------------------\n");
            printInOrder(root, h);
            printf("--------------------------------\n");
        }
        else if(ch==7)
        {
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%d\n",rangeSearch(root,l,r));
        }
    }

}
Пример #2
0
int calcNodeHeight(struct treeNode * node) //return height of a node
{
    if(node==0) return -1;
    int l, r;
    l = calcNodeHeight(node->left);
    r = calcNodeHeight(node->right);
    if(l>r) return l+1;
    else return r+1;
}
Пример #3
0
int calcHeight(int item) //return height of an item in the tree
{
    struct treeNode * node = 0;
    node = searchItem(root, item);
    if(node==0) return -1; //not found
    else return calcNodeHeight(node);
}
int main(void)
{
    initializeTree();
    while(1)
    {
        printf("\n1. Insert item. 2. Delete item. 3. Search item. \n");
        printf("4. Print height of tree. 5. Print height of an item. \n");
        printf("6. PrintInOrder. 7. Calculate Depth 8. get MinItem. 9. get MaxItem.\n10. Range search. 11. Delete. 12. exit.\n");

        int ch;
        scanf("%d",&ch);
        if(ch==1)
        {
            int item;
            scanf("%d", &item);
            insertItem(root, item);
        }
        else if(ch==2)
        {
            int item;
            scanf("%d", &item);
            deleteItem(root, item);
        }
        else if(ch==3)
        {
            int item;
            scanf("%d", &item);
            struct treeNode * res = searchItem(root, item);
            if(res!=0) printf("Found.\n");
            else printf("Not found.\n");
        }
        else if(ch==4)
        {
            int height = calcNodeHeight(root);
            printf("Height of tree = %d\n", height);
        }
        else if(ch==5)
        {
            int item;
            scanf("%d", &item);
            int height = calcHeight(item);
            printf("Height of %d = %d\n", item, height);
        }
        else if(ch==6)
        {
            int h = calcNodeHeight(root);
            printf("\n--------------------------------\n");
            printInOrder(root, h);
            printf("--------------------------------\n");
        }
        else if(ch==12)
        {
            break;
        }
        else if(ch==7)
        {
            int i;
            scanf("%d",&i);
            printf("depth %d\n",calcDepth(i));
        }


        else if(ch==8)
        {
            printf("min item %d\n",getMinItem());
        }
        else if(ch==9)
        {
            printf("max item %d\n",getMaxItem());
        }
        else if(ch==10)
        {
            int item1,item2;
            scanf("%d%d", &item1,&item2);
            printf("Item number %d",rangeSearch(root,item1,item2));

        }
        else if(ch==11)
        {
            int a;
            scanf("%d",&a);
            deleteItem(root,a);
        }
    }

}
int main(void)
{
    //freopen("in2.txt", "r", stdin);
    initializeTree();
    while(1)
    {
        //printMenu();
        int ch;
        scanf("%d",&ch);
        if(ch == 1)
        {
            int item;
            scanf("%d", &item);
            insertItem(root, item);
        }
        else if(ch == 2)
        {
            int item;
            scanf("%d", &item);
            deleteItem(root, item);
        }
        else if(ch == 3)
        {
            int item;
            scanf("%d", &item);
            struct treeNode * res = searchItem(root, item);
            if(res!=0) printf("Found.\n");
            else printf("Not found.\n");
        }
        else if(ch == 4)
        {
            int height = calcNodeHeight(root);
            printf("Height of tree = %d\n", height);
        }
        else if(ch == 5)
        {
            int item;
            scanf("%d", &item);
            int height = calcHeight(item);
            printf("Height of %d = %d\n", item, height);
        }
        else if(ch == 6)
        {
            int h = calcNodeHeight(root);
            printf("\n--------------------------------\n");
            printInOrder(root, h);
            printf("--------------------------------\n");
        }
        else if(ch == 7)
        {
            printf("%d\n", getSize(root));
        }
        else if(ch == 8)
        {
            int item;
            scanf("%d", &item);
            printf("%d\n", calcDepth(item));
        }
        else if(ch == 9)
        {
            printf("%d\n", calcNodeDepth(root));
        }
        else if(ch == 10)
        {
            if(root == 0) {
                printf("tree is empty\n");
                continue;
            }
            printf("%d\n", getMinItem());
        }
        else if(ch == 11)
        {
            if(root == 0) {
                printf("tree is empty\n");
                continue;
            }
            printf("%d\n", getMaxItem());
        }
        else if(ch == 12)
        {
            int left, right;
            scanf("%d %d", &left, &right);
            printf("%d\n", rangeSearch(root, left, right));
        }
        else if(ch == 13)
        {
            int item;
            scanf("%d", &item);
            //printf("Del : %d\n", item);
            //deleteItem(root, item);
            printf("%d\n", deleteItem(root, item));
        }
        else if(ch == 14)
        {
            break;
        }
    }

}