/* Binary tree with deleteion code any problem contact me
ikkhan5757@gmail.com */
#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;
class node;
node* first=NULL;
node* new_node=NULL;
node* current=NULL;
class node{
public:
int data;
node* left;
node* right;
int add(int num){
new_node=new node;
new_node->data=num;
new_node->left=NULL;
new_node->right=NULL;
if(first==NULL){
first=new_node;
return 0;
}
current=first;
while(1){
if( (current->data) > (new_node->data) ){ // when child is less than parrent
if( current->left == NULL){ // if left is empty child of parent
current->left=new_node;
return 0;
}
current=current->left;
}
else{
if(current->right==NULL){
current->right=new_node;
return 0;
}
current=current->right;
}
}
}
/////////////////////DELE
//////////////////////////////////
/////////////////////DELETE//////////////////////////////////
int del(int num){
if(first==NULL)return 1;
node* parent=NULL;
node* delt=first;
while(1){
if(delt==NULL){
return 0;
}
else if(delt->data>num){
parent=delt;
delt=delt->left;
}
else if(delt->data<num){
parent=delt;
delt=delt->right;
}
else{
cout<<"data founded"<<endl;
/////////// case 1 AND 2 ////////////
if( (delt->left==NULL) || (delt->right==NULL) || (delt->left&&delt->right==NULL)){
attach(parent,delt);
delete delt;
return 0;
}
/////////// case 3 //////////
else{
node* free_node=replace(delt);
if(delt==first){
first=free_node;
change(delt,free_node);
}
else{
change(delt,free_node);
attach(parent,free_node);
}
delete delt;
return 0;
}
}
}
}
/// getting the node to replace with delettion one
node* replace(node *replacer){
node* replacer_parent=replacer;
replacer=replacer->right;
while(replacer->left!=NULL){
replacer_parent=replacer;
replacer=replacer->left;
}
attach(replacer_parent,replacer);
return replacer;
}
void change(node* delt,node* freenode){
freenode->left=delt->left;
freenode->right=delt->right;
}
////////////delte leaf/////////////////
node* leaf(node* delt){
if(delt->left==NULL && delt->right==NULL) return NULL;
else if(delt->right==NULL) return delt->left;
else return delt->right;
}
//////// attach function //////////////
void attach(node* parent,node* delt){
if(delt==first) first=leaf(delt);
else if(parent->left==delt) parent->left=leaf(delt);
else parent->right=leaf(delt);
}
//display
void display_preorder_BST(node *dummy_root)
{
if (dummy_root != NULL)
{
cout<< dummy_root->data<< endl;
display_preorder_BST(dummy_root -> left);
display_preorder_BST(dummy_root -> right);
}
}
//display inorder
void display_inorder_BST(node *dummy_root)
{
if (dummy_root != NULL)
{
display_preorder_BST(dummy_root -> left);
cout<< dummy_root->data<< endl;
display_preorder_BST(dummy_root -> right);
}
}
};
int main(){
node list;
string choice;
while(choice!="0"){
system("cls");
cout<<"what do u want"<<endl;
cout<<"enter 1 to add"<<endl;
cout<<"enter 2 to display"<<endl;
cout<<"enter 3 to delete"<<endl;
cout<<"enter 4 to exit"<<endl;
cin>>choice;
if(choice=="1"){
system("cls");
int value;
cout<<"which value do u want to add in stack"<<endl;
cin>>value;
list.add(value);
cout<<"value has been successfully added"<<endl;
cout<<"press any key to continue";
getch();
}
if(choice=="2"){
string a;
cout<<"press 1 to display in pre order\n2 to display in in order"<<endl;
cin>>a;
if(a=="1"){
cout<<"preorder"<<endl;
list.display_preorder_BST(first);
getch();
}
if(a=="2"){
cout<<"inorder "<<endl;
list.display_inorder_BST(first);
getch();
}
}
if(choice=="3"){
system("cls");
int value;
cout<<"which value do u want to search"<<endl;
cin>>value;
int a=list.del(value);
if(a==1){
cout<<"no value is there to delete"<<endl;
}
cout<<"press any key to continue";
getch();
}
}
}
ikkhan5757@gmail.com */
#include<iostream>
#include<windows.h>
#include<conio.h>
using namespace std;
class node;
node* first=NULL;
node* new_node=NULL;
node* current=NULL;
class node{
public:
int data;
node* left;
node* right;
int add(int num){
new_node=new node;
new_node->data=num;
new_node->left=NULL;
new_node->right=NULL;
if(first==NULL){
first=new_node;
return 0;
}
current=first;
while(1){
if( (current->data) > (new_node->data) ){ // when child is less than parrent
if( current->left == NULL){ // if left is empty child of parent
current->left=new_node;
return 0;
}
current=current->left;
}
else{
if(current->right==NULL){
current->right=new_node;
return 0;
}
current=current->right;
}
}
}
/////////////////////DELE
//////////////////////////////////
/////////////////////DELETE//////////////////////////////////
int del(int num){
if(first==NULL)return 1;
node* parent=NULL;
node* delt=first;
while(1){
if(delt==NULL){
return 0;
}
else if(delt->data>num){
parent=delt;
delt=delt->left;
}
else if(delt->data<num){
parent=delt;
delt=delt->right;
}
else{
cout<<"data founded"<<endl;
/////////// case 1 AND 2 ////////////
if( (delt->left==NULL) || (delt->right==NULL) || (delt->left&&delt->right==NULL)){
attach(parent,delt);
delete delt;
return 0;
}
/////////// case 3 //////////
else{
node* free_node=replace(delt);
if(delt==first){
first=free_node;
change(delt,free_node);
}
else{
change(delt,free_node);
attach(parent,free_node);
}
delete delt;
return 0;
}
}
}
}
/// getting the node to replace with delettion one
node* replace(node *replacer){
node* replacer_parent=replacer;
replacer=replacer->right;
while(replacer->left!=NULL){
replacer_parent=replacer;
replacer=replacer->left;
}
attach(replacer_parent,replacer);
return replacer;
}
void change(node* delt,node* freenode){
freenode->left=delt->left;
freenode->right=delt->right;
}
////////////delte leaf/////////////////
node* leaf(node* delt){
if(delt->left==NULL && delt->right==NULL) return NULL;
else if(delt->right==NULL) return delt->left;
else return delt->right;
}
//////// attach function //////////////
void attach(node* parent,node* delt){
if(delt==first) first=leaf(delt);
else if(parent->left==delt) parent->left=leaf(delt);
else parent->right=leaf(delt);
}
//display
void display_preorder_BST(node *dummy_root)
{
if (dummy_root != NULL)
{
cout<< dummy_root->data<< endl;
display_preorder_BST(dummy_root -> left);
display_preorder_BST(dummy_root -> right);
}
}
//display inorder
void display_inorder_BST(node *dummy_root)
{
if (dummy_root != NULL)
{
display_preorder_BST(dummy_root -> left);
cout<< dummy_root->data<< endl;
display_preorder_BST(dummy_root -> right);
}
}
};
int main(){
node list;
string choice;
while(choice!="0"){
system("cls");
cout<<"what do u want"<<endl;
cout<<"enter 1 to add"<<endl;
cout<<"enter 2 to display"<<endl;
cout<<"enter 3 to delete"<<endl;
cout<<"enter 4 to exit"<<endl;
cin>>choice;
if(choice=="1"){
system("cls");
int value;
cout<<"which value do u want to add in stack"<<endl;
cin>>value;
list.add(value);
cout<<"value has been successfully added"<<endl;
cout<<"press any key to continue";
getch();
}
if(choice=="2"){
string a;
cout<<"press 1 to display in pre order\n2 to display in in order"<<endl;
cin>>a;
if(a=="1"){
cout<<"preorder"<<endl;
list.display_preorder_BST(first);
getch();
}
if(a=="2"){
cout<<"inorder "<<endl;
list.display_inorder_BST(first);
getch();
}
}
if(choice=="3"){
system("cls");
int value;
cout<<"which value do u want to search"<<endl;
cin>>value;
int a=list.del(value);
if(a==1){
cout<<"no value is there to delete"<<endl;
}
cout<<"press any key to continue";
getch();
}
}
}
Comments
Post a Comment