You are on page 1of 4

Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

Total Marks: 04

Obtained Marks:

Data Structures and


Algorithms
Assignment # 03
Last date of Submission: 8th December 2022

Submitted To: Sir Fakhar Ul Islam


_____________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Student Name: Abdullah Siraj


______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

Reg. Number: 2112280


______________________________________________________________________________________________________________________________________________________________________________________________________________________________________

DSA 3-B SZABIST-ISB


Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT


Instructions: Copied or shown assignments will be marked zero. Late submissions are not entertained
in any case.

Question
Write a program that prompts the user for a number, inserts it in the
BST and displays the contents of the tree using inorder
traversal. Give the source code and the runtime screen. Use the
data 50,30,20,40,70,60,80 to test your program.
Note:
⦁ Change the filename to your ID, e.g. 2073105.doc
⦁ Upload the .doc on Google Classroom.
⦁ Make sure that the output screen does not have black
background.
⦁ Poor indentation and wrong format will result in deduction of
marks.

Code:

#include <iostream>
using namespace std;

struct Node {
int key;
struct Node *left, *right;
};

struct Node *newNode(int item)


{
struct Node *temp = new Node;
temp->key = item;
temp->left = temp->right = NULL;
return temp;
}

void inorder(struct Node *root)


DSA 3-B SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

{
if (root != NULL) {
inorder(root->left);
cout << root->key << " ";
inorder(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 if (key > node->key)
node->right = insert(node->right, key);

return node;
}

int main()
{

system("color F1");

struct Node *root = NULL;


int num;

cout<<"Enter a number: ";


cin>>num;
root = insert(root, num);

cout << "Inorder traversal of the given tree \n";


inorder(root);

insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
DSA 3-B SZABIST-ISB
Shaheed Zulfikar Ali Bhutto Institute of Science & Technology

COMPUTER SCIENCE DEPARTMENT

insert(root, 80);

cout << "\nInorder traversal of the given tree \n";


inorder(root);

return 0;
}

Output:

DSA 3-B SZABIST-ISB

You might also like