0% found this document useful (0 votes)
1 views7 pages

QWE

The document contains a C++ implementation of a Binary Search Tree (BST) with functionalities for insertion, deletion, searching, updating, and printing the tree. It includes a menu-driven interface for user interaction, allowing users to create a tree, insert elements, delete elements, update values, sort the tree, and search for specific values. Additionally, it features a loading function and a banner for user engagement.

Uploaded by

babbba48
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)
1 views7 pages

QWE

The document contains a C++ implementation of a Binary Search Tree (BST) with functionalities for insertion, deletion, searching, updating, and printing the tree. It includes a menu-driven interface for user interaction, allowing users to create a tree, insert elements, delete elements, update values, sort the tree, and search for specific values. Additionally, it features a loading function and a banner for user engagement.

Uploaded by

babbba48
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/ 7

#include <iostream>

#include<time.h>
using namespace std;

//anouncements
void banner();
class Node;
class BST;
void menu(BST btree);
void loading(int co);
void get();
bool Search(int key);

class Node
{
public:
int data;
Node *left, *right;

Node(int value)
{
data=value;
left=right=NULL;
}
};
class BST
{
public:
Node *root;

BST()
{
root=NULL;
}
Node* Insert(Node *r,int item)
{
int count=0;
// cout<<"r-data="<<r->data<<endl;
if(r==NULL)
{
count++;

Node *newnode=new Node(item);

r=newnode;

}
else if(item <r->data)
r->left=Insert(r->left,item);

else
r->right=Insert(r->right,item);

return r;

}
void Insert(int item)
{
root= Insert(root,item);

void Preorder(Node*r)//root->left->right

{
if(r==NULL)
return;
cout<<r->data<<"\t";
Preorder(r->left);
Preorder(r->right);

void Inorder(Node*r)//left->root->right

{
if(r==NULL)
return;

Inorder(r->left);
cout<<r->data<<"\t";
Inorder(r->right);

}
void Postorder(Node*r)//left->right->root

{
if(r==NULL)
return;

Postorder(r->left);
Postorder(r->right);
cout<<r->data<<"\t";

void update(Node* nod,int ovalue,int nvalue){


if(nod==NULL)
cout<<"list is empty"<<endl;

else{
//first condition
if(nod->data > ovalue){
update(nod->left,ovalue,nvalue);

//second condition
else if(nod->data <ovalue){
update(nod->right,ovalue,nvalue);

}
//else
else{
nod->data = nvalue;
}

}
}
bool found;
Node* Search(Node *r,int key){

if (r==NULL)
{
cout<<"Not found"<<endl;
return NULL;
found=false;
}

else if ( r->data==key)
{
cout<<"Found"<<endl;
return r;
found=true;

else if(key<r->data)
return Search(r->left,key);

else
return Search(r->right,key);

Node *FindMax(Node* r,int value){


if(r==NULL)
return NULL;

else if(r->right==NULL)
return r;

else
return FindMax(r->right,value);
}
Node *Delete(Node *ro,int value){
if(ro==NULL)
cout<<"List is empty"<<endl;

if(value<ro->data)
ro->left=Delete(ro->left,value);

else if(value >ro->data)


ro->right = Delete(ro->right,value);

else{
if(ro->left==NULL & ro-right==NULL)
ro=NULL;

else if(ro->left !=NULL && ro->right==NULL)


{
ro->data = ro->left->data;
delete ro->left;
ro->left=NULL;
}
else if(ro->left==NULL && ro->right !=NULL)
{
ro->data = ro->right->data;
delete ro->right;
ro-> right = NULL;
}
else
{
Node *max=FindMax(ro->left,value);
ro->data =max->data;
Delete(ro->left,max->data);
}
}
return ro;

};

//main function
int main()
{

BST btree;
banner();
//loading();
menu(btree);

//Banner
void banner(){

cout<<"****************************************************************************
*"<<endl;
cout<<"****************************************************************************
*"<<endl;
cout<<" WELCOME"<<endl;

cout<<"****************************************************************************
*"<<endl;

void printT(Node *nod){


if(nod == NULL)
cout<<"Tree is empty!"<<endl;

else{
if(nod->left !=NULL){
printT(nod->left);

}
else if(nod->right !=NULL){
printT(nod->right);
}

else{
cout<<nod->data<<",";
}

}
}

void menu(BST btree){


int choiceM;
char clicker;
int co=0;
int val1,val2,oval4,nval4,val6;
bool run=true;
while(run){
cout<<"\
n*****************************************************************************"<<en
dl;

cout<<"****************************************************************************
*"<<endl;
cout<<"Choose:\n1- Create a new tree list\n2- Insert to the tree \n3-
Delete \n4- Update \n5- Sort \n6- Search\n7- Print \n8- Quit"<<endl<<">>>";
cin>>choiceM;

switch(choiceM){
//checks
case 1:
cout<<"\n";
cout<<"Creating a new list......"<<endl;
//loading(co);
cout<<"List is successfully created\n";

cout<<"*****************************************************************\n";
cout<<"Enter the root element:"<<endl<<">>>";
cin>>val1;
btree.Insert(val1);

cout<<"*****************************************************************\n";
cout<<"Click M to go to Menu\n>>>";
cin>>clicker;
break;

//checks
case 2:
cout<<"Enter the element to be inserted"<<endl<<">>>";
cin>>val2;
btree.Insert(val2);

cout<<"*****************************************************************\n";
cout<<"Click M to go to Menu\n>>>";
cin>>clicker;
break;
//still
case 3:
cout<<"Enter the element to be deleted\n"<<endl<<">>>";
break;

//checks
case 4:
cout<<"Enter the value to update:\n>>>";
cin>>oval4;
cout<<"Enter the new value:>>>";
cin>>nval4;
btree.update(btree.root,oval4,nval4);

cout<<"*****************************************************************\n";
cout<<"Click M to go to Menu\n>>>";
cin>>clicker;

break;
//checks
case 5:
cout<<"Sorting\n:";

cout<<"******************************************************************"<<endl;
btree.Inorder(btree.root);
break;

case 6:
cout<<"Search:"<<endl;
cout<<"Enter value to strt searching:\n>>>";
cin>>val6;
btree.Search(btree.root,val6);
break;

case 7:
cout<<"printing the tree:"<<endl;
printT(btree.root);
break;

//checks
case 8:
cout<<"quitting:";
loading(co);
run=false;
break;

}
}
}

// loading method
void loading(int co){
int randSec;
int sran;
if(co>0){

srand(time(NULL));
randSec= 30;
sran = rand()%10;
sran+=4*rand()%5;
sran++;

}
else{
co++;

srand(time(NULL));
randSec = 30;
sran = rand()%5;

int ssrand=sran;
cout<<"loading=[";
while(randSec>0){
while(ssrand>0){
cout<<"#";
ssrand--;
}
cout<<"#";
time_t stop= clock() + CLOCKS_PER_SEC;
while(clock()<stop){

}
randSec-=sran++;
}
cout<<"] %100"<<endl;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy