Dsa Assignment: Name - Sanchit Namdeo Roll No. - 211020443 Branch - DSAI
Dsa Assignment: Name - Sanchit Namdeo Roll No. - 211020443 Branch - DSAI
1).
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int ele)
{
this->data = ele;
this->left = NULL;
this->right = NULL;
}
};
if (temp == NULL)
{
cout << endl;
if (!q.empty())
{
q.push(NULL);
}
}
else
{
cout << temp->data << " ";
if (temp->left)
{
q.push(temp->left);
}
if (temp->right)
{
q.push(temp->right);
}
}
}
}
if (inStrt == inEnd)
{
return tNode;
}
return tNode;
}
cout<<endl;
cout << "Inorder traversal" << endl;
inorder(root);
cout << endl;
cout << "Preorder traversal" << endl;
preorder(root);
cout << endl;
cout << "Postorder traversal" << endl;
postorder(root);
cout << endl;
cout<<"LevelOrder traversal"<<endl;
levelOrderTraverser(root);
cout<<endl;
return 0;
}
OUTPUT
2).
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int ele)
{
this->data = ele;
this->left = NULL;
this->right = NULL;
}
};
while (!q.empty())
{
Node *temp = q.front();
q.pop();
if (temp == NULL)
{
cout << endl;
if (!q.empty())
{
q.push(NULL);
}
}
else
{
cout << temp->data << " ";
if (temp->left)
{
q.push(temp->left);
}
if (temp->right)
{
q.push(temp->right);
}
}
}
}
Node *build(int in[], int post[], int inStrt, int inEnd, int *pIndex)
{
if (inStrt == inEnd)
return node;
return node;
}
int main()
{
int in[] = {4, 8, 2, 5, 1, 6, 3, 7};
int post[] = {8, 4, 5, 2, 6, 7, 3, 1};
int n = sizeof(in) / sizeof(in[0]);
Node *root = developTree(in, post, n);
return 0;
}
OUTPUT
Program on Binary Search Tree
1).
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int ele)
{
this->data = ele;
this->left = NULL;
this->right = NULL;
}
};
q.push(NULL);
while (!q.empty())
{
Node *temp = q.front();
q.pop();
if (temp == NULL)
{
cout << endl;
if (!q.empty())
{
q.push(NULL);
}
}
else
{
cout << temp->data << " ";
if (temp->left)
{
q.push(temp->left);
}
if (temp->right)
{
q.push(temp->right);
}
}
}
}
int main()
{
Node *root=NULL;
takeInput(root);
return 0;
}
OUTPUT
2).
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int ele)
{
this->data = ele;
this->left = NULL;
this->right = NULL;
}
};
// 1 child
// left
if (root->left != NULL && root->right == NULL)
{
Node *temp = root->left;
delete root;
return temp;
}
// 1 child
// right
if (root->left == NULL && root->right != NULL)
{
Node *temp = root->right;
delete root;
return temp;
}
// 2 child
if (root->left != NULL && root->right != NULL)
{
int min = Minval(root->right)->data;
root->data = min;
root->right = deletefromBST(root->right, min);
return root;
}
}
else if (root->data > val)
{
root->left = deletefromBST(root->left, val);
return root;
}
else
{
root->right = deletefromBST(root->right, val);
return root;
}
}
q.push(NULL);
while (!q.empty())
{
Node *temp = q.front();
q.pop();
if (temp == NULL)
{
cout << endl;
if (!q.empty())
{
q.push(NULL);
}
}
else
{
cout << temp->data << " ";
if (temp->left)
{
q.push(temp->left);
}
if (temp->right)
{
q.push(temp->right);
}
}
}
}
int main()
{
Node *root=NULL;
takeInput(root);
// delete 14
cout<<"AFTER DELETION OF 14"<<endl;
deletefromBST(root,14);
cout << "Inorder traversal" << endl;
inorder(root);
cout << endl;
cout << "Preorder traversal" << endl;
preorder(root);
cout << endl;
cout << "Postorder traversal" << endl;
postorder(root);
cout << endl;
cout << "LevelOrder traversal" << endl;
levelOrderTraverser(root);
cout << endl;
// delete 3
cout<<"AFTER DELETION OF 3"<<endl;
deletefromBST(root,3);
cout << "Inorder traversal" << endl;
inorder(root);
cout << endl;
cout << "Preorder traversal" << endl;
preorder(root);
cout << endl;
cout << "Postorder traversal" << endl;
postorder(root);
cout << endl;
cout << "LevelOrder traversal" << endl;
levelOrderTraverser(root);
cout << endl;
// delete 8
cout<<"AFTER DELETION OF 8"<<endl;
deletefromBST(root,8);
cout << "Inorder traversal" << endl;
inorder(root);
cout << endl;
cout << "Preorder traversal" << endl;
preorder(root);
cout << endl;
cout << "Postorder traversal" << endl;
postorder(root);
cout << endl;
cout << "LevelOrder traversal" << endl;
levelOrderTraverser(root);
cout << endl;
return 0;
}
OUTPUT
3).
#include <bits/stdc++.h>
using namespace std;
class Node
{
public:
int data;
Node *left;
Node *right;
Node(int ele)
{
this->data = ele;
this->left = NULL;
this->right = NULL;
}
};
q.push(NULL);
while (!q.empty())
{
Node *temp = q.front();
q.pop();
if (temp == NULL)
{
cout << endl;
if (!q.empty())
{
q.push(NULL);
}
}
else
{
cout << temp->data << " ";
if (temp->left)
{
q.push(temp->left);
}
if (temp->right)
{
q.push(temp->right);
}
}
}
}
int main()
{
Node *root=NULL;
takeInput(root);
return 0;
}
OUTPUT