Exp No : 5(a)
Date : IMPLEMENTATION OF BINARY SEARCH TREE
AIM:
To write a C++ program to implement binary search tree.
PSEUDOCODE:
1. Start
2. insert(r, e)
3. if (r == NULL)
create new node
r==n
else if (e > r->data)
r->right = insert(r->right, e)
else
r->left = insert(r->left, e)
return r
4. inorder(r)
if (r != NULL)
inorder(r->left)
print r->data
inorder(r->right)
5. preorder(r)
if (r != NULL)
print r->data
preorder(r->left)
preorder(r->right)
6. postorder(r)
if (r != NULL)
postorder(r->left)
postorder(r->right)
print r->data
7. height(r)
if (r == NULL)
return 0
else
lh = height(r->left)
rh = height(r->right)
23CSR201 MANORANJITHAM 717824P231
if (lh > rh)
return lh + 1
return rh + 1
8. levelorder(root, level)
if (root == NULL)
return
if (level == 1)
print root->data
else if (level > 1)
levelorder(root->left, level - 1)
levelorder(root->right, level - 1)
9. Level_Order(root)
h = height(root)
for i = 0 to h
levelorder(root, i)
print newline
10.printleastvalue(root)
while (root->left != NULL)
root = root->left
print root->data
11.printGreatervalue(root)
while (root->right != NULL)
root = root->right
print root->data
12.search(r, key)
if (r == NULL)
return NULL
else if (key > r->data)
return search(r->right, key)
else if (key < r->data)
return search(r->left, key)
else
return r
13.Input n
14.for i = 0 to n-1
Input val
root = insert(root, val)
15.End
23CSR201 MANORANJITHAM 717824P231
SOURCE CODE:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node * left, *right;
node(int data)
{
this -> data = data;
this -> left = this -> right = NULL;
}
};
node * insert(node*r, int e)
{
node * n;
if(r==NULL)
{
n=new node(e);
r=n;
}
else if(e > r->data)
r -> right = insert(r->right,e);
else
r -> left = insert(r->left,e);
return r;
}
void inorder(node*r)
{
if(r!=NULL)
{
inorder(r -> left);
cout<<r -> data << " ";
inorder (r -> right);
}
}
void preorder(node*r)
{
23CSR201 MANORANJITHAM 717824P231
if(r!=NULL)
{
cout<<r -> data << " ";
preorder(r -> left);
preorder (r -> right);
}
}
void postorder(node*r)
{
if(r!=NULL)
{
postorder(r -> left);
postorder (r -> right);
cout<<r -> data << " ";
}
}
int height(node*r)
{
if(r == NULL)
return 0;
else
{
int lh = height(r->left);
int rh = height(r->right);
if(lh>rh)
return lh+1;
return rh+1;
}
}
void levelorder(node*root, int level)
{
if(root==NULL)
return;
if(level==1)
cout<<root->data<<" ";
else if(level>1)
{
levelorder(root->left,level-1);
levelorder(root->right,level-1);
}
}
23CSR201 MANORANJITHAM 717824P231
void Level_Order(node*root)
{
int h=height(root);
for(int i=0; i<=h; i++)
{
levelorder(root,i);
}
cout << endl;
}
void printleastvalue(node*root)
{
while(root->left!=NULL)
root=root->left;
cout<<root->data<<endl;
}
void printGreatervalue(node*root)
{
while(root->right!=NULL)
root=root->right;
cout<<root->data<<endl;
}
node * search(node*r, int key)
{
if(r==NULL)
return NULL;
else if(key>r->data)
return search(r->right,key);
else if(key<r->data)
return search(r->left, key);
else
return r;
}
int main()
{
int n, val;
cin>>n;
node * root = NULL;
for(int i=0; i<n; i++)
{
cin>>val;
root=insert(root,val);
}
cout<<"inorder:"<<endl;
23CSR201 MANORANJITHAM 717824P231
inorder(root);
cout << endl;
cout<<"Preorder:"<<endl;
preorder(root);
cout << endl;
cout<<"Postorder:"<<endl;
postorder(root);
cout << endl;
cout<<"Levelorder:"<<endl;
Level_Order(root);
cout << "Height: " << height(root) << endl;
cout<<"GreaterValue:"<<endl;
printGreatervalue(root);
cout<<"SmallerValue:"<<endl;
printleastvalue(root);
int key;
cout<<"Enter the element to be searched:"<<endl;
cin >> key;
cout << "Search: ";
node* result = search(root, key);
if (result == NULL)
cout << "Not Found" << endl;
else
cout << "The Element "<<result->data<<" is Found"<< endl;
return 0;
23CSR201 MANORANJITHAM 717824P231
OUTPUT:
23CSR201 MANORANJITHAM 717824P231
Exp No : 5(a) IMPLEMENTATION OF ADELSON-VELSKY AND
Date :
LANDIS TREE
AIM:
To write a C++ program to implement AVL tree.
PSEUDOCODE:
1. Start
2. height(N)
if(N==NULL)
return 0
return N->height
3. getBalance(N)
if(N==NULL)
return 0
return height(N->left)-height(N->right)
4. rightRotate(y)
x=y->left
T2=x->right
x->right=y
y->left=T2
y->height=max(height(y->left),height(y->right))+1
x->height=max(height(x->left),height(x->right))+1
return x
5. leftRotate(x)
y=x->right
T2=y->left
y->left=x
x->right=T2
x->height=max(height(x->left),height(x->right))+1
y->height=max(height(y->left),height(y->right))+1
return y
6. insert(r,key)
if(r==NULL)
n=new node(key)
r=n
else if(ke>r->data)
r->right=insert(r->right,key)
else if(key<r->data)
r->left=insert(r->left,key)
23CSR201 MANORANJITHAM 717824P231
r->height=1+max(height(r->left),height(r->right))
balance=getBalance(r)
if(balance>1&&key<r->left->data)
return rightRotate(r)
if(balance<-1&&key>r->right->data)
return leftRotate(r)
if(balance>1&&key>r->left->data)
r->left=leftRotate(r->left)
return rightRotate(r)
SOURCE CODE:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node *left,*right;
int height;
node(int data)
{
this->data=data;
this->left=this->right=NULL;
this->height=1;
}
};
int height(node *N)
{
if(N==NULL)
return 0;
return N->height;
}
int getBalance(node *N)
{
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
node *rightRotate(node *y)
{
node *x = y->left;
23CSR201 MANORANJITHAM 717824P231
node *T2 = x->right;
x->right = y;
y->left = T2;
y->height = max(height(y->left), height(y->right))+1;
x->height = max(height(x->left), height(x->right))+1;
return x;
}
node *leftRotate(node *x)
{
node *y = x->right;
node *T2 = y->left;
y->left = x;
x->right = T2;
x->height = max(height(x->left), height(x->right))+1;
y->height = max(height(y->left), height(y->right))+1;
return y;
}
node *insert(node *r,int key)
{
node *n;
if(r==NULL)
{
n=new node(key);
r=n;
}
else if(key>r->data)
r->right=insert(r->right,key);
else if(key<r->data)
r->left=insert(r->left,key);
r->height = 1 + max(height(r->left),height(r->right));
int balance = getBalance(r);
if (balance > 1 && key < r->left->data)
return rightRotate(r);
if (balance < -1 && key > r->right->data)
return leftRotate(r);
if (balance > 1 && key > r->left->data)
{
r->left = leftRotate(r->left);
return rightRotate(r);
}
if (balance < -1 && key < r->right->data)
{
23CSR201 MANORANJITHAM 717824P231
r->right = rightRotate(r->right);
return leftRotate(r);
}
return r;
}
void inorder(node *r)
{
if(r!=NULL)
{
inorder(r->left);
cout<<r->data<<" ";
inorder(r->right);
}
}
int main()
{
int n,val;
cin>>n;
node *root=NULL;
for(int i=0;i<n;i++)
{
cin>>val;
root=insert(root,val);
}
inorder(root);
cout<<endl;
}
23CSR201 MANORANJITHAM 717824P231
OUTPUT:
RESULT:
Thus, a C++ program to implement binary search trees (BST) and AVL trees was
successfully executed and the output was verified.
23CSR201 MANORANJITHAM 717824P231
23CSR201 MANORANJITHAM 717824P231
Exp No : 5(b) IMPLEMENTATION OF KTH SMALLEST
Date : ELEMENT IN A BINARY SEARCH TREE
AIM:
To write a C++ program to find the Kth Smallest Element in a Binary Search Tree
(BST).
PSEUDO CODE:
1. Start
2. Initialize: count = 0, ans = -1
3. kthSmallest(root, k)
if (root == NULL)
return
kthSmallest(root->left, k)
Increment count
if (count == k)
ans = root->data
kthSmallest(root->right, k)
4. insert(root, val)
if (root == NULL)
return new node(val)
if (val < root->data)
root->left = insert(root->left, val)
else
root->right = insert(root->right, val)
return root
5. End
23CSR201 MANORANJITHAM 717824P231
SOURCE CODE:
#include <iostream>
using namespace std;
class node {
public:
int data;
node *left, *right;
node(int val) {
data = val;
left = right = NULL;
}
};
int count = 0, ans = -1;
void kthSmallest(node* root, int k) {
if (root == NULL)
return;
kthSmallest(root->left, k);
count++;
if (count == k)
ans = root->data;
kthSmallest(root->right, k);
}
node* insert(node* root, int val) {
if (root == NULL) return new node(val);
if (val < root->data)
root->left = insert(root->left, val);
else
root->right = insert(root->right, val);
return root;
}
int main() {
int n, val, k;
cin >> n;
node* root = NULL;
for (int i = 0; i < n; i++) {
cin >> val;
root = insert(root, val);
}
23CSR201 MANORANJITHAM 717824P231
cin >> k;
kthSmallest(root, k);
cout << ans << endl;
return 0;
}
OUTPUT:
RESULT:
Thus , the C++ program to to find the Kth Smallest Element in a Binary Search Tree
(BST) was successfully implemented and the output was verified.
23CSR201 MANORANJITHAM 717824P231
Exp No : 5(c)
Date : THE RANGE SUM OF A BINARY SEARCH TREE
AIM:
To write a C++ program to calculate the sum of all node values in a Binary Search Tree
(BST) that lie within a given inclusive range low and high.
PSEUDOCODE:
1. Start
2. insert(root, value)
if (root i== NULL)
create new node
root = new node
else if (value > root->data)
root->right = insert(root->right, value)
else:
root->left = insert(root->left, value)
return root
3. rangeSumBST(root, low, high)
initialize total = 0
if (root == NULL)
return 0
if(root->data >= low && root->data <= high)
total += root -> data;
if (root->data > low)
total += rangeSumBST(root->left, low, high)
if (root->data < high)
total += rangeSumBST(root->right, low, high);
return total;
4. In main:
Input n
Repeat n times:
Input val
Insert val into BST
Input low and high
Call rangeSumBST(root, low, high)
Print the result
5. End
23CSR201 MANORANJITHAM 717824P231
SOURCE CODE:
#include<iostream>
using namespace std;
class node
{
public:
int data;
node * left, *right;
node(int data)
{
this -> data = data;
this -> left = this -> right = NULL;
}
};
node * insert(node*r, int e)
{
node * n;
if(r==NULL)
{
n=new node(e);
r=n;
}
else if(e > r->data)
r -> right = insert(r->right,e);
else
r -> left = insert(r->left,e);
return r;
}
int rangeSumBST(node* root, int low, int high) {
int total = 0;
if(root==NULL)
return 0;
if(root->data >= low && root->data <= high)
total += root -> data;
if (root->data > low)
total += rangeSumBST(root->left, low, high);
if (root->data < high)
total += rangeSumBST(root->right, low, high);
return total;
23CSR201 MANORANJITHAM 717824P231
int main()
{
int n, val, low, high;
cin>>n;
node * root = NULL;
for(int i=0; i<n; i++)
{
cin>>val;
root=insert(root,val);
}
cout<<"Enter Low and High"<<endl;
cin>>low>>high;
int result = rangeSumBST(root, low, high);
cout<<"Sum is : "<<result<<endl;
return 0;
OUTPUT:
RESULT:
Thus , the C++ program to calculate the sum of all node values in a Binary Search Tree
(BST) that lie within a given inclusive range low and high was successfully executed
and the output was verified.
23CSR201 MANORANJITHAM 717824P231