Question

Write a program in C fro Binary Search tree using following functions 1. Insertion operation using...

Write a program in C fro Binary Search tree using following functions

1. Insertion operation using recursion

2. Deletion operation

3. Minimum/Maximum of a BST

6. Reorganize the tree so that the tree height is minimum

7. Print all the nodes from the node to the path to the root

8. Find the lowest common shared node between two given nodes

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

All functionality implemented without no. 6

if its not running on your machine(because of un-availability of some library run on

https://www.onlinegdb.com/online_c++_compiler#

code

#include<stdio.h>
#include<stdlib.h>
#include<vector>
#include<iostream>
#include <bits/stdc++.h>
using namespace std;

struct node
{
    struct node *left, *right;
    int data;
  
};
void printPathsRecur(struct node* node, int path[], int pathLen);
void printArray(int ints[], int len);
//A function that create nee node
struct node *newNode(int item)
{
    struct node *temp = (struct node *)malloc(sizeof(struct node));
    temp->data = item;
    temp->left = temp->right = NULL;
    return temp;
}

// get inorder traversal
void inorder(struct node *root)
{
    if (root != NULL)
    {
        inorder(root->left);
        cout<<" "<<root->data;
        inorder(root->right);
    }
}

/* insert new node using recursion */
struct node* insert(struct node* node, int data)
{
    /* If the node is empty, return a new node */
    if (node == NULL) return newNode(data);

    /* Otherwise, call Recursive */
    if (data < node->data)
        node->left = insert(node->left, data);
    else
        node->right = insert(node->right, data);

    /* return node */
    return node;
}

struct node * minValueNode(struct node* node)
{
    struct node* current = node;

    /* loop down to find the leftmost leaf */
    while (current->left != NULL)
        current = current->left;

    return current;
}

/* Given a binary search tree and a data, this function deletes the data
   and returns the new root */
struct node* deleteNode(struct node* root, int data)
{
    // base case
    if (root == NULL) return root;

    // If the data to be deleted is smaller than the root's data,
    // then it lies in left subtree
    if (data < root->data)
        root->left = deleteNode(root->left, data);

    // If the data to be deleted is greater than the root's data,
    // then it lies in right subtree
    else if (data > root->data)
        root->right = deleteNode(root->right, data);

    // if data is same as root's data, then This is the node
    // to be deleted
    else
    {
        // node with only one child or no child
        if (root->left == NULL)
        {
            struct node *temp = root->right;
            free(root);
            return temp;
        }
        else if (root->right == NULL)
        {
            struct node *temp = root->left;
            free(root);
            return temp;
        }

        // node with two children: Get the inorder successor (smallest
        // in the right subtree)
        struct node* temp = minValueNode(root->right);

        // Copy the inorder successor's content to this node
        root->data = temp->data;

        // Delete the inorder successor
        root->right = deleteNode(root->right, temp->data);
    }
    return root;
}
// find min value
int minValue(struct node* node) {
struct node* curr = node;
while (curr->left != NULL) {
    curr = curr->left;
}
return(curr->data);
}
// find max value
int maxValue(struct node* node)
{  
    /* loop down to find the rightmost leaf */
    struct node* current = node;
    while (current->right != NULL)
        current = current->right;
    
    return (current->data);
}
// check is leaf node or not
bool isLeaf(struct node* node)
{
   return (node->left == nullptr && node->right == nullptr);
}

// Print path from leaf to root
void printPath(vector<int> const &path)
{
   for (int k = path.size() - 1; k > 0; k--)
       cout << path.at(k) << " -> ";

   cout << path.at(0) << endl;
}
void LeafToRootPaths(struct node* node, vector<int> &path)
{
  
   if (node == nullptr)
       return;

   path.push_back(node->data);

   if (isLeaf(node))
       printPath(path);

   LeafToRootPaths(node->left, path);
   LeafToRootPaths(node->right, path);

   // remove current node after left and right subtree are done
   path.pop_back();
}

// print all paths from leaf to root node
void LeafToRootPaths(struct node* node)
{

   vector<int> path;
   // recursive function
   LeafToRootPaths(node, path);
}

bool findPath(struct node *root, vector<int> &Path, int j)
{
    // base case
    if (root == NULL) return false;

    // Store this node in path vector. The node will be removed if
    // not in path from root to k
    Path.push_back(root->data);

    // See if the k is same as root's data
    if (root->data == j)
        return true;

    // Check if k is found in left or right sub-tree
    if ( (root->left && findPath(root->left, Path, j)) ||
         (root->right && findPath(root->right, Path, j)) )
        return true;

    Path.pop_back();
    return false;
}

int findLCA(struct node *root, int l1, int l2)
{
    // to store paths to n1 and n2 from the root
    vector<int> path1, path2;

    if ( !findPath(root, path1, l1) || !findPath(root, path2, l2))
          return -1;

    /* paths to get the first different value */
    int k;
    for (k = 0; k < path1.size() && k < path2.size() ; k++)
        if (path1[k] != path2[k])
            break;
    return path1[k-1];
}

// main function

int main()
{
cout<<"Inserting node in tree \n";
   
    struct node *root = NULL;
    root = insert(root, 50);
    root = insert(root, 30);
    root = insert(root, 20);
    root = insert(root, 40);
    root = insert(root, 70);
    root = insert(root, 60);
    root = insert(root, 80);

    cout<<"After inserting node Inorder traversal of the given tree is \n";
    inorder(root);

    cout<<"\n Deleting node\n";
    cout<<"\nDelete 50\n";
    root = deleteNode(root, 50);
    cout<<"\n Inorder traversal of the modified (Deleted 50) tree \n";
    inorder(root);
  
    cout<<"\n Minimum value Tree is "<< minValue(root);
    cout<<"\n Maximum value in Tree is "<< maxValue(root);
    cout<<"\n Getting all path from leaf to root";
    cout<<"\n path is \n";
    LeafToRootPaths(root);
    cout<<"Lowest common shared node between two given nodes (20,40)";
cout << "nLCA(20, 40) = " << findLCA(root, 20, 40);




    return 0;
}

/*

output

Inserting node in tree                                                                                                                 

After inserting node Inorder traversal of the given tree is                                                                            

20 30 40 50 60 70 80                                                                                                                  

Deleting node                                                                                                                         

                                                                                                                                       

Delete 50                                                                                                                              

                                                                                                                                       

Inorder traversal of the modified (Deleted 50) tree                                                                                   

20 30 40 60 70 80                                                                                                                     

Minimum value in BST is 20                                                                                                            

Maximum value in BST is 80                                                                                                            

getting all path from leaf to root                                                                                                    

path is                                                                                                                               

20 -> 30 -> 60                                                                                                                         

40 -> 30 -> 60                                                                                                                         

80 -> 70 -> 60                                                                                                                         

Lowest common shared node between two given nodes (20,40)nLCA(20, 40) = 30         

*/

Add a comment
Know the answer?
Add Answer to:
Write a program in C fro Binary Search tree using following functions 1. Insertion operation using...
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
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