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 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) { }
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write the code.
(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...
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 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: 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 i) { // implement } 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;...
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h....
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h. BST.h code below. CPP code also provided for reference, nothing to be changed on cpp code. #include #include "BSTNode.h" using namespace std; #ifndef BST_H_ #define BST_H_ class BST { public: BSTNode *root; int size; BST() { root = NULL; size = 0; } ~BST() { if (root != NULL) deepClean(root); } BSTNode *search(int key) { // complete this method } BSTNode *insert(int val) {...
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h....
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h. BST.h code below #include <iostream> #include "BSTNode.h" using namespace std; #ifndef BST_H_ #define BST_H_ class BST { public: BSTNode *root; int size; BST() { root = NULL; size = 0; } ~BST() { if (root != NULL) deepClean(root); } BSTNode *search(int key) { // complete this method } BSTNode *insert(int val) { // complete this method } bool remove(int val) { // complete this...
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h....
In C++, dealing with Binary Search trees. Implement search, insert, removeLeaf, and removeNodeWithOneChild methods in BST.h. BST.h code below #include <iostream> #include "BSTNode.h" using namespace std; #ifndef BST_H_ #define BST_H_ class BST { public: BSTNode *root; int size; BST() { root = NULL; size = 0; } ~BST() { if (root != NULL) deepClean(root); } BSTNode *search(int key) { // complete this method } BSTNode *insert(int val) { // complete this method } bool remove(int val) { // complete this...
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,...
in C++, given a binary search tree of ints, in which each node contains a size...
in C++, given a binary search tree of ints, in which each node contains a size parameter (also an int), explain, in English, not code, how you would find the median element of the tree in theta(log N) time.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT