user233233
user233233

Reputation: 103

How I can balanced a tree into array

I have made a working binary tree.

Now I want when I press option # 4, it'll show binary tree is sorted according to the array (that means the middle should be root, the left part of the left subtree, then the right part of the field to the right subtree).

I have created functions required to do what it should be done but still it does not work as it should.

I have created NodSize(), it controls how many nodes are in the tree.

I have created balanceTree(), it takes all that is in the root.

I have created ArrayInorder(),

And Balance(), it makes balancing the tree according to array.

Where did I do wrong and how can I solve it?

..

You can see down here how I made it:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define MAX 100

struct tree
{
    int data;
    struct tree *left;
    struct tree *right;
};


struct tree *CreateNode(int data)
{
    struct tree *node = (struct tree*) malloc(sizeof(struct tree));
    if (node != NULL)
    {
        node->data = data;
        node->left = NULL;
        node->right = NULL;
    }
    return node;
}

struct tree *insert(struct tree *root, int data)
{
    if (root == NULL)
    {
        root = CreateNode(data);
    }
    if (root->data > data)
    {
        if (root->left == NULL)
        {
            root->left = CreateNode(data);
        }
        else
        {
            insert(root->left, data);
        }
    }
    else if (root->data < data)
    {
        if (root->right == NULL)
        {
            root->right = CreateNode(data);
        }
        else
        {
            insert(root->right, data);
        }
    }
    return root;
}


struct tree *delet(struct tree *ptr, int x)
{
    struct tree *p1, *p2;
    if (!ptr)
    {
        printf("\n NOTHING ");
        return(ptr);
    }
    else
    {
        if (ptr->data < x) 
        {
            ptr->right = delet(ptr->right, x); 

        }
        else if (ptr->data > x) 
        {
            ptr->left = delet(ptr->left, x); 
            return ptr;
        }
        else
        {
            if (ptr->data == x) 
            {
                if (ptr->left == ptr->right) 
                {
                    free(ptr);
                    return(NULL);
                }
                else if (ptr->left == NULL) 
                {
                    p1 = ptr->right;
                    free(ptr);
                    return p1;
                }
                else if (ptr->right == NULL) 
                {
                    p1 = ptr->left;
                    free(ptr);
                    return p1;
                }
                else
                {
                    p1 = ptr->right;
                    p2 = ptr->right;
                    while (p1->left != NULL)
                        p1 = p1->left;
                    p1->left = ptr->left;
                    free(ptr);
                    return p2;
                }
            }
        }
    }
    return(ptr);
}


void Findroot(struct tree *root)
{
    if (root != NULL)
    {
        printf("\nRoot is %d\n", root->data);
    }
    else
    {
        printf("\nNOTHING\n");
    }
}

void inorder(struct tree *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        printf(" %d", root->data);
        inorder(root->right);
    }
    return;
}

int NodSize(struct tree *root)
{
    if (root == NULL)
        return 0;
    else
        return (NodSize(root->left) + 1 + NodSize(root->right));
}

void DestoryTree(struct tree * root)
{
    if (root == NULL)
        return;

    DestoryTree(root->left);
    DestoryTree(root->right);


    printf("\n Destory node: %d", root->data);
    free(root);
}


struct tree *Balance(int arr[], int start, int end)
{
    if (start > end)
        return NULL;

    int mid = (start + end) / 2;
    struct tree *root = CreateNode(arr[mid]);

    root->left = Balance(arr, start, mid - 1);

    root->right = Balance(arr, mid + 1, end);
    return root;
}


int ArrayInorder(struct tree *root, int *arr, int helper)
{
    if (root != NULL)
    {
        helper = ArrayInorder(root->left, arr, helper);
        arr[helper] = root->data;
        helper++;
        helper = ArrayInorder(root->right, arr, helper);
    }
    return helper;
}


void balanceTree(struct tree *root)
{
    int *arr, size;
    size = NodSize(root);
    arr = (int*)malloc(sizeof(int)*size);
    ArrayInorder(root, arr, 0);

    //DestoryTree(root);
    root = Balance(arr, 0, size - 1);
}


void CreateBalancedTreeFromArray(struct tree *root, int *arr, int size)
{
    DestoryTree(root);
    root = Balance(arr, 0, size - 1);
}



int main(void)
{
    struct tree *root;
    int valja, item, dele;
    root = NULL;

    do
    {
        do
        {
            printf("\n1. Add a node to BINARY TREE ");
            printf("\n2. Delete a node from BINARY TREE ");
            printf("\n3. Show ROOT");
            printf("\n4. Show Balanced Tree IN (Array)");
            printf("\n5. Stang");
            printf("\nYour choice? : ");
            scanf(" %d", &valja);
            if (valja<1 || valja>5)
                printf("\n Fel - Try again");
        } while (valja<1 || valja>5);
        switch (valja)
        {
        case 1:
            system("cls");
            printf("\n Write a new element: ");
            scanf("%d", &item);
            root = insert(root, item);
            printf("\n root is %d \n", root->data);
            printf("INORDER: ");
            inorder(root);
            printf("\n");
            break;
        case 2:
            system("cls");
            printf("\n Write an element to delete: ");
            scanf(" %d", &dele);
            root = delet(root, dele);
            printf("\n");
            break;
        case 3:
            system("cls");
            Findroot(root);
            printf("\n");
            break;
        case 4:
            balanceTree(root);
            Findroot(root);
            inorder(root);
            break;
        default:
            printf("\n bye ");
        }
    } while (valja != 5);
    return(0);
}

Upvotes: 0

Views: 95

Answers (1)

JS1
JS1

Reputation: 4767

The problem is here:

void balanceTree(struct tree *root)

Although this function works, the new root that is created is NOT returned to the caller because root has been passed by value. You need to change the function to:

struct tree *balanceTree(struct tree *root)

and add this at the end:

return root;

Call the function with:

root = balanceTree(root);

Upvotes: 1

Related Questions