0% found this document useful (0 votes)
50 views23 pages

Dsa Assignment: Name - Sanchit Namdeo Roll No. - 211020443 Branch - DSAI

The document contains code for operations on binary search trees: 1) It defines a Node class and functions to insert nodes, take input to build the tree, and traverse it using inorder, preorder and postorder traversal. 2) Additional functions are included to delete a node from the BST and print the tree using level order traversal. 3) The main function builds a BST from input, inserts all nodes, and calls the various traversal functions to output the tree.

Uploaded by

Ravil Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views23 pages

Dsa Assignment: Name - Sanchit Namdeo Roll No. - 211020443 Branch - DSAI

The document contains code for operations on binary search trees: 1) It defines a Node class and functions to insert nodes, take input to build the tree, and traverse it using inorder, preorder and postorder traversal. 2) Additional functions are included to delete a node from the BST and print the tree using level order traversal. 3) The main function builds a BST from input, inserts all nodes, and calls the various traversal functions to output the tree.

Uploaded by

Ravil Patel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

DSA ASSIGNMENT

Name – Sanchit Namdeo


Roll No. – 211020443
Branch – DSAI

Program on Binary 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;
}
};

int search(int arr[], int strt, int end, int value);

void levelOrderTraverser(Node *root)


{
queue<Node *> q;
q.push(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);
}
}
}
}

Node *developTree(int inorder[], int preorder[], int inStrt, int inEnd)


{

static int preIndex = 0;

if (inStrt > inEnd)


{
return NULL;
}

Node *tNode = new Node(preorder[preIndex++]);

if (inStrt == inEnd)
{
return tNode;
}

int inIndex = search(inorder, inStrt, inEnd, tNode->data);

tNode->left = developTree(inorder, preorder, inStrt, inIndex - 1);


tNode->right = developTree(inorder, preorder, inIndex + 1, inEnd);

return tNode;
}

int search(int arr[], int strt, int end, int value)


{
for (int i = strt; i <= end; i++)
{
if (arr[i] == value)
{
return i;
}
}
}

void inorder(Node *root)


{
if (root == NULL)
{
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}

void preorder(Node *root)


{
if (root == NULL)
{
return;
}
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}

void postorder(Node *root)


{
if (root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
int main()
{
int in[] = {2, 5, 6, 7, 10, 12, 14, 15, 16};
int pre[] = {10, 5, 2, 6, 7, 14, 12, 15, 16};
int len = sizeof(in) / sizeof(in[0]);
Node *root = developTree(in, pre, 0, len - 1);

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;
}
};

int search(int arr[], int start, int end, int value);

void levelOrderTraverser(Node *root)


{
queue<Node *> q;
q.push(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);
}
}
}
}

Node *build(int in[], int post[], int inStrt, int inEnd, int *pIndex)
{

if (inStrt > inEnd)


return NULL;

Node *node = new Node(post[*pIndex]);


(*pIndex)--;

if (inStrt == inEnd)
return node;

int iIndex = search(in, inStrt, inEnd, node->data);

node->right = build(in, post, iIndex + 1, inEnd, pIndex);


node->left = build(in, post, inStrt, iIndex - 1, pIndex);

return node;
}

Node *developTree(int in[], int post[], int n)


{
int pIndex = n - 1;
return build(in, post, 0, n - 1, &pIndex);
}

int search(int arr[], int start, int end, int value)


{
int i;
for (i = start; i <= end; i++)
{
if (arr[i] == value)
{
break;
}
}
return i;
}
void inorder(Node *root)
{
if (root == NULL)
{
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}

void preorder(Node *root)


{
if (root == NULL)
{
return;
}
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}

void postorder(Node *root)


{
if (root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}

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);

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
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;
}
};

Node *insertAtBST(Node *root, int data)


{
if (root == NULL)
{
root = new Node(data);
return root;
}

if (data > root->data)


{
root->right = insertAtBST(root->right, data);
}
else
{
root->left = insertAtBST(root->left, data);
}
return root;
}

void takeInput(Node *&root)


{
int data;
cin >> data;
while (data != -1)
{
root = insertAtBST(root, data);
cin >> data;
}
}

void levelOrderTraverser(Node *root)


{
queue<Node *> q;
q.push(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);
}
}
}
}

void inorder(Node *root)


{
if (root == NULL)
{
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}

void preorder(Node *root)


{
if (root == NULL)
{
return;
}
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}

void postorder(Node *root)


{
if (root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}

int main()
{
Node *root=NULL;
takeInput(root);

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;
}
};

Node *insertAtBST(Node *root, int data)


{
if (root == NULL)
{
root = new Node(data);
return root;
}

if (data > root->data)


{
root->right = insertAtBST(root->right, data);
}
else
{
root->left = insertAtBST(root->left, data);
}
return root;
}

void takeInput(Node *&root)


{
int data;
cin >> data;

while (data != -1)


{
root = insertAtBST(root, data);
cin >> data;
}
}

Node *Minval(Node *root)


{
Node *temp = root;
while (temp->left != NULL)
{
temp = temp->left;
}
return temp;
}

Node *deletefromBST(Node *root, int val)


{
if (root == NULL)
{
return root;
}
if (root->data == val)
{
// 0 child
if (root->left == NULL && root->right == NULL)
{
delete root;
return 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;
}
}

void levelOrderTraverser(Node *root)


{
queue<Node *> q;
q.push(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);
}
}
}
}

void inorder(Node *root)


{
if (root == NULL)
{
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}

void preorder(Node *root)


{
if (root == NULL)
{
return;
}
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}

void postorder(Node *root)


{
if (root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}

int main()
{
Node *root=NULL;
takeInput(root);

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;

// 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;
}
};

Node *insertAtBST(Node *root, int data)


{
if (root == NULL)
{
root = new Node(data);
return root;
}

if (data > root->data)


{
root->right = insertAtBST(root->right, data);
}
else
{
root->left = insertAtBST(root->left, data);
}
return root;
}

void takeInput(Node *&root)


{
int data;
cin >> data;

while (data != -1)


{
root = insertAtBST(root, data);
cin >> data;
}
}

int leafNode(Node *root){


if (root==NULL)
{
return 0;
}
if (root->left==NULL && root->right==NULL)
{
return 1;
}
else{
return leafNode(root->left)+leafNode(root->right);
}
}

void levelOrderTraverser(Node *root)


{
queue<Node *> q;
q.push(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);
}
}
}
}

void inorder(Node *root)


{
if (root == NULL)
{
return;
}
inorder(root->left);
cout << root->data << " ";
inorder(root->right);
}

void preorder(Node *root)


{
if (root == NULL)
{
return;
}
cout << root->data << " ";
preorder(root->left);
preorder(root->right);
}

void postorder(Node *root)


{
if (root == NULL)
{
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}

int main()
{
Node *root=NULL;
takeInput(root);

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;
cout<<"THE NUMBER OF LEAF NODES ARE - "<<leafNode(root)<<endl;

return 0;
}

OUTPUT

You might also like