You are on page 1of 23

int count(node *n)

{
int c = 1;

if (n == NULL)
return 0;
else
{
c += count(n->l);
c += count(n->r);
return c;
}
}

// asgg.cpp : Defines the entry point for the console application.


//

#include "stdafx.h"
# include <iostream>
using namespace std;

// Struct
struct node
{
int data;
node *left;
node *right;
};
node* root=NULL;

// Class of Binary Search Tree

class BST
{
private:

node *root;
node* getnewnode(int key)
{
node* newNode = new node();
newNode->data=key;
newNode->left=newNode->right=NULL;
return newNode;
}

public:

BST(BST &ob)
{
this->root = ob.root;
}

// Insert Function
void insertNode(int key)

{
this->root = insert(this->root,key);
}

node* insert(node* root, int key){


if (root== NULL)
{
root=getnewnode(key);
}
else if (key < root->data)
{

cout<<" At left after "<<root->data;


root->left=insert(root->left,key);
}
else
{

cout<<" At right after "<<root->data;


root->right=insert(root->right,key);
}
return root;

}
// Search Function
bool searchNode(int key){

return this->Search(this->root,key);

}
bool Search(node* root, int key) /// Search Function
{
if (root==NULL)
return false ;
else if (root->data==key)

{
cout<<"Value Found";
return true;
}

else if (key<=root->data)
{ if (left==NULL)
return false;

else
cout<<root->data;
return Search(root->left,key);
}
else
{
if(right==NULL)
return false;

else
{ cout<<root->data;
return Search(root->right,key);
}
}

// Delte Node Function

bool DeleteNode(int key)


{

return this->Delete(this->root,key);

node* findMaxNode(node* root)


{
if(root->right == NULL)
return root;
findMaxNode(root->right);
}

node* Delete(node* root ,int key)


{

//base case when not found or found then recursion breaks

if(root == NULL) return root;


else if(root->data > key) root->left = Delete(root->left,
key);
else if(root->data < key) root->right = Delete(root->right, key);
else
{
//when the node to be deleted is found
//Four possibilities

//case1: When the node to be deleted has no children


if(root->left == NULL && root->right == NULL)
{
delete root;
root = NULL;
}
//case2: When the node to be deleted has ONLY LEFT child
else if(root->right == NULL)
{
node* temp = root;
root = root->left;
delete temp;
}
//case3: When the node to be deleted has ONLY RIGHT child
else if(root->left == NULL)
{
node* temp = root;
root = root->right;
delete temp;
}
//case4: When the node to be deleted has TWO children
else
{
node* maxNode = findMaxNode(root->left);//finding the
maximum in LEFT sub-tree
root->data = maxNode->data; //Overwriting the root node
with MAX-LEFT
root->left = Delete(root->left, maxNode->data);//deleted
the MAX-LEFT node
}
return root;
}

void print()
{
this->inorder(this->root);
}

void inorder(node* root){


if (root==NULL)
return;

if (root!= NULL)
{

inorder(root->left);
cout<<root->data<<" ";
this->inorder(root->right);
}
}

void print1()
{
this->preorder(this->root);
}

void preorder(node *root)


{
if (root!= NULL)
{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}

void print2()
{
this->postorder(this->root);
}

void postorder(node* root)


{
if (root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}

int countN()
{
return count(this->root);

int count(node* root)


{
int c = 1;

if (root == NULL)
return 0;
else
{
c += count(root->left);
c += count(root->right);
return c;
}
}

int countL()
{
return countleaf(this->root);

int countleaf(node* root)


{
int TotalNode;
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
TotalNode=countleaf(root->left) + countleaf(root->right);
return TotalNode;
}

/*int totalNode(node* root)


{
int TotalNode;
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
TotalNode=(totalNode(root->left) + totalNode(root->right)) + 1;
return TotalNode;
}*/
bool isEmpty() const { return root==NULL; }

//int* Delete(node *,int key);


// void inorder(node *root);
//void preorder(node *);
//void postorder(node *);
//int totalNode(node* );
//int countleaf(node* );

BST()
{
root = NULL;
}

virtual ~BST()
{
cout<<"Destructor Called";
this->print();
system("pause");
}

};

node* getnewnode(int key)


{
node* newNode = new node();
newNode->data=key;
newNode->left=newNode->right=NULL;
return newNode;
}

node* insert(node* root,int key)


{
if (root== NULL)
{
root=getnewnode(key);
}
else if (key < root->data)
{

cout<<" At left after "<<root->data;


root->left=insert(root->left,key);
}
else
{

cout<<" At right after "<<root->data;


root->right=insert(root->right,key);
}
//cout<<root->data;
return root;
}

bool Search(node* root, int key) /// Search Function


{
if (root==NULL)
return false ;
else if (root->data==key)

{ cout<<"Value Found";
return true;
}

else if (key<=root->data)
{ if (left==NULL)
return false;

else
cout<<root->data;
return Search(root->left,key);
}
else
{
if(right==NULL)
return false;

else
{ cout<<root->data;
return Search(root->right,key);
}
}

node* findMaxNode(node* root)


{
if(root->right == NULL) return root;
findMaxNode(root->right);
}

/*node* Delete(node* root,int key)


{
//base case when not found or found then recursion breaks

if(root == NULL) return root;


else if(root->data > key) root->left = Delete(root->left, key);
else if(root->data < key) root->right = Delete(root->right, key);
else
{
//when the node to be deleted is found
//Four possibilities

//case1: When the node to be deleted has no children


if(root->left == NULL && root->right == NULL)
{
delete root;
root = NULL;
}
//case2: When the node to be deleted has ONLY LEFT child
else if(root->right == NULL)
{
node* temp = root;
root = root->left;
delete temp;
}
//case3: When the node to be deleted has ONLY RIGHT child
else if(root->left == NULL)
{
node* temp = root;
root = root->right;
delete temp;
}
//case4: When the node to be deleted has TWO children
else
{
node* maxNode = findMaxNode(root->left);//finding the
maximum in LEFT sub-tree
root->data = maxNode->data; //Overwriting the root node
with MAX-LEFT
root->left = Delete(root->left, maxNode->data);//deleted
the MAX-LEFT node
}
return root;
}

*/

void inorder(node* root)


{
if (root==NULL)
return;

if (root!= NULL)
{

inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}

/*void inorder(node* root){


if (root==NULL)
return;
if (root!= NULL)
{

this->inorder(root->left);
cout<<root->data<<" ";
this->inorder(root->right);
}
}
*/
/*void preorder(node *root)
{
if (root!= NULL)
{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}*/

/*void postorder(node* root)


{
if (root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
*/

int totalNode(node* root)


{
int TotalNode=0;
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
TotalNode=totalNode(root->left) + totalNode(root->right) + 1;
return TotalNode;
}

int countleaf(node* root)


{
int TotalNode;
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
TotalNode=countleaf(root->left) + countleaf(root->right);
return TotalNode;
}

int _tmain(int argc, _TCHAR* argv[])


{
int data;
int find;
int option;
int c;
BST bst;
BST bst1;
do
{
cout<<"1. Insertion"<<endl;
cout<<"2. Searching"<<endl;
cout<<"3. Deletion"<<endl;
cout<<"4. Inorder"<<endl;
cout<<"5. Preoder:"<<endl;
cout<<"6. Postorder:"<<endl;
cout<<"7. Total Nodes:"<<endl;
cout<<"8. countleaf:"<<endl;
cout<<"9. Copy Constructor"<<endl;
cout<<"0. To Exit"<<endl;

cin>>option;

switch(option)

{
case 1:
cout<<"Enter the Number"<<endl;
cin>>data;
bst.insertNode(data);

break;
case 2:
cout<<"Enter the Number You find"<<endl;
cin>>find;
if(bst.searchNode(find))
cout<<"You Number is found";
else
cout<<"your Number is not Found";
break;
case 3:
cout<<"Enter the Data"<<endl;
cin>>data;
bst.DeleteNode(data);
//print(Root);
break;

case 4:
bst.print();
break;

case 5:
bst.print1();
break;

case 6:
bst.print2();
break;

case 7:
//cout<<"Total Node "<<totalNode(root)<<endl;
c = bst.countN();
cout<<c;
break;

case 8:
cout<<"Total Leaf "<<bst.countL()<<endl;
break;

case 9:
cout<<"Copy Constructor"<<endl;
bst1 = bst;
break;

}
system("pause");
system("cls");
}
while(option!=0);
return 0;
}

BinaryTreeNode* remove(BinaryTreeNode *node, int data)


{
if(node == NULL)
{
std::cerr << "ERROR: Node does not exists\n";
return node;
}

if(data == node->data)
{
BinaryTreeNode *retval = NULL;

if(node->left == NULL)
{
retval = node->right;
delete node;
return retval;
}
else if(node->right == NULL)
{
retval = node->left;
delete node;
return retval;
}
else
{
BinaryTreeNode *successor = getSuccessor(node->left);
node->data = successor->data;
node->left = remove(node->left, successor->data);
}
}
else if(data < node->data)
{
node->left = remove(node->left, data);
}
else
{
node->right = remove(node->right, data);
}

return node;
}

BinaryTreeNode* getSuccessor(BinaryTreeNode *node)


{
while(node->right != NULL)
node = node->right;
return node;
}

// asgg.cpp : Defines the entry point for the console application.


//

#include "stdafx.h"
# include <iostream>
using namespace std;

// Struct
struct node
{
int data;
node *left;
node *right;

};
node* root=NULL;
node *retval;

// Class of Binary Search Tree

class BST
{
private:

node *root;
node* getnewnode(int key)
{
node* newNode = new node();
newNode->data=key;
newNode->left=newNode->right=NULL;
return newNode;
}
public:

BST(BST &ob)
{
this->root = ob.root;
}

// Insert Function

void insertNode(int key)

{
this->root = insert(this->root,key);
}

node* insert(node* root, int key){


if (root== NULL)
{
root=getnewnode(key);
}
else if (key < root->data)
{

cout<<" At left after "<<root->data;


root->left=insert(root->left,key);
}
else
{

cout<<" At right after "<<root->data;


root->right=insert(root->right,key);
}
return root;

}
// Search Function
bool searchNode(int key){

return this->Search(this->root,key);

}
bool Search(node* root, int key) /// Search Function
{
if (root==NULL)
return false ;
else if (root->data==key)

{
cout<<"Value Found";
return true;
}

else if (key<=root->data)
{ if (left==NULL)
return false;

else
cout<<root->data;
return Search(root->left,key);
}
else
{
if(right==NULL)
return false;

else
{ cout<<root->data;
return Search(root->right,key);
}
}

// Delte Node Function

bool DeleteNode(int key)


{

return this->Delete(this->root,key);

/*node* findMaxNode(node* root)


{
if(root->right == NULL)
return root;
findMaxNode(root->right);
}

node* Delete(node* root ,int key)


{

//base case when not found or found then recursion breaks

if(root == NULL) return root;


else if(root->data > key) root->left = Delete(root->left,
key);
else if(root->data < key) root->right = Delete(root->right, key);
else
{
//when the node to be deleted is found
//Four possibilities

//case1: When the node to be deleted has no children


if(root->left == NULL && root->right == NULL)
{
delete root;
root = NULL;
}
//case2: When the node to be deleted has ONLY LEFT child
else if(root->right == NULL)
{
node* temp = root;
root = root->left;
delete temp;
}
//case3: When the node to be deleted has ONLY RIGHT child
else if(root->left == NULL)
{
node* temp = root;
root = root->right;
delete temp;
}
//case4: When the node to be deleted has TWO children
else
{
node* maxNode = findMaxNode(root->left);//finding the
maximum in LEFT sub-tree
root->data = maxNode->data; //Overwriting the root node
with MAX-LEFT
root->left = Delete(root->left, maxNode->data);//deleted
the MAX-LEFT node
}
return root;
}

}*/

node* Delete(node *root, int data)


{
if(root == NULL)
{

cout << "Node does not exists\n";


return root;
}

if(data == root->data)
{
node *retval = NULL;

if(root->left == NULL)
{
retval = root->right;
delete root;
return retval;
}

else if(root->right == NULL)


{
retval = root->left;
delete root;
return retval;
}
else
{
node *successor = getSuccessor(root->left);
root->data = successor->data;
root->left = Delete(root->left, successor->data);
}
}
else if(data < root->data)
{
cout<<root->data;
root->left = Delete(root->left, data);
}
else
{
cout<<root->data;
root->right = Delete(root->right, data);
}

return root;
}

node* getSuccessor(node *root)

{
if(root->right == NULL)
return root;
getSuccessor(root->right);
}

void print()
{
this->inorder(this->root);
}

void inorder(node* root){


if (root==NULL)
return;

if (root!= NULL)
{

inorder(root->left);
cout<<root->data<<" ";
this->inorder(root->right);
}
}

void print1()
{
this->preorder(this->root);
}

void preorder(node *root)


{
if (root!= NULL)
{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}

void print2()
{
this->postorder(this->root);
}

void postorder(node* root)


{
if (root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}

int countN()
{
return count(this->root);

int count(node* root)


{
int c = 1;

if (root == NULL)
return 0;
else
{
c += count(root->left);
c += count(root->right);
return c;
}
}

int countL()
{
return countleaf(this->root);

int countleaf(node* root)


{
int TotalNode;
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
TotalNode=countleaf(root->left) + countleaf(root->right);
return TotalNode;
}

/*int totalNode(node* root)


{
int TotalNode;
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
TotalNode=(totalNode(root->left) + totalNode(root->right)) + 1;
return TotalNode;
}*/

bool isEmpty() const { return root==NULL; }

//int* Delete(node *,int key);


// void inorder(node *root);
//void preorder(node *);
//void postorder(node *);
//int totalNode(node* );
//int countleaf(node* );

BST()
{
root = NULL;
}

virtual ~BST()
{
cout<<"Destructor Called";
this->print();
system("pause");
}

};

node* getnewnode(int key)


{
node* newNode = new node();
newNode->data=key;
newNode->left=newNode->right=NULL;
return newNode;
}

node* insert(node* root,int key)


{
if (root== NULL)
{
root=getnewnode(key);
}
else if (key < root->data)
{

cout<<" At left after "<<root->data;


root->left=insert(root->left,key);
}
else
{

cout<<" At right after "<<root->data;


root->right=insert(root->right,key);
}

//cout<<root->data;
return root;
}

bool Search(node* root, int key) /// Search Function


{
if (root==NULL)
return false ;
else if (root->data==key)

{ cout<<"Value Found";
return true;
}

else if (key<=root->data)
{ if (left==NULL)
return false;

else
cout<<root->data;
return Search(root->left,key);
}
else
{
if(right==NULL)
return false;

else
{ cout<<root->data;
return Search(root->right,key);
}
}

node* findMaxNode(node* root)


{
if(root->right == NULL) return root;
findMaxNode(root->right);
}
/*node* Delete(node* root,int key)
{
//base case when not found or found then recursion breaks

if(root == NULL) return root;


else if(root->data > key) root->left = Delete(root->left, key);
else if(root->data < key) root->right = Delete(root->right, key);
else
{
//when the node to be deleted is found
//Four possibilities

//case1: When the node to be deleted has no children


if(root->left == NULL && root->right == NULL)
{
delete root;
root = NULL;
}
//case2: When the node to be deleted has ONLY LEFT child
else if(root->right == NULL)
{
node* temp = root;
root = root->left;
delete temp;
}
//case3: When the node to be deleted has ONLY RIGHT child
else if(root->left == NULL)
{
node* temp = root;
root = root->right;
delete temp;
}
//case4: When the node to be deleted has TWO children
else
{
node* maxNode = findMaxNode(root->left);//finding the
maximum in LEFT sub-tree
root->data = maxNode->data; //Overwriting the root node
with MAX-LEFT
root->left = Delete(root->left, maxNode->data);//deleted
the MAX-LEFT node
}
return root;
}

*/

void inorder(node* root)


{
if (root==NULL)
return;

if (root!= NULL)
{

inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}

/*void inorder(node* root){


if (root==NULL)
return;

if (root!= NULL)
{

this->inorder(root->left);
cout<<root->data<<" ";
this->inorder(root->right);
}
}
*/
/*void preorder(node *root)
{
if (root!= NULL)
{
cout<<root->data<<" ";
preorder(root->left);
preorder(root->right);
}
}*/

/*void postorder(node* root)


{
if (root==NULL)
return;
postorder(root->left);
postorder(root->right);
cout<<root->data<<" ";
}
*/

int totalNode(node* root)


{
int TotalNode=0;
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
TotalNode=totalNode(root->left) + totalNode(root->right) + 1;
return TotalNode;
}

int countleaf(node* root)


{
int TotalNode;
if(root == NULL)
return 0;
else
if(root->left == NULL && root->right == NULL)
return 1;
else
TotalNode=countleaf(root->left) + countleaf(root->right);
return TotalNode;
}

int _tmain(int argc, _TCHAR* argv[])


{
int data;
int find;
int option;
int c;
BST bst;
BST bst1;
do
{
cout<<"1. Insertion"<<endl;
cout<<"2. Searching"<<endl;
cout<<"3. Deletion"<<endl;
cout<<"4. Inorder"<<endl;
cout<<"5. Preoder:"<<endl;
cout<<"6. Postorder:"<<endl;
cout<<"7. Total Nodes:"<<endl;
cout<<"8. countleaf:"<<endl;
cout<<"9. Copy Constructor"<<endl;
cout<<"0. To Exit"<<endl;

cin>>option;

switch(option)

{
case 1:
cout<<"Enter the Number"<<endl;
cin>>data;
bst.insertNode(data);

break;
case 2:
cout<<"Enter the Number You find"<<endl;
cin>>find;
if(bst.searchNode(find))
cout<<"You Number is found";
else
cout<<"your Number is not Found";
break;
case 3:
cout<<"Enter the Data"<<endl;
cin>>data;
bst.DeleteNode(data);
//print(Root);
break;
case 4:
bst.print();
break;

case 5:
bst.print1();
break;

case 6:
bst.print2();
break;

case 7:
//cout<<"Total Node "<<totalNode(root)<<endl;
c = bst.countN();
cout<<c;
break;

case 8:
cout<<"Total Leaf "<<bst.countL()<<endl;
break;

case 9:
cout<<"Copy Constructor"<<endl;
bst1 = bst;
break;

}
system("pause");
system("cls");
}
while(option!=0);
return 0;
}

You might also like