Example #1
0
node  Delete_Element(tree t, node Parent, void * data, int *H, int * found, void * found_data)
{
    node Temp;
    if(!Parent)
    {
        printf("\n datarmation does not exist");
        return(Parent);
    }
    else
    {
        if (t->comp(data, Parent->data) < 0)
        {
            Parent->left = Delete_Element(t, Parent->left, data, H, found, found_data);
            if(*H)
                Parent = Balance_Right_Heavy(Parent, H);
        }
        else if(t->comp(data, Parent->data) > 0)
        {
            Parent->right = Delete_Element(t, Parent->right, data, H, found, found_data);
            if(*H)
                Parent = Balance_Left_Heavy(Parent, H);
        }
        else
        {
            *found = 1;
            *((int*)found_data) = (int) Parent->data;
            Temp= Parent;

            if(Temp->right == NULL)
            {
                Parent = Temp->left;
                *H = T;
                free(Temp);
            }
            else if(Temp->left == NULL)
            {
                Parent = Temp->right;
                *H = T;
                free(Temp);
            }
            else
            {
                Temp->left = DELETE(Temp->left, Temp, H);
                if(*H)
                    Parent = Balance_Right_Heavy(Parent, H);
            }
        }
    }
    return(Parent);
}
Example #2
0
int tree_compare(tree t1, tree t2) {
    if (t1->comp != t2->comp) return 1;
    if (t1->size != t2->size) return 1;

    list values1 = tree_to_list(t1);
    list values2 = tree_to_list(t2);

    int len = t1->size;
    int i = 0;

    for (i = 0; i < len; ++i) {
        void * p1 = list_get(values1, i);
        void * p2 = list_get(values2, i);
        if (t1->comp(p1,p2) != 0) {
            return 1;
        }
    }

    list_free(values1);
    list_free(values2);

    return 0;
}
Example #3
0
node Binary_Tree(tree t, void * data, node Parent, int *H, int * found)
{
    node Node1;
    node Node2;
    if(!Parent)
    {
        Parent = (node) malloc(sizeof(struct node));
        Parent->data = data;
        Parent->left = NULL;
        Parent->right = NULL;
        Parent->flag = 0;
        *H = T;
        return (Parent);
    }

    if(t->comp(data, Parent->data) < 0)
    {
        Parent->left = Binary_Tree(t, data, Parent->left, H, found);
        if(*H)
            /* Left branch has grown higher */
        {
            switch(Parent->flag)
            {
            case 1: /* Right heavy */
                Parent->flag = 0;
                *H = F;
                break;
            case 0: /* Balanced tree */
                Parent->flag = -1;
                break;
            case -1: /* Left heavy */
                Node1 = Parent->left;
                if(Node1->flag == -1)
                {
                    Parent->left= Node1->right;
                    Node1->right = Parent;
                    Parent->flag = 0;
                    Parent = Node1;
                }
                else
                {
                    Node2 = Node1->right;
                    Node1->right = Node2->left;
                    Node2->left = Node1;
                    Parent->left = Node2->right;
                    Node2->right = Parent;
                    if(Node2->flag == -1)
                        Parent->flag = 1;
                    else
                        Parent->flag = 0;
                    if(Node2->flag == 1)
                        Node1->flag = -1;
                    else
                        Node1->flag = 0;
                    Parent = Node2;
                }

                Parent->flag = 0;
                *H = F;
            }
        }
    }
    else if(t->comp(data, Parent->data) > 0)
    {
        Parent->right = Binary_Tree(t,data, Parent->right, H, found);
        if(*H)
            /* Right branch has grown higher */
        {
            switch(Parent->flag)
            {
            case -1: /* Left heavy */
                Parent->flag = 0;
                *H = F;
                break;
            case 0: /* Balanced tree */
                Parent->flag = 1;
                break;

            case 1: /* Right heavy */
                Node1 = Parent->right;
                if(Node1->flag == 1)
                {
                    Parent->right= Node1->left;
                    Node1->left = Parent;
                    Parent->flag = 0;
                    Parent = Node1;
                }
                else
                {
                    Node2 = Node1->left;
                    Node1->left = Node2->right;
                    Node2->right = Node1;
                    Parent->right = Node2->left;
                    Node2->left = Parent;

                    if(Node2->flag == 1)
                        Parent->flag = -1;
                    else
                        Parent->flag = 0;
                    if(Node2->flag == -1)
                        Node1->flag = 1;
                    else
                        Node1->flag = 0;
                    Parent = Node2;
                }

                Parent->flag = 0;
                *H = F;
            }
        }
    }
    else {
        *found = 1;
    }
    return(Parent);
}