Professional Documents
Culture Documents
Binary Tree
Binary Tree
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
{
this->root = insert(this->root, key);
}
}
// Search Function
bool searchNode(int 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
cout << root->data;
return Search(root->left, key);
}
else
{
if (right == NULL)
return false;
else
{
cout << root->data;
return Search(root->right, 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<<root->data;
return root;
}
{
cout << "Value Found";
return true;
}
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);
}
}*/
int main() {
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;