Question

In: Computer Science

Write a program in C language that implements the logic of Binary Trees with the following...

Write a program in C language that implements the logic of Binary Trees with the following requirements:

1- Elements of the tree should be read from the user.

2- Keep the binary tree heigh-balanced that is the absloute value of ((hight of left sub-tree) - ( height of right sub-tree)) should not be greater than 1. If so, then the binary tree is no longer balanced.

Hint: The best approach is to maintain balance during insertion.

*Remember, we are talking about baisc binary trees not binary search trees. ONLY CODE IN C LANGUAGE IS ACCEPTED.

Solutions

Expert Solution

The complete code is provided below:

Screenshot of the code:

Sample Output:

Code To Copy:

//Include the header files.

#include <stdio.h>

#include<stdlib.h>

//Define the structure.

struct Tree_Node

{

    int key_value;

    struct Tree_Node *left_ptr;

    struct Tree_Node *right_ptr;

    int Tree_height;

};

//Define the function the compute

// the height of the tree.

int compute_height(struct Tree_Node *t_node)

{

    if (t_node == NULL)

        return 0;

    return t_node->Tree_height;

}

//Define the function to compute maximum.

int max(int a, int b)

{

    return (a > b)? a : b;

}

//Define a function that create a newnode.

struct Tree_Node* newNode(int key)

{

    struct Tree_Node* new_node = (struct Tree_Node*)

                        malloc(sizeof(struct Tree_Node));

    new_node->key_value   = key;

    new_node->left_ptr   = NULL;

    new_node->right_ptr = NULL;

    new_node->Tree_height = 1;

    return(new_node);

}

//Define the function used to rotate the tree at right.

struct Tree_Node *rightRotate(struct Tree_Node *y)

{

    struct Tree_Node *x = y->left_ptr;

    struct Tree_Node *T2 = x->right_ptr;

    // Perform rotation

    x->right_ptr = y;

    y->left_ptr = T2;

   

    y->Tree_height = max(compute_height(y->left_ptr), compute_height(y->right_ptr))+1;

    x->Tree_height = max(compute_height(x->left_ptr), compute_height(x->right_ptr))+1;

    return x;

}

//Define the function used to rotate the tree at left.

struct Tree_Node *leftRotate(struct Tree_Node *x)

{

    struct Tree_Node *y = x->right_ptr;

    struct Tree_Node *T2 = y->left_ptr;

    //Perform the rotation in the tree.

    y->left_ptr = x;

    x->right_ptr = T2;

    //Update the height of the tree.

    x->Tree_height = max(compute_height(x->left_ptr), compute_height(x->right_ptr))+1;

    y->Tree_height = max(compute_height(y->left_ptr), compute_height(y->right_ptr))+1;

    // Return new root

    return y;

}

// Get Balance factor of node N

int getBalance(struct Tree_Node *N)

{

    if (N == NULL)

        return 0;

    return compute_height(N->left_ptr) - compute_height(N->right_ptr);

}

//Define the function to insert the element in the tree.

struct Tree_Node* Insert_element(struct Tree_Node* t_node, int key)

{

   

    if (t_node == NULL)

        return(newNode(key));

    if (key < t_node->key_value)

        t_node->left_ptr = Insert_element(t_node->left_ptr, key);

    else if (key > t_node->key_value)

        t_node->right_ptr = Insert_element(t_node->right_ptr, key);

    else

        return t_node;

  

    t_node->Tree_height = 1 + max(compute_height(t_node->left_ptr),

                           compute_height(t_node->right_ptr));

  

    int balance = getBalance(t_node);

    if (balance > 1 && key < t_node->left_ptr->key_value)

        return rightRotate(t_node);

  

    if (balance < -1 && key > t_node->right_ptr->key_value)

        return leftRotate(t_node);

   

    if (balance > 1 && key > t_node->left_ptr->key_value)

    {

        t_node->left_ptr = leftRotate(t_node->left_ptr);

        return rightRotate(t_node);

    }

   

    if (balance < -1 && key < t_node->right_ptr->key_value)

    {

        t_node->right_ptr = rightRotate(t_node->right_ptr);

        return leftRotate(t_node);

    }

