0% found this document useful (0 votes)
17 views3 pages

CS301P Lab 5 Solution

Uploaded by

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

CS301P Lab 5 Solution

Uploaded by

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

// CS301P Lab 5 Problem KST Learning

#include<iostream>
#include<conio.h>
using namespace std;

class Node
{
private:
int data;
Node *left;
Node *right;

public:
void setData(int d)
{
data = d;
}
void setLeft(Node *ptr)
{
left = ptr;
}
void setRight(Node *ptr)
{
right = ptr;
}
int getData()
{
return data;
}
Node* getLeft()
{
return left;
}
Node* getRight()
{
return right;
}
};

class BST
{
public:
Node *Root;

BST()
{
Root = NULL;
}

void insert(int d)
{
Node *newNode = new Node;
newNode -> setData(d);
newNode -> setLeft(NULL);
newNode -> setRight(NULL);

if(Root == NULL)
{
Root = newNode;
}
else
{
Node *pre = Root;
Node *ptr = Root;
while(ptr != NULL)
{
if(d < ptr -> getData())
{
pre = ptr;
ptr = ptr -> getLeft();
if(ptr == NULL)
{
pre -> setLeft(newNode);
}
}
else if(d > ptr -> getData())
{
pre = ptr;
ptr = ptr -> getRight();
if(ptr == NULL)
{
pre -> setRight(newNode);
}
}
else
{
cout << "\n\n*** Duplicate Value Found ***";
delete newNode;
break;
}
}
}
}

int BSTMin()
{
Node *ptr = Root;
while(ptr -> getLeft() != NULL)
{
ptr = ptr -> getLeft();
}
return ptr -> getData();
}

int BSTMax()
{
Node *ptr = Root;
while(ptr -> getRight() != NULL)
{
ptr = ptr -> getRight();
}
return ptr -> getData();
}

void InOrder(Node *ptr)


{
if(ptr != NULL)
{
InOrder(ptr -> getLeft());
cout << ptr -> getData() << " ";
InOrder(ptr -> getRight());
}
}

void PreOrder(Node *ptr)


{
if(ptr != NULL)
{
cout << ptr -> getData() << " ";
PreOrder(ptr -> getLeft());
PreOrder(ptr -> getRight());
}
}

void PostOrder(Node *ptr)


{
if(ptr != NULL)
{
PostOrder(ptr -> getLeft());
PostOrder(ptr -> getRight());
cout << ptr -> getData() << " ";
}
}
};

main()
{
BST obj;
string names[] = {"Saleh","Ali","Umar","Musaddiq","Rehman","Hassaan"};
int totalNames = sizeof(names)/sizeof(names[0]);
cout << "Inserting the Length of Names in BST Nodes One By One";
cout << "\n\n-----------------------------------------------------\n\n";
for(int i=0; i<totalNames; i++)
{
cout << "Length of "<< names[i] << " is: " << names[i].length() << "\n\
n";
obj.insert(names[i].length());
}
cout << "\nValue of BST Minimum Node is: " << obj.BSTMin();
cout << "\nValue of BST Maximum Node is: " << obj.BSTMax();
cout << "\n\n-----------------------------------------------------\n\n";
cout << "In Order Traversal: ";
obj.InOrder(obj.Root);
cout << "\nPre Order Traversal: ";
obj.PreOrder(obj.Root);
cout << "\nPost Order Traversal: ";
obj.PostOrder(obj.Root);
getch();
return 0;
}

You might also like