|
| 1 | +// C++ program to insert a node in AVL tree |
| 2 | +#include<bits/stdc++.h> |
| 3 | +using namespace std; |
| 4 | + |
| 5 | +// An AVL tree node |
| 6 | +class Node |
| 7 | +{ |
| 8 | + public: |
| 9 | + int key; |
| 10 | + Node *left; |
| 11 | + Node *right; |
| 12 | + int height; |
| 13 | +}; |
| 14 | + |
| 15 | +// A utility function to get maximum |
| 16 | +// of two integers |
| 17 | +int max(int a, int b); |
| 18 | + |
| 19 | +// A utility function to get the |
| 20 | +// height of the tree |
| 21 | +int height(Node *N) |
| 22 | +{ |
| 23 | + if (N == NULL) |
| 24 | + return 0; |
| 25 | + return N->height; |
| 26 | +} |
| 27 | + |
| 28 | +// A utility function to get maximum |
| 29 | +// of two integers |
| 30 | +int max(int a, int b) |
| 31 | +{ |
| 32 | + return (a > b)? a : b; |
| 33 | +} |
| 34 | + |
| 35 | +/* Helper function that allocates a |
| 36 | +new node with the given key and |
| 37 | +NULL left and right pointers. */ |
| 38 | +Node* newNode(int key) |
| 39 | +{ |
| 40 | + Node* node = new Node(); |
| 41 | + node->key = key; |
| 42 | + node->left = NULL; |
| 43 | + node->right = NULL; |
| 44 | + node->height = 1; // new node is initially |
| 45 | + // added at leaf |
| 46 | + return(node); |
| 47 | +} |
| 48 | + |
| 49 | +// A utility function to right |
| 50 | +// rotate subtree rooted with y |
| 51 | +// See the diagram given above. |
| 52 | +Node *rightRotate(Node *y) |
| 53 | +{ |
| 54 | + Node *x = y->left; |
| 55 | + Node *T2 = x->right; |
| 56 | + |
| 57 | + // Perform rotation |
| 58 | + x->right = y; |
| 59 | + y->left = T2; |
| 60 | + |
| 61 | + // Update heights |
| 62 | + y->height = max(height(y->left), |
| 63 | + height(y->right)) + 1; |
| 64 | + x->height = max(height(x->left), |
| 65 | + height(x->right)) + 1; |
| 66 | + |
| 67 | + // Return new root |
| 68 | + return x; |
| 69 | +} |
| 70 | + |
| 71 | +// A utility function to left |
| 72 | +// rotate subtree rooted with x |
| 73 | +// See the diagram given above. |
| 74 | +Node *leftRotate(Node *x) |
| 75 | +{ |
| 76 | + Node *y = x->right; |
| 77 | + Node *T2 = y->left; |
| 78 | + |
| 79 | + // Perform rotation |
| 80 | + y->left = x; |
| 81 | + x->right = T2; |
| 82 | + |
| 83 | + // Update heights |
| 84 | + x->height = max(height(x->left), |
| 85 | + height(x->right)) + 1; |
| 86 | + y->height = max(height(y->left), |
| 87 | + height(y->right)) + 1; |
| 88 | + |
| 89 | + // Return new root |
| 90 | + return y; |
| 91 | +} |
| 92 | + |
| 93 | +// Get Balance factor of node N |
| 94 | +int getBalance(Node *N) |
| 95 | +{ |
| 96 | + if (N == NULL) |
| 97 | + return 0; |
| 98 | + return height(N->left) - height(N->right); |
| 99 | +} |
| 100 | + |
| 101 | +// Recursive function to insert a key |
| 102 | +// in the subtree rooted with node and |
| 103 | +// returns the new root of the subtree. |
| 104 | +Node* insert(Node* node, int key) |
| 105 | +{ |
| 106 | + /* 1. Perform the normal BST insertion */ |
| 107 | + if (node == NULL) |
| 108 | + return(newNode(key)); |
| 109 | + |
| 110 | + if (key < node->key) |
| 111 | + node->left = insert(node->left, key); |
| 112 | + else if (key > node->key) |
| 113 | + node->right = insert(node->right, key); |
| 114 | + else // Equal keys are not allowed in BST |
| 115 | + return node; |
| 116 | + |
| 117 | + /* 2. Update height of this ancestor node */ |
| 118 | + node->height = 1 + max(height(node->left), |
| 119 | + height(node->right)); |
| 120 | + |
| 121 | + /* 3. Get the balance factor of this ancestor |
| 122 | + node to check whether this node became |
| 123 | + unbalanced */ |
| 124 | + int balance = getBalance(node); |
| 125 | + |
| 126 | + // If this node becomes unbalanced, then |
| 127 | + // there are 4 cases |
| 128 | + |
| 129 | + // Left Left Case |
| 130 | + if (balance > 1 && key < node->left->key) |
| 131 | + return rightRotate(node); |
| 132 | + |
| 133 | + // Right Right Case |
| 134 | + if (balance < -1 && key > node->right->key) |
| 135 | + return leftRotate(node); |
| 136 | + |
| 137 | + // Left Right Case |
| 138 | + if (balance > 1 && key > node->left->key) |
| 139 | + { |
| 140 | + node->left = leftRotate(node->left); |
| 141 | + return rightRotate(node); |
| 142 | + } |
| 143 | + |
| 144 | + // Right Left Case |
| 145 | + if (balance < -1 && key < node->right->key) |
| 146 | + { |
| 147 | + node->right = rightRotate(node->right); |
| 148 | + return leftRotate(node); |
| 149 | + } |
| 150 | + |
| 151 | + /* return the (unchanged) node pointer */ |
| 152 | + return node; |
| 153 | +} |
| 154 | + |
| 155 | +// A utility function to print preorder |
| 156 | +// traversal of the tree. |
| 157 | +// The function also prints height |
| 158 | +// of every node |
| 159 | +void preOrder(Node *root) |
| 160 | +{ |
| 161 | + if(root != NULL) |
| 162 | + { |
| 163 | + cout << root->key << " "; |
| 164 | + preOrder(root->left); |
| 165 | + preOrder(root->right); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +// Driver Code |
| 170 | +int main() |
| 171 | +{ |
| 172 | + Node *root = NULL; |
| 173 | + |
| 174 | + /* Constructing tree given in |
| 175 | + the above figure */ |
| 176 | + root = insert(root, 10); |
| 177 | + root = insert(root, 20); |
| 178 | + root = insert(root, 30); |
| 179 | + root = insert(root, 40); |
| 180 | + root = insert(root, 50); |
| 181 | + root = insert(root, 25); |
| 182 | + |
| 183 | + /* The constructed AVL Tree would be |
| 184 | + 30 |
| 185 | + / \ |
| 186 | + 20 40 |
| 187 | + / \ \ |
| 188 | + 10 25 50 |
| 189 | + */ |
| 190 | + cout << "Preorder traversal of the " |
| 191 | + "constructed AVL tree is \n"; |
| 192 | + preOrder(root); |
| 193 | + |
| 194 | + return 0; |
| 195 | +} |
| 196 | + |
| 197 | +// This code is contributed by |
| 198 | +// rathbhupendra |
0 commit comments