You are on page 1of 23

Tree

Program 1: Pre-Order , Post-Order , In-Order


Program 2: Node Count of a given tree
Program 3: Count Leaf Nodes
Program 4: Count N1 nodes
Program 5: count N2 nodes
Program 6: is Complete
Program 7: is strictly
Program 13: Recursive Creation of Binary Tree
Program 15: Sum of all Nodes
Program 17: Tree is identical or not
Program 18 : Mirror Image
Program 24 :height of a tree

#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *left;
struct Node *right;
};
struct Node *GetNode(int x)
{
struct Node *newNode;
newNode = new (struct Node);
newNode->left = NULL;
newNode->right = NULL;
newNode->data = x;
return newNode;
}

// Max function
// return Max value (a & b)
int MAX(int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}

// Power function
// return a^b
int POWER(int a, int b)
{
int p = 1;
for (int i = 0; i < b; i++)
{
p *= a;
}
return p;
}
// Pre Order traversal
void PreOrder(struct Node *T)
{
if (T != NULL)
{
cout << T->data << " ";
PreOrder(T->left);
PreOrder(T->right);
}
}
// PostOrder Traversal
void PostOrder(struct Node *T)
{
if (T != NULL)
{
PostOrder(T->left);
PostOrder(T->right);
cout << T->data << " ";
}
}
// In order traversal
void InOrder(struct Node *T)
{
if (T != NULL)
{
InOrder(T->left);
cout << T->data << " ";
InOrder(T->right);
}
}
// Count Nodes

int CountNode(struct Node *Root)


{
if (Root == NULL)
{
return 0;
}
else
{
return 1 + CountNode(Root->left) + CountNode(Root->right);
}
}
int CountN1Nodes(struct Node *Root)
{
if (Root == NULL)
{
return 0;
}
else if (Root->left == NULL && Root->right != NULL)
{
return 1 + CountN1Nodes(Root->right);
}
else if (Root->left != NULL && Root->right == NULL)
{
return 1 + CountN1Nodes(Root->left);
}
else
{
return CountN1Nodes(Root->left) + CountN1Nodes(Root->right);
}
}
int CountN2Nodes(struct Node *Root)
{
if (Root == NULL)
{
return 0;
}
else if (Root->left != NULL && Root->right != NULL)
{
return 1 + CountN2Nodes(Root->left) + CountN2Nodes(Root->right);
}
else
{
return CountN2Nodes(Root->left) + CountN2Nodes(Root->right);
}
}
int LeafNode(struct Node *Root)
{
if (Root == NULL)
{
return 0;
}
else if (Root->left == NULL && Root->right == NULL)
{
return 1;
}
else
{
return LeafNode(Root->left) + LeafNode(Root->right);
}
}
bool IsComplete(struct Node *Root, int index, int node_Count)
{
if (Root == NULL)
{
return true;
}
else if (index >= node_Count)
{
return false;
}
else
{
return IsComplete(Root->left, 2 * index + 1, node_Count) && IsComplete(Root->right, 2 * index + 2,
node_Count);
}
}

// Height of a Given Tree


int Height(struct Node *T)
{
if (T == NULL)
{
return 0;
}
else if (T->left == NULL && T->left == NULL)
{
return 0;
}
else
{
return 1 + MAX(Height(T->left), Height(T->right));
}
}

// Is Strictly or not
bool IsStrictly(struct Node *T)
{
int c = CountN1Nodes(T);
if (c == 0)
{
return true;
}
else
{
return false;
}
}
bool IsIdentical(struct Node *T1, struct Node *T2)
{
if (T1 == NULL || T2 == NULL)
{
return T1 == T2;
}
return (T1 == T2) && IsIdentical(T1, T2) && IsIdentical(T1, T2);
}
// Mirror Image of a tree
void MirrorImage(struct Node **T)
{
if (T != NULL)
{
struct Node *temp = (*T)->right;
if (temp != NULL)
{
(*T)->right = (*T)->left;
(*T)->left = temp;
MirrorImage(&(*T)->left);
MirrorImage(&(*T)->right);
}
}
}
int SumNodes(struct Node *T)
{
if (T != NULL)
{
return T->data + SumNodes(T->left) + SumNodes(T->right);
}
else
{
return 0;
}
}

// Recursive Creation of a Tree


