DS 5 Mono
DS 5 Mono
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
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
#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)
{
}
}
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;
}
}
return 0;
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)
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;
}
};
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;
}
RESULT:
Thus, a C++ program to implement binary search trees (BST) and AVL trees was
successfully executed and the output was verified.
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
#include <iostream>
using namespace std;
class node {
public:
int data;
node *left, *right;
node(int val) {
data = val;
left = right = NULL;
}
};
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.
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
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.