Professional Documents
Culture Documents
#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
// 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;
}
}
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;
#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;
}
}
//
void DeleteEntireTree(struct Node **T)
{
if (*T != NULL)
{
DeleteEntireTree(&((*T)->left));
DeleteEntireTree(&((*T)->right));
struct Node *P = *T;
cout << P->data << endl;
delete (P);
}
}
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 << ' ';
}
}
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;
}
#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;
}
// 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;
}
#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);
}
// 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);