You are on page 1of 6

Department of Software Engineering

Data Structures & Algorithms Lab


Lab # 11

SUBMITTED TO:
Sir Rehan Ahmed Siddiqui
SUBMITTED BY:
Mahnoor Mustafa
(2020-BSE-051)
CLASS:
BSE III B
Task 1: 

Give answers to the following.

Traverse the binary tree given above in pre, post and inorder. 

a. Preorder Traversal: 14,2,1,3,11,10,7,30,40


b. Post Traversal: 1,3,2,7,10,40,30,11,14
c. In-order Traversal: 1,2,3,14,7,10,11,40,30

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

 void Preorder(node * ptr) 


 void Postorder(node * ptr) 
 void Inorder(node * ptr) 
  

};

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

struct Node* minValueNode(struct Node* node)


{
struct Node* current = node;

/* loop down to find the leftmost leaf */


while (current->left != NULL)
current = current->left;

return current;
}

struct Node* deleteNode(struct Node* root, int data)


{
// base case
if (root == NULL) return root;

// If the key to be deleted is smaller than the root's key,


// then it lies in left subtree
if (data < root->data)
root->left = deleteNode(root->left, data);

// If the key to be deleted is greater than the root's key,


// then it lies in right subtree
else if (data > root->data)
root->right = deleteNode(root->right, data);

// if key is same as root's key, then This is the node


// to be deleted
else
{
// node with only one child or no child
if (root->left == NULL)
{
struct Node* temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct Node* temp = root->left;
free(root);
return temp;
}

// node with two children: Get the inorder successor (smallest


// in the right subtree)
struct Node* temp = minValueNode(root->right);

// Copy the inorder successor's content to this node


root->data = temp->data;

// Delete the inorder successor


root->right = deleteNode(root->right, temp->data);
}
return root;
}

};

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

You might also like