Question

In: Computer Science

C++ (function code) Binary Search Trees (BST) Code for height, min value, max value, counting node/leaves/full...

C++ (function code)

Binary Search Trees (BST)

Code for height, min value, max value, counting node/leaves/full nodes

Solutions

Expert Solution

  C++ ( Function Code)

Binary Search Tree (BST)

Q.Write a Code for Height of Binary Search Tree in C++ ?

SOLUTION :

#include<iostream>
using namespace std;

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

struct node* getNode(int data)
{
   node* newNode=new node();
   newNode->data=data;
   newNode->left=NULL;
   newNode->right=NULL;
   return newNode;
}

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

struct node* Insert(struct node* root, int data)
{
   if (root == NULL)
       return getNode(data);

   if (data < root->data)
       root->left = Insert(root->left, data);
   else if (data > root->data)
       root->right = Insert(root->right, data);

   return root;
}

int FindHeight(node* root)
{
   if(root==NULL)
       return 0;

   else
   {

       int lb=FindHeight(root->left);
       int rb=FindHeight(root->right);
       return max(lb,rb)+1;
   }
}
int main()
{
   node* root=NULL;
   root=Insert(root,7);
   Insert(root,9);
   Insert(root,4);
   Insert(root,1);

   Insert(root,5);

   cout<<"Inorder: ";
   inorder(root);
   cout<<endl;

   cout<<"Height of the tree is "<<FindHeight(root)<<endl;
   cout<<"Max. Depth of the tree is "<<FindHeight(root)-1;
  
   return 0;
}
// Inorder : 14579

// Output : Height of Tree is 3.

Q. Write a code for Minimum Value of Binary Search Tree in C++ ?

SOLUTION :


#include <bits/stdc++.h>
using namespace std;

/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node
{
   int data;
   struct node* left;
   struct node* right;
};

/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
struct node* newNode(int data)
{
struct node* node = (struct node*)
                   malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

return(node);
}

/* Give a binary search tree and a number,
inserts a new node with the given number in
the correct place in the tree. Returns the new
root pointer which the caller should then use
(the standard trick to avoid using reference
parameters). */
struct node* insert(struct node* node, int data)
{
/* 1. If the tree is empty, return a new,
   single node */
if (node == NULL)
   return(newNode(data));
else
{
   /* 2. Otherwise, recur down the tree */
   if (data <= node->data)
       node->left = insert(node->left, data);
   else
       node->right = insert(node->right, data);

   /* return the (unchanged) node pointer */
   return node;
}
}

/* Given a non-empty binary search tree,
return the minimum data value found in that
tree. Note that the entire tree does not need
to be searched. */
int minValue(struct node* node)
{
struct node* current = node;

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

/* Driver Code*/
int main()
{
struct node* root = NULL;
root = insert(root, 4);
insert(root, 2);
insert(root, 1);
insert(root, 3);
insert(root, 6);
insert(root, 5);

cout << "\n Minimum value in BST is " << minValue(root);
getchar();
return 0;
}

// OUTPUT :

// Minimum Value in BST IS 1.

Q. Write a code for Maximum Value of Binary Search Tree in C++ ?

SOLUTION :

#include <bits/stdc++.h>
using namespace std;

/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct node {
   int data;
   struct node* left;
   struct node* right;
};

// Function to create a new node
struct node* newNode(int data)
{
   struct node* node = (struct node*)
       malloc(sizeof(struct node));
   node->data = data;
   node->left = NULL;
   node->right = NULL;

   return (node);
}

// Function to insert a new node in BST
struct node* insert(struct node* node, int data)
{
   /* 1. If the tree is empty, return a new,     
   single node */
   if (node == NULL)
       return (newNode(data));
   else {
       /* 2. Otherwise, recur down the tree */
       if (data <= node->data)
           node->left = insert(node->left, data);
       else
           node->right = insert(node->right, data);

       /* return the (unchanged) node pointer */
       return node;
   }
}

// Function to find the node with maximum value
// i.e. rightmost leaf node
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);
}

