Question

PLEASE implement all the empty methods in BTree.c (only four functions) in C. #include #include struct...

PLEASE implement all the empty methods in BTree.c (only four functions) in C.

#include 
#include

struct Node {
     int value;
     struct Node* leftChild;
     struct Node* rightChild;
};

struct bTree {
     struct Node * root;
};

//please implement all the four functions
struct bTree* newTree (int value) {
        return NULL
}

int add (struct bTree* root, int value) {
        return 1;   
}

//you need to use free() to release the memory when a node is removed
int removeNode (struct bTree* root, int value) {
        return 1;
}

int contain (struct bTree* root, int value) {
        return 1;
}



void printInorder(struct Node* node) 
{ 
    if (node == NULL) 
        return; 
  
    printInorder(node->leftChild); 
  

    printf("%d ", (node->value));
  

    printInorder(node->rightChild); 
}

int printTree (struct bTree* root) {
        printInorder(root->root);
        printf("\n");
}

int main () {
        struct bTree* root = newTree(72);
        add(root, 12);
        add(root, 52);
        add(root, 87);
        add(root, 18);
        add(root, 49);
        add(root, 43);
        add(root, 82);
        add(root, 34);
        add(root, 73);
        add(root, 21);

        printTree(root);

        if(contain(root, 73) == 1){
                printf("This tree contains 73\n");
        }else{
                printf("This tree doesn't contains 73\n");
        }
        
        if(contain(root, 22) == 1){
                printf("This tree contains 22\n");
        }else{
                printf("This tree doesn't contains 22\n");
        }

        removeNode(root, 18);
        printf("After delete 18\n");
        printTree(root);

        removeNode(root, 49);
        printf("After delete 49\n");
        printTree(root);
        return 0;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

/******************************************************************************

Online C Compiler.
Code, Compile, Run and Debug C program online.
Write your code in this editor and press "Run" button to compile and execute it.

*******************************************************************************/

#include<stdio.h>
#include<stdlib.h>

struct Node {
int value;
struct Node* leftChild;
struct Node* rightChild;
};

struct bTree {
struct Node * root;
};

struct Node * new_node(int value){
struct Node *node;
node = malloc(sizeof(struct Node));
node->value = value;
node->leftChild = NULL;
node->rightChild = NULL;
  
return node;
}

//please implement all the four functions
struct bTree* newTree (int value) {
struct bTree *p;
p = malloc(sizeof(struct bTree));
p->root = new_node(value);
/*p->root = malloc(sizeof(struct Node));
p->root->value = value;
p->root->leftChild = NULL;
p->root->rightChild = NULL;*/

return p;
}

struct Node* insert(struct Node *node, int x)
{
if(node==NULL)
return new_node(x);
else if(x > node->value) // x is greater. Should be inserted to right
node->rightChild = insert(node->rightChild, x);
else // x is smaller should be inserted to left
node->leftChild = insert(node->leftChild, x);
return node;
}

int add (struct bTree* root, int value) {
  
   insert(root->root, value);
   return 1;
}

struct Node* find_minimum(struct Node *root)
{
if(root == NULL)
return NULL;
else if(root->leftChild != NULL) // node with minimum value will have no left child
return find_minimum(root->leftChild); // left most element will be minimum
return root;
}

struct Node* delete(struct Node *root, int x)
{
//searching for the item to be deleted
if(root==NULL)
return NULL;
if (x > root->value)
root->rightChild = delete(root->rightChild, x);
else if(x<root->value)
root->leftChild = delete(root->leftChild, x);
else
{
//No Children
if(root->leftChild==NULL && root->rightChild==NULL)
{
free(root);
return NULL;
}

//One Child
else if(root->leftChild==NULL || root->rightChild==NULL)
{
struct Node *temp;
if(root->leftChild==NULL)
temp = root->rightChild;
else
temp = root->leftChild;
free(root);
return temp;
}

//Two Children
else
{
struct Node *temp = find_minimum(root->rightChild);
root->value = temp->value;
root->rightChild = delete(root->rightChild, temp->value);
}
}
return root;
}

//you need to use free() to release the memory when a node is removed
int removeNode (struct bTree* root, int value) {
       delete(root->root, value);
return 1;
}

struct Node *parent;
struct Node * search(struct Node *root, int data)
{
if(root == NULL) {
return 0;
} else if (root->value == data) {
return root;
} else if (data > root->value) {
parent = root;
search(root->rightChild, data);
} else (data < root->value) {
parent = root;
search(root->leftChild, data);
}
}

int contain (struct bTree* root, int value) {
  
struct Node *temp = search(root->root, value);
if(temp == NULL)
return 0;
else
return 1;
}

void printInorder(struct Node* node)
{
if (node == NULL)
return;
  
printInorder(node->leftChild);
  

printf("%d ", (node->value));
  

printInorder(node->rightChild);
}

int printTree (struct bTree* root) {
printInorder(root->root);
printf("\n");
}

int main () {
struct bTree* root = newTree(72);
add(root, 12);
add(root, 52);
add(root, 87);
add(root, 18);
add(root, 49);
add(root, 43);
add(root, 82);
add(root, 34);
add(root, 73);
add(root, 21);

printTree(root);

if(contain(root, 73) == 1){
printf("This tree contains 73\n");
}else{
printf("This tree doesn't contains 73\n");
}
  
if(contain(root, 22) == 1){
printf("This tree contains 22\n");
}else{
printf("This tree doesn't contains 22\n");
}

removeNode(root, 18);
printf("After delete 18\n");
printTree(root);

removeNode(root, 49);
printf("After delete 49\n");
printTree(root);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
PLEASE implement all the empty methods in BTree.c (only four functions) in C. #include #include struct...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Need this in C++ Goals: Your task is to implement a binary search tree of linked...

    Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...

  • From the code below with Binary Search Tree recurrence T(n)=? use the recursion method and substitution method to solve the recurrence. Find the tightest bound g(n) for T(n) you can for which T(n)= O(...

    From the code below with Binary Search Tree recurrence T(n)=? use the recursion method and substitution method to solve the recurrence. Find the tightest bound g(n) for T(n) you can for which T(n)= O(g(n)). Explain your answer and use recursion tree method. void insert(int data) { struct node *tempNode = (struct node*) malloc(sizeof(struct node)); struct node *current; struct node *parent; tempNode->data = data; tempNode->leftChild = NULL; tempNode->rightChild = NULL; //if tree is empty if(root == NULL) { root = tempNode;...

  • ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled...

    ​​​​​ You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...

  • ****find_last_node.c #include <stdio.h> #include "linked_list.h" int main(){    ...

    ****find_last_node.c #include <stdio.h> #include "linked_list.h" int main(){       struct node *linked_list = NULL;    linked_list = add_to_list(linked_list, 5, 'a');    linked_list = add_to_list(linked_list, 10, 'b');    linked_list = add_to_list(linked_list, 4, 'c');    linked_list = add_to_list(linked_list, 10, 'd');    linked_list = add_to_list(linked_list, 5, 'e');    linked_list = add_to_list(linked_list, 7, 'f');    linked_list = add_to_list(linked_list, 5, 'g');    linked_list = add_to_list(linked_list, 3, 'h');    int search_number;    printf("Enter number you want to search for:");    scanf("%d", &search_number);    struct node *last_node = find_last(linked_list, search_number);    if (last_node != NULL)    {        printf("Node found: value = %d and...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; }; - INPUT i k : Insert the node with the key value of k in...

  • Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each...

    Write a C program insertion and deletion functions for a max heap represented as a linked binary tree. Assume that each node has a parent field as well as the usual left child, right child, and key fields. -Condition : Do not use Array representation. Use the following structure. typedef struct node *treePointer; typedef struct node { int key; treePointer parent; treePointer leftChild, rightChild; };    - INPUT i k : Insert the node with the key value of k in...

  • C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node...

    C++: PLEASE HELP~!!! ~~~~~~~~ Implement bool AVLTree::deleteNode(string ss) method. Function deleteNode() tries to delete the node containing value ss. If there is no such node, it returns false. Otherwise, it deletes the node, check the balance of the tree, rebalance the tree if it is necessary. When you delete a node, consider three different scenarios: -The node is a leaf -The node has only ONE child subtree -The node has two child subtrees Important: When you implement this project, do...

  • #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode...

    #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode * left; TNode * right; TNode * parent; }; class BTree { public: //constructors and destructor BTree(); BTree(TNode *r);// initialize BTree with the root r. r is the only node. BTree(const BTree & t); // copy constructor BTree(const int *p, const int n);// similar to the copy constructor, but your input is of the array form. // input is given an array that denotes...

  • C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct...

    C PROGRAMMING #include <stdio.h> #include <stdlib.h> struct nodet { int data; struct nodet *link; }; struct nodet *makeAnode(int val) { struct nodet *box; box = malloc(sizeof(struct nodet) ); box->data = val; box->link = NULL; return box; } void printList(struct nodet *L) { struct nodet = *mov; mov = L; while(mov != NULL) { printf("%d ", mov->data); mov = mov->link; } printf("\n"); } // THIS SHOULD COUNT HOW MANY ITEMS (NODES) ARE IN THE LIST. int listLen(struct nodet **L) { int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT