Professional Documents
Culture Documents
{
int c = 1;
if (n == NULL)
return 0;
else
{
c += count(n->l);
c += count(n->r);
return c;
}
}
#include "stdafx.h"
# include <iostream>
using namespace std;
// Struct
struct node
{
int data;
node *left;
node *right;
};
node* root=NULL;
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);
}
}
// 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);
}
}
return this->Delete(this->root,key);
void print()
{
this->inorder(this->root);
}
if (root!= NULL)
{
inorder(root->left);
cout<<root->data<<" ";
this->inorder(root->right);
}
}
void print1()
{
this->preorder(this->root);
}
void print2()
{
this->postorder(this->root);
}
int countN()
{
return count(this->root);
if (root == NULL)
return 0;
else
{
c += count(root->left);
c += count(root->right);
return c;
}
}
int countL()
{
return countleaf(this->root);
BST()
{
root = NULL;
}
virtual ~BST()
{
cout<<"Destructor Called";
this->print();
system("pause");
}
};
{ 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);
}
}
*/
if (root!= NULL)
{
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
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);
}
}*/
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;
}
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;
}
#include "stdafx.h"
# include <iostream>
using namespace std;
// Struct
struct node
{
int data;
node *left;
node *right;
};
node* root=NULL;
node *retval;
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
{
this->root = insert(this->root,key);
}
}
// 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);
}
}
return this->Delete(this->root,key);
}*/
if(data == root->data)
{
node *retval = NULL;
if(root->left == NULL)
{
retval = root->right;
delete root;
return retval;
}
return root;
}
{
if(root->right == NULL)
return root;
getSuccessor(root->right);
}
void print()
{
this->inorder(this->root);
}
if (root!= NULL)
{
inorder(root->left);
cout<<root->data<<" ";
this->inorder(root->right);
}
}
void print1()
{
this->preorder(this->root);
}
void print2()
{
this->postorder(this->root);
}
int countN()
{
return count(this->root);
if (root == NULL)
return 0;
else
{
c += count(root->left);
c += count(root->right);
return c;
}
}
int countL()
{
return countleaf(this->root);
BST()
{
root = NULL;
}
virtual ~BST()
{
cout<<"Destructor Called";
this->print();
system("pause");
}
};
//cout<<root->data;
return root;
}
{ 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);
}
}
*/
if (root!= NULL)
{
inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
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);
}
}*/
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;
}