// Program For Creation Of Binary Tree (Iterative way)
#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;
}
// Program For Creation Of Binary Tree (using Recursion)
#include <iostream>
using namespace std;
class Tree
{
public:
int data;
Tree *left, *right;
Tree(int data)
{
this->data = data;
left = right = NULL;
}
};
Tree *BinaryTree()
{
int x;
cout << "enter (-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;
}
int main()
{
cout << "Enter the root node,";
Tree *root = BinaryTree();
return 0;
}
// Program for Traversal of Binary Tree (Iterative method)
#include <iostream>
#include <queue>
#include <stack>
using namespace std;
// Class to represent a binary tree node
class Tree
{
public:
int data;
Tree *left, *right;
Tree(int data)
{
this->data = data;
left = right = NULL;
}
};
// Function to construct the binary tree iteratively
Tree *BinaryTree()
{
int x;
cout << "Enter root node (-1 for NULL): ";
cin >> x;
if (x == -1)
return NULL;
queue<Tree *> q;
Tree *root = new Tree(x);
q.push(root);
while (!q.empty())
{
Tree *temp = q.front();
q.pop();
// Input for left child
cout << "Enter left child of " << temp->data << " (-1 for NULL): ";
cin >> x;
if (x != -1)
{
temp->left = new Tree(x);
q.push(temp->left);
}
// Input for right child
cout << "Enter right child of " << temp->data << " (-1 for NULL): ";
cin >> x;
if (x != -1)
{
temp->right = new Tree(x);
q.push(temp->right);
}
}
return root;
}
// Iterative PreOrder Traversal
void PreOrder(Tree *root)
{
if (!root)
return;
stack<Tree *> s;
s.push(root);
cout << "PreOrder Traversal: ";
while (!s.empty())
{
Tree *temp = s.top();
s.pop();
cout << temp->data << " ";
// Push right child first, so left child is processed first
if (temp->right)
s.push(temp->right);
if (temp->left)
s.push(temp->left);
}
cout << endl;
}
// Iterative PostOrder Traversal
void PostOrder(Tree *root)
{
if (!root)
return;
stack<Tree *> s1, s2;
s1.push(root);
// Collect nodes in reverse postorder
while (!s1.empty())
{
Tree *temp = s1.top();
s1.pop();
s2.push(temp);
if (temp->left)
s1.push(temp->left);
if (temp->right)
s1.push(temp->right);
}
// Print nodes in postorder
cout << "PostOrder Traversal: ";
while (!s2.empty())
{
cout << s2.top()->data << " ";
s2.pop();
}
cout << endl;
}
// Iterative InOrder Traversal
void InOrder(Tree *root)
{
if (!root)
return;
stack<Tree *> s;
Tree *current = root;
cout << "InOrder Traversal: ";
while (!s.empty() || current != NULL)
{
// Traverse left subtree
while (current != NULL)
{
s.push(current);
current = current->left;
}
// Visit the node
current = s.top();
s.pop();
cout << current->data << " ";
// Traverse right subtree
current = current->right;
}
cout << endl;
}
int main()
{
cout << "Create a binary tree:" << endl;
Tree *root = BinaryTree();
if (!root)
{
cout << "Tree is empty. No traversal is possible." << endl;
return 0;
}
// Perform tree traversals
PreOrder(root);
PostOrder(root);
InOrder(root);
return 0;
}
// Program for Treversal of Binary Tree (using Recursion)
#include <iostream>
using namespace std;
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;
}
// Program for insertion of new Node into Binary Tree.
#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 *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;
};
// Program for Insertion of nodes into Binary Search Tree
#include <iostream>
using namespace std;
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
{
root->right = Insert(root->right, val);
}
return root;
}
}
void INOrder(Tree *root)
{
if (root == NULL)
return;
INOrder(root->left);
cout << root->data << " ";
INOrder(root->right);
}
int main()
{
Tree *root = NULL;
root = Insert(root, 5);
root = Insert(root, 2);
root = Insert(root, 1);
root = Insert(root, 9);
root = Insert(root, 10);
root = Insert(root, 3);
INOrder(root);
return 0;
}