|
| 1 | +/********************** |
| 2 | + author: shrutisheoran |
| 3 | +***********************/ |
| 4 | + |
| 5 | +#include <iostream> |
| 6 | +#include <queue> |
| 7 | + |
| 8 | +using namespace std; |
| 9 | + |
| 10 | +typedef struct node { |
| 11 | + int data; |
| 12 | + int height; |
| 13 | + struct node* left; |
| 14 | + struct node* right; |
| 15 | +}node; |
| 16 | + |
| 17 | +int max(int a, int b) { |
| 18 | + return a > b ? a : b; |
| 19 | +} |
| 20 | + |
| 21 | +node* createNode(int data) { |
| 22 | + node *nn = new node(); |
| 23 | + nn->data = data; |
| 24 | + nn->height = 0; |
| 25 | + nn->left = NULL; |
| 26 | + nn->right = NULL; |
| 27 | + return nn; |
| 28 | +} |
| 29 | + |
| 30 | +// Returns height of tree |
| 31 | +int height(node *root) { |
| 32 | + if(root==NULL) |
| 33 | + return 0; |
| 34 | + return 1 + max(height(root->left), height(root->right)); |
| 35 | +} |
| 36 | + |
| 37 | +// Returns difference between height of left and right subtree |
| 38 | +int getBalance(node *root) { |
| 39 | + return height(root->left) - height(root->right); |
| 40 | +} |
| 41 | + |
| 42 | +/************************************ |
| 43 | + Returns Node after Right Rotation |
| 44 | +*************************************/ |
| 45 | +node* rightRotate(node *root) { |
| 46 | + node *t = root->left; |
| 47 | + node *u = t->right; |
| 48 | + t->right = root; |
| 49 | + root->left = u; |
| 50 | + return t; |
| 51 | +} |
| 52 | + |
| 53 | +/************************************ |
| 54 | + Returns Node after Left Rotation |
| 55 | +*************************************/ |
| 56 | +node* leftRotate(node *root) { |
| 57 | + node *t = root->right; |
| 58 | + node *u = t->left; |
| 59 | + t->left = root; |
| 60 | + root->right = u; |
| 61 | + return t; |
| 62 | +} |
| 63 | + |
| 64 | +/************************ |
| 65 | + Balanced Insertion |
| 66 | +************************/ |
| 67 | +node* insert(node* root, int item) { |
| 68 | + node *nn = createNode(item); |
| 69 | + if(root == NULL) |
| 70 | + return nn; |
| 71 | + if(item<root->data) |
| 72 | + root->left = insert(root->left, item); |
| 73 | + else |
| 74 | + root->right = insert(root->right, item); |
| 75 | + int b = getBalance(root); |
| 76 | + if(b>1) { |
| 77 | + if(getBalance(root->left)<0) |
| 78 | + root->left = leftRotate(root->left); // Left-Right Case |
| 79 | + return rightRotate(root); // Left-Left Case |
| 80 | + } |
| 81 | + else if(b<-1) { |
| 82 | + if(getBalance(root->right)>0) |
| 83 | + root->right = rightRotate(root->right); // Right-Left Case |
| 84 | + return leftRotate(root); // Right-Right Case |
| 85 | + } |
| 86 | + return root; |
| 87 | +} |
| 88 | + |
| 89 | +/************************************** |
| 90 | + LevelOrder (Breadth First Search) |
| 91 | +*************************************/ |
| 92 | +void levelOrder(node* root) { |
| 93 | + queue<node*> q; |
| 94 | + q.push(root); |
| 95 | + while(!q.empty()) { |
| 96 | + root = q.front(); |
| 97 | + cout<<root->data<<" "; |
| 98 | + q.pop(); |
| 99 | + if(root->left) |
| 100 | + q.push(root->left); |
| 101 | + if(root->right) |
| 102 | + q.push(root->right); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +int main() { |
| 107 | + node *root = NULL; |
| 108 | + // Testing AVL Tree |
| 109 | + int i; |
| 110 | + for(i = 1 ; i <= 7 ; i++) |
| 111 | + root = insert(root, i); |
| 112 | + cout<<"LevelOrder: "; |
| 113 | + levelOrder(root); |
| 114 | + return 0; |
| 115 | +} |
0 commit comments