// Driver code
int main()
{
   struct node* root = NULL;
   root = insert(root, 4);
   insert(root, 2);
   insert(root, 1);
   insert(root, 3);
   insert(root, 6);
   insert(root, 5);

   cout << "Maximum value in BST is " << maxValue(root);

   return 0;
}

/ / OUTPUT :

// Maximum value in BST Is 6 .

Q. Write a code for Counting the Number of Nodes of BST IN C++ ?

SOLUTION :  

#include<iostream>
using namespace std;

int n=1;

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

struct node* getNode(int data)
{
   node* newNode=new node();
   newNode->data=data;
   newNode->left=NULL;
   newNode->right=NULL;
   return newNode;
}

struct node* Insert(struct node* root, int data)
{
   if (root == NULL)
       return getNode(data);
  
   if (data < root->data)
       root->left = Insert(root->left, data);
   else if (data > root->data)
       root->right = Insert(root->right, data);

   return root;
}


int CountNodes(node*root)
{
   if(root==NULL)
       return 0;
   if(root->left!=NULL)
   {
       n=n+1;
       n=CountNodes(root->left);
   }
   if(root->right!=NULL)
   {
       n=n+1;
       n=CountNodes(root->right);
   }
   return n;
}

int main()
{
   node* root=NULL;
   root=Insert(root,3);
   Insert(root,4);
   Insert(root,2);
   Insert(root,5);
   Insert(root,1);

   cout<<"Total No. of Nodes in the BST = "<<CountNodes(root)<<endl;

   return 0;
}

// OUTPUT :

// The Total Number of Nodes in BST IS 5.


Related Solutions

(IN C) Write the code to manage a Binary Tree. Each node in the binary tree...
(IN C) Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include: • Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node. • Find a node by integer value: This function takes in two...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key...
IN JAVA Given a binary search tree, extract min operation finds the node with minimum key and then takes it out of the tree. The program can be C++/Java or C-style pseudocode. Do not use function call to either Min or delete done in the class. Write this from scratch. (No need to ensure AVL properties, just show it for a generic BST) Node * extract min(Node * x) { }
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
Implement a function to remove a leaf node from a binary search tree. Using the following...
Implement a function to remove a leaf node from a binary search tree. Using the following class and function definition: class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode * remove_leaf(int item, bool &removed) { // implement } The remove function will return the node with the item if it's present in the tree. If the node is a leaf node and is removed by the function,...
Write the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string.
Programming CWrite the code to manage a Binary Tree. Each node in the binary tree includes an integer value and string. The binary tree is sorted by the integer value. The functions include:• Insert into the binary tree. This function will take in as parameters: the root of the tree, the integer value, and the string. Note that this function requires you to create the node.• Find a node by integer value: This function takes in two parameters: the root...
In MIPS assembly language, write a function that will display the max and min value in...
In MIPS assembly language, write a function that will display the max and min value in an array. Then write a function to calculate and display the average of all values in an array. This must be done in MIPS assembly language.
Write a C program/code that prompts the user for a minimum min and a maximum max....
Write a C program/code that prompts the user for a minimum min and a maximum max. Then use these values to print the squares of all even numbers between the min and max variables. For example if the user enters 6 as the minimum and 200 as the maximum, the program/code should print the following. Enter limit on minimum square: 6 Enter limit on maximum square: 200 36 64 100 144 196
c++ Binary Search Tree: find parent of a node please insert comment to explain what the...
c++ Binary Search Tree: find parent of a node please insert comment to explain what the code do. Use struct Node{ int data; Node* left; Node* right};
Write a program of Binary Search in C++ by using function and arrays with the explanation.
Write a program of Binary Search in C++ by using function and arrays with the explanation.
In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool&...
In c++ A binary search member function of SortedType has the following prototype: void.SortedType::BinarySearch(int value, bool& found); Write the function definition as a recursive search, assuming an array-based implementation. Add a main function as a driver to test the BinarySearch. Test the edge cases.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT