Professional Documents
Culture Documents
DS Lab 11 (2020-BSE-051)
DS Lab 11 (2020-BSE-051)
SUBMITTED TO:
Sir Rehan Ahmed Siddiqui
SUBMITTED BY:
Mahnoor Mustafa
(2020-BSE-051)
CLASS:
BSE III B
Task 1:
Traverse the binary tree given above in pre, post and inorder.
Draw the expression tree of the given algebraic expression and traverse the tree in pre, post and
inorder.
(a+b*c)+((d*e+f)*g)
+
+ *
a *
* g
b c
d +
e f
Code Task
Complete the given class to implement a binary search tree.
class Node
{
public:
Node *left;
Node *right;
int data;
} ;
class bst
{
Node *root;
public:
bst();
bool isempty();
void insert(int item);
bool search(int item);
};
#include <iostream>
using namespace std;
class Node
{
public:
Node* left;
Node* right;
int data;
};
class bst
{
Node* root;
public:
bst()
{
root = NULL;
}
bool isempty()
{
if (root == NULL)
return true;
else
return false;
}
void insert(int item)
{
Node* ptr = root;
Node* prev = 0;
while (ptr != 0)
{
prev = ptr;
if (item < ptr->data)
ptr = ptr->left;
else if (item > ptr->data)
ptr = ptr->right;
else
{
cout << "VALUE ALREADY EXIST.\n";
return;
}
}
Node* temp = new Node;
temp->data = item;
temp->left = 0;
temp->right = 0;
if (prev == 0)
root = temp;
else if (item < prev->data)
prev->left = temp;
else
prev->right = temp;
}
Node* getroot()
{
return root;
}
void inorder_traversal(Node* p)
{
if (p != NULL)
{
inorder_traversal(p->left);
cout << p->data << " ";
inorder_traversal(p->right);
}
}
return current;
}
};
int main ()
{
bst T1;
T1.insert(7);
T1.insert(3);
T1.insert(4);
T1.insert(5);
T1.insert(2);
T1.inorder_traversal(T1.getroot());
T1.deleteNode(T1.getroot(), 4);
cout << endl;
T1.inorder_traversal(T1.getroot());
system("pause");
return 0;
}