Need a project done?

C++ Programming Developer

Search This Blog

Binary Tree in C++


Binary Tree program in C++ is given below:

See First: Binary Tree in C++ (A relatively easier Binary Tree program)

// Binary Tree in C++
// Binary Tree program to insert generically in the binary tree.

#include "stdafx.h" //Comment This line if it creates error.
#include <iostream>
using namespace std;



//////////////////////////////////////////////////////////////
struct nodeType{
nodeType();
int info;
nodeType* father;
nodeType* left;
nodeType* right;
};
nodeType::nodeType(){
father = left = right = NULL;
}
//////////////////////////////////////////////////////////////
class binaryTree{
public:
binaryTree(){  root = NULL; }
//------------------------------------------------------------
nodeType* makeTree(int x){
nodeType* p = new nodeType;
p->info = x;
p->father = NULL;
p->right = NULL;
p->left = NULL;
root = p;
return p;
}
//------------------------------------------------------------
void setLeft(nodeType* p, int x){
if (p == NULL){
cout << "Void Insertion!" << endl;
}
else if (p->left != NULL){
cout << "Invalid Insertion" << endl;
}
else{
p->left = new nodeType;
p->left->info = x;
p->left->right = NULL;
p->left->left = NULL;
p->left->father = p;
}
}
//------------------------------------------------------------
void setRight(nodeType* p, int x){
if (p==NULL){
cout << "Void Insertion" <<endl;
}
else if (p->right != NULL){
cout << "Invalid Insertion" << endl;
}
else{
p->right = new nodeType;
p->right->info = x;
p->right->left = NULL;
p->right->right = NULL;
p->right->father = p;
}
}
//------------------------------------------------------------
nodeType*& getRoot(){
return root;
}
//------------------------------------------------------------
void insert(nodeType *p, int x){
if (p == NULL){
p = new nodeType;
p->info = x;
p->father = NULL;
p->right = NULL;
p->left = NULL;
}
else{
if (x < p->info){ //left side
if (p->left != NULL)
insert (p->left, x);
else{ //p->left == NULL
//create a node, and insert on left.
//1. create a node
nodeType* newNode = new nodeType;
newNode->info = x;
newNode->father = p;
newNode->right = newNode->left = NULL;
//2. insert on p's left.
p->left = newNode;
}
}
else{ // go on right side.
if (p->right!=NULL)
insert(p->right, x);
else{
//create a node, and insert on right.
//1. create a node.
nodeType* newNode = new nodeType;
newNode->father = p;
newNode->info = x;
newNode->left = newNode->right = NULL;
//2. insert on right
p->right = newNode;
}
}
}
}
//------------------------------------------------------------
void print(nodeType* p){
if (p == NULL){
cout << "No item" << endl;
return;
}
cout << p->info << endl;

if (p!=NULL && p->left == NULL && p->right == NULL)
return ;
if (p!=NULL && p->left != NULL && p->right == NULL)
print (p->left);
if (p!=NULL && p->left == NULL && p->right != NULL)
print (p->right);
if (p!=NULL && p->left != NULL && p->right != NULL){
print (p->left);
print (p->right);
}
}
//------------------------------------------------------------
//----------------------------------------------------------------
//----------------------------------------------------------------
private:
nodeType* root;
};
//////////////////////////////////////////////////////////////
int main(){
binaryTree obj;
cout << "Enter Number to insert in binary tree: "; int x; cin >> x;
obj.makeTree(x);
while (1){
cout <<"1. Insert" << endl
<< "2. Print All" << endl
<< "3. Exit" << endl;
int opt; cin >> opt;

switch(opt){
case 1:
cout << "Enter Number: "; int x; cin >> x;
obj.insert(obj.getRoot(),x);
break;
case 2:
cout << "---------------------------------" << endl;
obj.print(obj.getRoot());
cout << "---------------------------------" << endl;
break;
case 3:
return 0;
}
}
return 0;
}

No comments:

Post a Comment

"Don't let anyone ever make you feel like you don't deserve what you want."