1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
#include<iostream>
using namespace std;
struct AVL {
int height;
int data;
AVL *left;
AVL *right;
};
AVL *root = NULL;
int max(int num1, int num2) {
return (num1 > num2) ? num1 : num2;
}
int height(AVL *root) {
if (root == NULL)
{
return 0;
}
root->height = max(height(root->left), height(root->right)) + 1;
return root->height;
}
int balance(AVL *root) {
if (root == NULL) {
return 0;
}
return (height(root->left) - height(root->right));
}
AVL *rotate_right(AVL *root) {
AVL *newroot = root->left;
root->left = newroot->right;
newroot->right = root;
newroot->height = max(height(newroot->left), height(newroot->right)) + 1;
root->height = max(height(root->left), height(root->right)) + 1;
return newroot;
}
AVL *rotate_left(AVL *root) {
AVL *newroot = root->right;
root->right = newroot->left;
newroot->left = root;
newroot->height = max(height(newroot->left), height(newroot->right)) + 1;
root->height = max(height(root->left), height(root->right)) + 1;
return newroot;
}
AVL *insert(AVL *root, AVL *node) {
if (root == NULL) {
root = node;
}
else if (node->data < root->data) {
root->left = insert(root->left, node);
}
else if (node->data > root->data) {
root->right = insert(root->right, node);
}
else {
return root;
}
int b = balance(root);
root->height = 1 + max(height(node->left), height(node->right));
if (b > 1 && node->data < root->left->data) {
return rotate_right(root);
}
else if (b > 1 && node->data > root->left->data) {
root->left = rotate_left(root->left);
return rotate_right(root);
}
else if (b < -1 && node->data > root->right->data) {
return rotate_left(root);
}
else if (b < -1 && node->data < root->right->data) {
root->right = rotate_left(root->right);
return rotate_left(root);
}
return root;
}
void show(AVL *root) {
if (root == NULL) {
return;
}
else {
show(root->left);
cout << "Height " << root->height;
cout << "\tData " << root->data << endl;
show(root->right);
}
}
void printKDistant(AVL *root, int k)
{
if (root == NULL)
return;
if (k == 0)
{
if (root->left == NULL && root->right == NULL)
{
cout << "NODE WITH DATA" << root->data << " has least children ";
}
else if (root->left == NULL || root->right == NULL)
{
cout << "NODE WITH DATA " << root->data << " has one children" << endl;
}
if (root->left != NULL && root->right != NULL)
{
cout << "NODE WITH DATA " << root->data << " has max children" << endl;
}
return;
}
else
{
printKDistant(root->left, k - 1);
printKDistant(root->right, k - 1);
}
}
int main()
{
int num,height;
cout << "How Many Nodes You Want to Create: ";
cin >> num;
for (int i = 0; i < num; i++)
{
AVL *node = new AVL;
cout << "Enter Data:- ";
cin >> node->data;
node->left = NULL;
node->right = NULL;
node->height = 1;
root = insert(root, node);
}
cout << "Enter height at which you want to check the childrens of respected nodes" << endl;
cin >> height;
printKDistant(root, height);
system("pause");
}
|