Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab Manual # 11: Title: Binary Tree Implementation

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Lab # 11 - Data Structures & Algorithms

Lab Manual # 11

Title: Binary Tree implementation

Student Name: Muhammad Usama Saghar Class Roll: 2019-cpe-27

Subject Teacher: Dr. Muhammad Waqar Ashraf


Lab Engineer : Engr. Abdul Rehman

LAB POINTS SCORE


Lab Performance [10] 0 1 2 3 4 5
Lab Participation
Lab Activity Completion on the Same Day

Lab Submission[10] 0 1 2 3 4 5
Completeness & Correctness
Required Conclusion & Results

No of Checks
SUB TOTAL
TOTAL SCORE

Course Instructor

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 - Data Structures & Algorithms
P-1 Write a program to create a binary tree and search a value in it.
Your Code:
#include<iostream>

using namespace std;

// A structure representing a node of a tree.


struct node
{
int data;
node *left;
node *right;
};

// A function creating new node of the tree and assigning the data.
node* CreateNode(int data)
{
node *newnode = new node;
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;

return newnode;
}

// A function creating binary search tree.


node* InsertIntoTree(node* root, int data)
{
// Create node using data from argument list.
node *temp = CreateNode(data);
node *t = new node;
t = root;

// If root is null, assign it to the node created.


if(root == NULL)
root = temp;
else
{
// Find the position for the new node to be inserted.
while(t != NULL)
{
if(t->data < data )
{
if(t->right == NULL)
{
// If current node is NULL then insert the node.
t->right = temp;
break;
}
// Shift pointer to the left.
t = t->right;
}

else if(t->data > data)


Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 11 - Data Structures & Algorithms
{
if(t->left == NULL)
{
// If current node is NULL then insert the node.
t->left = temp;
break;
}
// Shift pointer to the left.
t = t->left;
}
}
}
return root;
}

// A function to search item in a BST.


void Search(node *root, int data)
{
int depth = 0;
node *temp = new node;
temp = root;
// Run the loop untill temp points to a NULL pointer.
while(temp != NULL)
{
depth++;
if(temp->data == data)
{
cout<<"\nData found at depth: "<<depth;
return;
}
// Shift pointer to left child.
else if(temp->data > data)
temp = temp->left;
// Shift pointer to right child.
else
temp = temp->right;
}

cout<<"\n Data not found";


return;
}

int main()
{
char ch;
int n, i, a[20]={89, 53, 95, 1, 9, 67, 72, 66, 75, 77, 18, 24, 35, 90, 38, 41, 49, 81, 27, 97};
node *root = new node;
root = NULL;

// Construct the BST.


for(i = 0; i < 20; i++)
root = InsertIntoTree(root, a[i]);

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 - Data Structures & Algorithms
up:
cout<<"\nEnter the Element to be searched: ";
cin>>n;

Search(root, n);

// Ask user to enter choice for further searching.


cout<<"\n\t\t\nMuhammad Usama Saghar 2019-cpe-27";
cout<<"\n\n\tDo you want to search more...enter choice(y/n)?";
cin>>ch;
if(ch == 'y' || ch == 'Y')
goto up;

return 0;
}

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 - Data Structures & Algorithms
P-2 Write a program that delete a node in binary tree at any specific position given by the user.
Your Code:
#include<stdio.h>
#include<stdlib.h>
struct node{
int key;
struct node *left, *right;
};
struct node *newNode(int item){
struct node *temp = (struct node *)malloc(sizeof(struct node));
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}
void inordertraversal(struct node *root){
if (root != NULL){
inordertraversal(root->left);
printf("%d ", root->key);
inordertraversal(root->right);
}
}
struct node* insert(struct node* node, int key){
if (node == NULL) return newNode(key);
if (key < node->key)
node->left = insert(node->left, key);
else
node->right = insert(node->right, key);
return node;
}
struct node * minValueNode(struct node* node){
struct node* current = node;
while (current && current->left != NULL)
current = current->left;
return current;
}
struct node* deleteNode(struct node* root, int key){
if (root == NULL) return root;
if (key < root->key)
root->left = deleteNode(root->left, key);
else if (key > root->key)
root->right = deleteNode(root->right, key);
else{
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;
}
struct node* temp = minValueNode(root->right);
root->key = temp->key;
Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 11 - Data Structures & Algorithms
root->right = deleteNode(root->right, temp->key);
}
return root;
}
int main(){
struct node *root = NULL;
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);
printf("Inorder traversal of the given tree \n");
inordertraversal(root);
printf("\nDelete 20\n");
root = deleteNode(root, 20);
printf("Inorder traversal of the modified tree \n");
inordertraversal(root);
printf("\nDelete 30\n");
root = deleteNode(root, 30);
printf("Inorder traversal of the modified tree \n");
inordertraversal(root);
printf("\nDelete 50\n");
root = deleteNode(root, 50);
printf("Inorder traversal of the modified tree \n");
inordertraversal(root);
printf("\n\t\tMuhammad Usama Saghar 2019-cpe-27");
return 0;
}

Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 - Data Structures & Algorithms
P-3 Write a program that insert a node in binary tree at any specific position given by the user.
Your Code:

#include <iostream>

using namespace std;

class BST
{

int data;

BST *left, *right;

public:

// Default constructor.

BST();

// Parameterized constructor.

BST(int);

// Insert function.

BST* Insert(BST*, int);

// Inorder traversal.

void Inorder(BST*);
};

// Default Constructor definition.


BST ::BST()

: data(0)

, left(NULL)

, right(NULL)
{
}

// Parameterized Constructor definition.

BST ::BST(int value)


{

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 - Data Structures & Algorithms
data = value;

left = right = NULL;


}

// Insert function definition.

BST* BST ::Insert(BST* root, int value)


{

if (!root)

// Insert the first node, if root is NULL.

return new BST(value);

// Insert data.

if (value > root->data)

// Insert right node data, if the 'value'

// to be inserted is greater than 'root' node data.

// Process right nodes.

root->right = Insert(root->right, value);

else

// Insert left node data, if the 'value'

// to be inserted is greater than 'root' node data.

// Process left nodes.

root->left = Insert(root->left, value);

Version 1.0 Department of Computer Engineering UCE&T BZU Multan


Lab # 11 - Data Structures & Algorithms
// Return 'root' node, after insertion.

return root;
}

// Inorder traversal function.


// This gives data in sorted order.

void BST ::Inorder(BST* root)


{

if (!root) {

return;

Inorder(root->left);

cout << root->data << endl;

Inorder(root->right);
}

// Driver code

int main()
{

BST b, *root = NULL;

root = b.Insert(root, 2019);

b.Insert(root, 03);

b.Insert(root, 16);

b.Insert(root, 93);

b.Insert(root, 22);

b.Insert(root, 450);

b.Insert(root, 27);

b.Inorder(root);
cout<<"\n\t\tMuhammad Usama Saghar 2019-cpe-27";
return 0;
}

// This code is design by Muhammad Usama Saghar


Version 1.0 Department of Computer Engineering UCE&T BZU Multan
Lab # 11 - Data Structures & Algorithms
Paste your output here

Version 1.0 Department of Computer Engineering UCE&T BZU Multan

You might also like