    return t_node;

}

//Define the preorder traversal.

void Preorder_traversal(struct Tree_Node *root)

{

    if(root != NULL)

    {

        printf("%d ", root->key_value);

        Preorder_traversal(root->left_ptr);

        Preorder_traversal(root->right_ptr);

    }

}

//Define the main function.

int main()

{

//Create a root node of the tree.

struct Tree_Node *root_node = NULL;

//Insert the elements.

root_node = Insert_element(root_node, 10);

root_node = Insert_element(root_node, 20);

root_node = Insert_element(root_node, 30);

root_node = Insert_element(root_node, 40);

root_node = Insert_element(root_node, 50);

root_node = Insert_element(root_node, 25);

printf("Preorder Traversal : ");

//Call the function of preorder.

Preorder_traversal(root_node);

//Return the value for the main function.

return 0;

}


Related Solutions

(C++)Write a small program that implements the "fan tester" logic To recall: Logic in electronic devices....
(C++)Write a small program that implements the "fan tester" logic To recall: Logic in electronic devices. Most of the devices and appliances used in everyday life are controlled by electronic circuitry that works according to the laws of logic. A designer of an electronically-controlled device must first express the desired behavior in logic, and then test that the device behaves as desired under every set of circumstances. An electronic fan automatically turns on and off depending on the humidity in...
Write a MARIE program that implements the following logic. If X < Y Then X =...
Write a MARIE program that implements the following logic. If X < Y Then X = X + Y Else Y = 2X Assume the two numbers are X and Y and are entered by the user. Provide prompts to the user to enter the numbers and provide a meaningful output to the screen.
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in...
Write a C++ program that implements both the recursive binary and mergesort algorithms as described in zyBooks sections 9.4 and 9.5. Prompt the user for the location of a sequence of numbers, via an external file or data entry by the user. If you choose data entry, prompt the user for the number of values and read them into a data structure of your choice. Then use the mergesort algorithm to sort them in ascending order. Finally, prompt for a...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
In programming C language, write a program that creates a binary tree of words to be...
In programming C language, write a program that creates a binary tree of words to be used as a spell checking device for various text files. The list of words will come from a file “words.txt”. Your program is to read through the “words.txt” file and insert the word on that line into the tree in lexicographic order (also known as Dictionary order). Words in the file will be separated by spaces. Once this is done, your program should then...
Write a program in C language that uses a binary search algorithm to guess a number...
Write a program in C language that uses a binary search algorithm to guess a number from 1 to 100. The computer will keep guessing until they get the users number correct.
in C language only. Write a program called gamecards.c that has a card game logic by...
in C language only. Write a program called gamecards.c that has a card game logic by comparing the cards from 4 people and calculating which player has the best card. 1. Every card must be represented by exactly two chars representing a rank and a suit. ranks: '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K', 'A'. ('T' = 10) Suits: 'H', 'D', 'S', 'C'. 7D represents the “7 of Diamond” etc.. 2. Write a function called...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that...
Write an MSP430 assembly language program that implements the following algorithm: a macro called "vdot" that calculates the "dot product" of two vectors "a" and "b", implemented as “arrays” (following the “C” language convention), of 3 elements. the macro should receive 2 pointers to the first element of each vector and return the result in R13. I have another file where I save my vectors called, "vars.c" here I save: int a[] = { 14, 65, 9} int b[] =...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called 'numadd' that sums up all the numeric characters present in a phrase ("string" that follows the "C" language convention). By For example, if the phrase is "Today is the 28th of month 9", the subroutine must perform the following sum: 2 + 8 + 9 = 19. The subroutine must receive the address of the first character of the corresponding phrase in the "stack", and return...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called ‘numadd’ that...
Write an MSP430 assembly language program that implements the following algorithm: a subroutine, called ‘numadd’ that sums up all the numeric characters present in a sentence (“string” that follows the “C” language convention). For example, if the phrase is "Today is the 21st of month 5", the subroutine must perform the following sum: 2 + 1 + 5 = 8. The subroutine must receive the address of the first character of the corresponding phrase in the " stack ”, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT