BinaryTreeProgramms
BinaryTreeProgramms
#include <iostream>
#include <queue>
using namespace std;
class Tree
{
public:
int data;
Tree *left, *right;
Tree(int data)
{
this->data = data;
left = right = NULL;
}
};
Tree *CreateBinaryTree()
{
int x;
cout << "Enter the root Node, (-1 for NULL):";
cin >> x;
if (x == -1)
return NULL;
queue<Tree *> q;
Tree *root = new Tree(x);
q.push(root);
int lef, rig;
while (!q.empty())
{
Tree *temp = q.front();
q.pop();
cout << "Enter left child of " << temp->data << ", (-1 for NULL):";
cin >> lef;
if (lef != -1)
{
temp->left = new Tree(lef);
q.push(temp->left);
}
cout << "Enter right child of " << temp->data << ", (-1 for NULL):";
cin >> rig;
if (rig != -1)
{
temp->right = new Tree(rig);
q.push(temp->right);
}
}
return root;
}
// Level ORder Traversal of Binary TRee
void LevelOrder(Tree *root)
{
queue<Tree *> q;
q.push(root);
while (!q.empty())
{
Tree *temp = q.front();
cout << temp->data << " ";
q.pop();
if (temp->left)
q.push(temp->left);
if (temp->right)
q.push(temp->right);
}
}
int main()
{
cout << "Create Binary Tree ." << endl;
Tree *root = CreateBinaryTree();
if (!root)
{
cout << "Tree is empty, No Traversal is possible." << endl;
return 0;
}
LevelOrder(root);
return 0;
}
Tree(int data)
{
this->data = data;
left = right = NULL;
}
};
queue<Tree *> q;
Tree *root = new Tree(x);
q.push(root);
while (!q.empty())
{
Tree *temp = q.front();
q.pop();
return root;
}
stack<Tree *> s;
s.push(root);
if (temp->left)
s1.push(temp->left);
if (temp->right)
s1.push(temp->right);
}
stack<Tree *> s;
Tree *current = root;
int main()
{
cout << "Create a binary tree:" << endl;
Tree *root = BinaryTree();
if (!root)
{
cout << "Tree is empty. No traversal is possible." << endl;
return 0;
}
return 0;
}
class Tree
{
public:
int data;
Tree *left, *right;
Tree(int data)
{
this->data = data;
left = right = NULL;
}
};
Tree *BinaryTree()
{
int x;
cout << "(-1 for NULL):";
cin >> x;
if (x == -1)
return NULL;
Tree *temp = new Tree(x);
cout << "Enter left child of " << temp->data << ",";
temp->left = BinaryTree();
cout << "Enter right child of " << temp->data << ",";
temp->right = BinaryTree();
return temp;
}
void PreOrder(Tree *root)
{
if (root == NULL)
{
return;
}
cout << root->data << " ";
PreOrder(root->left);
PreOrder(root->right);
}
void InOrder(Tree *root)
{
if (root == NULL)
{
return;
}
InOrder(root->left);
cout << root->data << " ";
InOrder(root->right);
}
void PostOrder(Tree *root)
{
if (root == NULL)
{
return;
}
PostOrder(root->left);
PostOrder(root->right);
cout << root->data << " ";
}
int main()
{
Tree *root;
cout << "Enter root node :";
root = BinaryTree();
if (!root)
{
cout << "The tree is empty. No traversal is possible." << endl;
return 0;
}
cout << "PreORder Traversal of Binary Tree is ";
PreOrder(root);
cout << "\nInORder Traversal of Binary Tree is ";
InOrder(root);
cout << "\nPostORder Traversal of Binary Tree is ";
PostOrder(root);
return 0;
}
class Tree
{
public:
int data;
Tree *left, *right;
Tree(int data)
{
this->data = data;
left = right = NULL;
}
};
Tree *BinaryTree()
{
int x;
cout << "enter node value (-1 for NULL):";
cin >> x;
if (x == -1)
return NULL;
Tree *temp = new Tree(x);
cout << "Enter left child of " << temp->data << ", ";
temp->left = BinaryTree();
cout << "Enter the right child of " << temp->data << ", ";
temp->right = BinaryTree();
return temp;
}
void LevelOrder(Tree *root)
{
if (!root)
{
cout << "Tree is empty.";
return;
}
queue<Tree *> q;
q.push(root);
while (!q.empty())
{
Tree *temp = q.front();
q.pop();
cout << temp->data << " ";
if (temp->left)
{
q.push(temp->left);
}
if (temp->right)
{
q.push(temp->right);
}
}
}
Tree *Insertion(Tree *root, int value)
{
if (!root)
{
return new Tree(value);
}
else
{
queue<Tree *> q;
q.push(root);
while (!q.empty())
{
Tree *temp = q.front();
q.pop();
if (!temp->left)
{
temp->left = new Tree(value);
return root;
}
else if (!temp->right)
{
temp->right = new Tree(value);
return root;
}
else
{
q.push(temp->left);
q.push(temp->right);
}
}
}
return root;
}
int main()
{
Tree *root;
cout << "Enter the root Node, ";
root = BinaryTree();
cout << "Level Order of Binary Tree is " << endl;
LevelOrder(root);
int newValue = 3;
root = Insertion(root, newValue);
cout << "\nLevel Order of Binary Tree after insertion of a new Node (" << newValue << ")" <<
endl;
LevelOrder(root);
return 0;
};
class Tree
{
public:
int data;
Tree *left, *right;
Tree(int data)
{
this->data = data;
left = right = NULL;
}
};
Tree *Insert(Tree *root, int val)
{
if (!root)
{
return new Tree(val);
}
else
{
if (val < root->data)
{
root->left = Insert(root->left, val);
}
else
{