void BuildTree(struct Node **T)
{
int choice;
int ch;
if (*T == NULL)
{
cout << "Enter the Root Node Element";
cin >> ch;
struct Node *P = GetNode(ch);
*T = P;
BuildTree(&P);
}
else
{
cout << "Do You want to Enter the element in left Node (0/1)" << (*T)->data << endl;
cin >> choice;
if (choice == 1)
{
cout << "Enter Element" << endl;
cin >> ch;
struct Node *P = GetNode(ch);
(*T)->left = P;
BuildTree(&P);
}
cout << "Do You want to Enter the element in right Node (0/1)" << (*T)->data << endl;
cin >> choice;
if (choice == 1)
{
cout << "Enter Element" << endl;
cin >> ch;
struct Node *P = GetNode(ch);
(*T)->right = P;
BuildTree(&P);
}
}
}

int main()
{
struct Node *Root = NULL;
BuildTree(&Root);
cout << endl;
cout << "InOrder Traversal - ";
InOrder(Root);
cout << endl;
cout << "PreOrder Traversal - ";
PreOrder(Root);
cout << endl;
cout << "PostOrder Traversal - ";
PostOrder(Root);
cout << endl;
int count = CountN1Nodes(Root);
cout << "Total Nodes - " << CountNode(Root) << endl;
cout << "Total N1 Nodes - " << count << endl;
cout << "Total N2 Nodes - " << CountN2Nodes(Root) << endl;
cout << "Total Leaf Nodes - " << LeafNode(Root) << endl;
cout << "Is Strict Tree - " << IsStrictly(Root) << endl;
cout << "Is Complete Tree - " << IsComplete(Root, 0, CountNode(Root)) << endl;
cout << "Height of Tree - " << Height(Root) << endl;
cout << "Sum of all Nodes - " << SumNodes(Root) << endl;

// convert Tree to its Mirror Image


MirrorImage(&Root);
PreOrder(Root);
struct Node *Root2 = GetNode('A');

cout << IsIdentical(Root, Root2);


return 0;
}

Program 8 : Level order Traversal


Program 9 : Program for Vertical Traversal
Program 10 : Program for Top View Traversal
Program 11 : Program for Bottom view Traversal
Program 16 : write a program to delete to entire binary tree.
Program 22 : Program to find the diameter of the Binary Tree (distance between the
farthest node)
Program 25 : Program for Left View Traversal
Program 26 : Program for Right View Traversal
Program 30 : Write a Program for BST insertion (using Recursion)
Program : write a program to delete a node in binary tree.

#include <iostream>
using namespace std;
#include "Queue.h"
#include <math.h>
struct Node
{
int data;
int height;
int level;
struct Node *left;
struct Node *right;
struct Node *father;
};

// Queue
struct QNode *Front = NULL;
struct QNode *Rear = NULL;

int HashTableVT[100][100];
struct Node *HashTable[100][100]; // a hash table to map the node data
int maxHeight = 0; // maximum height of the tree
int minLevel = 0, maxLevel = 0; // minimum and maximum level of the B-tree
// GetNode function
// return a node with given data
struct Node *GetNode(int data)
{
struct Node *newNode;
newNode = new (struct Node);
newNode->data = data;
newNode->height = 0;
newNode->level = 0;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}

// Min function
// return Min value (a & b)
int MIN(int a, int b)
{
if (a > b)
{
return b;
}
else
{
return a;
}
}

// Max function
// return Max value (a & b)
int MAX(int a, int b)
{
if (a > b)
{
return a;
}
else
{
return b;
}
}

// Height of The Element


int Height(struct Node *node)
{
int hl = 0, hr = 0;
if (node->left == NULL)
{
hl = 0;
}
else
{
hl = 1 + node->left->height;
}
if (node->right == NULL)
{
hr = 0;
}
else
{
hr = 1 + node->right->height;
}
return max(hl, hr);
}
// Insert in BinarySearchTree
struct Node *BSTInsert(struct Node *node, int x)
{
struct Node *N = node;
if (N == NULL)
{
N = GetNode(x);
node = N;
return N;
}
else
{
if (x < (node)->data)
{
(node)->left = BSTInsert((node)->left, x);
node->left->level = node->level - 1;
node->left->father = node;
node->left->height = 1 + node->height;
maxHeight = MAX(node->left->height, maxHeight);
minLevel = MIN(minLevel, node->left->level);
maxLevel = MAX(maxLevel, node->left->level);
}
else if (x > (node)->data)
{
(node)->right = BSTInsert((node)->right, x);
node->right->level = node->level + 1;
node->right->father = node;
node->right->height = 1 + node->height;
maxHeight = MAX(node->right->height, maxHeight);
minLevel = MIN(minLevel, node->right->level);
maxLevel = MAX(maxLevel, node->right->level);
}
minLevel = MIN(minLevel, node->level);
maxLevel = MAX(maxLevel, node->level);
}
return (node);
}

// // Diameter of the Tree


// int Diameter(struct Node *node)
// {
// }

//
void DeleteEntireTree(struct Node **T)
{
if (*T != NULL)
{
DeleteEntireTree(&((*T)->left));
DeleteEntireTree(&((*T)->right));
struct Node *P = *T;
cout << P->data << endl;
delete (P);
}
}

// Inorder Recursive Traversal


void InOrder(struct Node *T)
{
if (T != NULL)
{
InOrder(T->left);
cout << " " << T->data << " ";
InOrder(T->right);
}
}

// level order Traversal


void LevelOrderTraversal(struct Node *Root)
{
if (Root != NULL)
{
struct Node *P = Root;
EnQueue(&Front, &Rear, P);
while (Front != NULL && Rear != NULL)
{
struct Node *Q = DeQueue(&Front);
cout << Q->data << " ";
if (Q->left != NULL)
{
EnQueue(&Front, &Rear, Q->left);
}
if (Q->right != NULL)
{
EnQueue(&Front, &Rear, Q->right);
}
}
}
}

// To find the minimum element in the given Tree


struct Node *BSTMin(struct Node **T)
{
struct Node *P = *T;
if (P->left == NULL)
{
return P;
}
return BSTMin(&P->left);
}

struct Node *BSTSuccessor(struct Node *P)


{
if ((P)->right != NULL)
{
return BSTMin(&P->right);
}
else
{
struct Node *Q = (P)->father;
while (Q != NULL && Q->right == P)
{
P = Q;
Q = Q->father;
}
return Q;
}
}

bool IsLeft(struct Node *T)


{
if (T == NULL || T->father == NULL)
{
return false;
}
else if (T->father->data > T->data)
{
return true;
}
else
{
return false;
}
}

bool IsRight(struct Node *T)


{
if (T == NULL || T->father == NULL)
{
return false;
}
else if (T->father->data < T->data)
{
return true;
}
else
{
return false;
}
}

int DeleteNode(struct Node *T)


{
if (T->left == NULL && T->right == NULL)
{
struct Node *father = T->father;
if (IsLeft(T))
{
father->left = NULL;
}
else if (IsRight(T))
{
father->right = NULL;
}
int x = T->data;
delete (T);
return x;
}
else if (T->left == NULL || T->right == NULL)
{
struct Node *father = T->father;
struct Node *child = NULL;
if (T->left != NULL)
{
child = T->left;
}
else
{
child = T->right;
}
if (IsLeft(T))
{
father->left = child;
}
else if (IsRight(T))
{
father->right = child;
}
int x = T->data;
delete (T);
return x;
}
else
{
struct Node *S = BSTSuccessor(T);
int x = DeleteNode(S);
T->data = x;
return x;
}
}

void InsertHTelement(struct Node *T)


{
int i = 0;
for (i = 0; i < 100; i++)
{
if (HashTable[T->height][i] == 0)
{
HashTable[T->height][i] = T;
break;
}
}
}

void CreateHashTable(struct Node *Root)


{
Front = NULL;
Rear = NULL;
if (Root != NULL)
{
EnQueue(&Front, &Rear, Root);
}
while (Front != NULL && Rear != NULL)
{
struct Node *temp = DeQueue(&Front);
InsertHTelement(temp);
if (temp->left != NULL)
{
EnQueue(&Front, &Rear, temp->left);
}
if (temp->right != NULL)
{
EnQueue(&Front, &Rear, temp->right);
}
}
}

// Insert in HashTable for vertical traversal


void InsertHTelementVT(struct Node *T)
{
int i = 0;

for (i = 0; i < 10; i++)


{
if (HashTableVT[i][T->level + abs(minLevel)] == 0)
{
HashTableVT[i][T->level + abs(minLevel)] = T->data;
break;
}
}
}

void RightView()
{
for (int i = 0; i <= maxHeight; i++)
{
cout << HashTable[i][0]->data << ' ';
}
}

void LeftView()
{
for (int i = 0; i <= maxHeight; i++)
{
int j = 0;
while (HashTable[i][j] != 0)
j++;
cout << HashTable[i][j - 1]->data << ' ';
}
}

void CreateVerticalOrderHT(struct Node *Root)


{
Front = NULL;
Rear = NULL;
if (Root != NULL)
{
EnQueue(&Front, &Rear, Root);
}
while (Front != NULL && Rear != NULL)
{
struct Node *temp = DeQueue(&Front);
InsertHTelementVT(temp);
if (temp->left != NULL)
{
EnQueue(&Front, &Rear, temp->left);
}
if (temp->right != NULL)
{

EnQueue(&Front, &Rear, temp->right);


}
}
}
void TopView()
{
for (int i = 0; i <= maxLevel + abs(minLevel); i++)
{
cout << HashTableVT[0][i] << " ";
}
}
void BottomView()
{
for (int j = 0; j <= maxLevel + abs(minLevel); j++)
{
int i = 0;
while (HashTableVT[i][j] != 0)
i++;
cout << HashTableVT[i - 1][j] << " ";
}
}
void VerticalOrderTraversal()
{
for (int j = 0; j <= maxLevel + abs(minLevel); j++)
{
int i = 0;
while (HashTableVT[i][j] != 0)
{
cout << HashTableVT[i][j] << " ";
i++;
}
}
}
int main()
{
struct Node *Root = NULL;
int arr[10] = {5, 3, 2, 7, 1, 9, 4, 8, 6};
for (int i = 0; i < 9; i++)
{
Root = BSTInsert(Root, arr[i]);
}
// cout << endl
// << "Diameter of TREE is -> "
// << Diameter(Root) << endl;
// cout << endl
// << "Level Order Traversal : ";
// LevelOrderTraversal(Root);
// cout << DeleteNode(Root->right->right->left) << endl;
// cout << DeleteNode(Root->left) << endl;
// cout << DeleteNode(Root->right) << endl;

InOrder(Root);
CreateHashTable(Root);
cout << endl
<< "Right View : ";
RightView();
cout << endl
<< "Left View : ";
LeftView();
CreateVerticalOrderHT(Root);
cout << endl
<< "Top View : ";

TopView();
cout << endl
<< "Bottom View : ";
BottomView();
cout << endl
<< "Vertical Order Traversal : ";
VerticalOrderTraversal();
// // cout << HashTableVT[0][-2];
return 0;
}

Program : Huffman coding

#include <iostream>
using namespace std;
struct TNode
{
int freq;
char info;
bool unicodes[100];
int len;
struct TNode *left;
struct TNode *right;
struct TNode *next;
};
struct TNode *GetTNode(char x, int f)
{
struct TNode *newNode;
newNode = new (struct TNode);
newNode->left = NULL;
newNode->right = NULL;
newNode->next = NULL;
newNode->info = x;
newNode->freq = f;
newNode->len = 0;
return newNode;
}

void InsBeg(struct TNode **S, struct TNode *N)


{
N->next = *S;
*S = N;
}

// Insert After
void InsAfter(struct TNode **P, struct TNode *N)
{
if (*P == NULL)
{
cout << "Void Insertion" << endl;
exit(1);
}
else
{
N->next = (*P)->next;
(*P)->next = N;
}
}
// Traverse
void Transverse(struct TNode *Start)
{
struct TNode *P = Start;
while (P != NULL)
{
cout << P->info << " " << P->freq << endl;
P = P->next;
}
cout << endl;
}
struct TNode *delbeg(struct TNode **start)
{
if (*start != NULL)
{
struct TNode *Q = *start;
*start = (*start)->next;
return Q;
}
else
{
cout << "Void Deletion";
exit(1);
}
}
int CountNode(struct TNode **Start)
{
struct TNode *P = *Start;
int count = 0;
while (P != NULL)
{
count++;
P = P->next;
}
return count;
}

void Insert(struct TNode **Start, struct TNode *N)


{
struct TNode *P = *Start;
if ((*Start) == NULL)
{
InsBeg(&(*Start), N);
}
else
{
struct TNode *Q = NULL;
while (P != NULL && N->freq >= P->freq)
{
Q = P;
P = P->next;
}
if (Q == NULL)
InsBeg(&(*Start), N);
else
InsAfter(&Q, N);
}
}

void InOrder(struct TNode *T)


{
if (T != NULL)
{
InOrder(T->left);
if (T->left == NULL && T->right == NULL)
{
cout << " " << T->info << " " << T->freq << " ";
for (int i = 0; i <= T->len; i++)
{

cout << T->unicodes[i] << " ";


}
}
cout << endl;
InOrder(T->right);
}
}
struct TNode *BuildHuffmanntree(struct TNode **N)
{
int n = CountNode(N);
struct TNode *Z = NULL;
for (int i = 0; i < n - 1; i++)
{
struct TNode *X = delbeg(N);
struct TNode *Y = delbeg(N);
Z = GetTNode('Z', X->freq + Y->freq);
Z->left = X;
Z->right = Y;
Insert(N, Z);
}
return Z;
}
void Unicodes(struct TNode **N, bool values[], int len)
{
if (*N != NULL)
{
if (len != -1)
{

if ((*N)->left == NULL && (*N)->right == NULL)


{
(*N)->len = len;
for (int i = 0; i <= len; i++)
{
(*N)->unicodes[i] = values[i];
}
}
}
values[len + 1] = false;
Unicodes(&(*N)->left, values, len + 1);
values[len + 1] = true;
Unicodes(&(*N)->right, values, len + 1);
}
}
int main()
{
struct TNode *Start = NULL;
struct TNode *T = NULL;
Insert(&Start, GetTNode('a', 50));
Insert(&Start, GetTNode('r', 70));
Insert(&Start, GetTNode('e', 30));
Insert(&Start, GetTNode('n', 15));
Insert(&Start, GetTNode('t', 15));
Insert(&Start, GetTNode('s', 10));
// Transverse(Start);
T = BuildHuffmanntree(&Start);
bool values[100];
int len = -1;
Unicodes(&T, values, len);
cout << "Info"
<< " "
<< "freq"
<< " "
<< " "
<< "unicodes" << endl;
InOrder(T);
return 0;
}

Program 16 : write a program to delete to entire binary tree.


Program 21 : write a program to implement Insertion and Search operation in BST
(Iterative)
Program 27 : write a program to implement min,max,successor, predecessor in the BST

#include <iostream>
using namespace std;
struct Node
{
int data;
struct Node *left;
struct Node *right;
struct Node *father;
};
struct Node *GetNode(char x)
{
struct Node *newNode;
newNode = new (struct Node);
newNode->left = NULL;
newNode->right = NULL;
newNode->data = x;
newNode->father = NULL;
return newNode;
}
// Search in a binary tree
struct Node *BSTsearch(struct Node **T, int key)
{
struct Node *P = *T;
if (P == NULL)
{
return NULL;
}
if (P->data == key)
{
return P;
}
else if (P->data > key)
{
P = P->left;
}
else
{
P = P->right;
}
return BSTsearch(&P, key);
}
// Insert In a Binary tree
void BSTInsert(struct Node **T, int x)
{
struct Node *N = GetNode(x);
struct Node *Q = NULL;
struct Node *P = *T;
while (P != NULL)
{
Q = P;
if (P->data > x)
{
P = P->left;
}
else
{
P = P->right;
}
}
if (Q == NULL)
{
*T = N;
}
else
{
if (Q->data > x)
{
Q->left = N;
Q->left->father = Q;
}
else
{
Q->right = N;
Q->right->father = Q;
}
}
}
// Maximum in binary tree
struct Node *BSTMax(struct Node **T)
{
struct Node *P = *T;
if (P->right == NULL)
{
return P;
}
return BSTMax(&P->right);
}
// minimum in Binary tree
struct Node *BSTMin(struct Node **T)
{
struct Node *P = *T;
if (P->left == NULL)
{
return P;
}
return BSTMin(&P->left);
}

// Deleteion of a Binary tree(Entire Tree)


void DeleteEntireTree(struct Node **T)
{
if (*T != NULL)
{
DeleteEntireTree(&((*T)->left));
DeleteEntireTree(&((*T)->right));
struct Node *P = *T;
cout << P->data << endl;
delete (P);
}
}

// InOrder traversal
void InOrder(struct Node *T)
{
if (T != NULL)
{
InOrder(T->left);
cout << T->data << " ";
InOrder(T->right);
}
}
struct Node *BSTSuccessor(struct Node *P)
{
if ((P)->right != NULL)
{
return BSTMin(&P->right);
}
else
{
struct Node *Q = (P)->father;
while (Q != NULL && Q->right == P)
{
P = Q;
Q = Q->father;
}
return Q;
}
}
struct Node *BSTpred(struct Node *P)
{
if ((P)->left != NULL)
{
return BSTMax(&(P)->left);
}
else
{
struct Node *Q = (P)->father;
while (Q != NULL && Q->left == P)
{
P = Q;
Q = Q->father;
}
return Q;
}
}
int main()
{
struct Node *Root = NULL;
BSTInsert(&Root, 2);
BSTInsert(&Root, 1);
BSTInsert(&Root, 3);
BSTInsert(&Root, 4);
BSTInsert(&Root, 0);

struct Node *P = BSTsearch(&Root, 1);


if (P == NULL)
{
cout << "Not Found !!!" << endl;
}
else
{
cout << "Found " << P->data << " " << P << endl;
}
struct Node *Q = BSTMin(&Root);
cout << Q->data << endl;
;
Q = BSTMax(&Root);
cout << Q->data << endl;
InOrder(Root);
cout << BSTSuccessor(Root->left)->data;
return 0;
}

You might also like