In: Computer Science
C++ (function code)
Binary Search Trees (BST)
Code for height, min value, max value, counting node/leaves/full nodes
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.