|
| 1 | +/* AVL tree is a self-balancing Binary Search Tree (BST) |
| 2 | + * where the difference between heights of left and right |
| 3 | + * subtrees cannot be more than one for all nodes. |
| 4 | + * We can guarantee an upper bound of O(Logn) for all |
| 5 | + * operations. The height of an AVL tree is always O(Logn) |
| 6 | + * where n is the number of nodes in the tree. |
| 7 | +*/ |
| 8 | + |
| 9 | +#include<bits/stdc++.h> |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +struct Node{ |
| 13 | + int data; |
| 14 | + int height; |
| 15 | + Node *right,*left; |
| 16 | +}; |
| 17 | + |
| 18 | +int height(Node* N){ |
| 19 | + if (N == NULL) |
| 20 | + return 0; |
| 21 | + return N->height; |
| 22 | +} |
| 23 | + |
| 24 | +int getBalance(Node* N){ |
| 25 | + if(N == NULL) |
| 26 | + return 0; |
| 27 | + return height(N->left) - height(N->right); |
| 28 | +} |
| 29 | + |
| 30 | +Node* newNode(int data){ |
| 31 | + Node* temp=new Node; |
| 32 | + temp->data=data; |
| 33 | + temp->height=1; |
| 34 | + temp->right=NULL; |
| 35 | + temp->left=NULL; |
| 36 | + return temp; |
| 37 | +} |
| 38 | + |
| 39 | +Node* rightRotate(Node* y){ |
| 40 | + Node* x=y->left; |
| 41 | + Node* temp=x->right; |
| 42 | + |
| 43 | + x->right=y; |
| 44 | + y->left=temp; |
| 45 | + |
| 46 | + x->height = max(height(x->left), height(x->right))+1; |
| 47 | + y->height = max(height(y->left), height(y->right))+1; |
| 48 | + |
| 49 | + return x;//new root |
| 50 | +} |
| 51 | + |
| 52 | +Node* leftRotate(Node* x){ |
| 53 | + Node* y=x->right; |
| 54 | + Node* temp=y->left; |
| 55 | + |
| 56 | + y->left=x; |
| 57 | + x->right=temp; |
| 58 | + |
| 59 | + x->height = max(height(x->left), height(x->right))+1; |
| 60 | + y->height = max(height(y->left), height(y->right))+1; |
| 61 | + |
| 62 | + return y;//new root |
| 63 | + |
| 64 | +} |
| 65 | + |
| 66 | +Node* insert(Node* temp,int data){ |
| 67 | + if(temp == NULL) |
| 68 | + return newNode(data); |
| 69 | + if(data>temp->data) |
| 70 | + temp->right=insert(temp->right,data); |
| 71 | + else if(data<temp->data) |
| 72 | + temp->left=insert(temp->left,data); |
| 73 | + else |
| 74 | + return temp; //no duplicates |
| 75 | + |
| 76 | + temp->height = 1 + max(height(temp->left), height(temp->right)); |
| 77 | + int balance = getBalance(temp); |
| 78 | + |
| 79 | + //Left Left Case---------Right Rotation |
| 80 | + if(balance > 1 && data < temp->left->data) |
| 81 | + return leftRotate(temp); |
| 82 | + //Right Right Case-------Left Rotation |
| 83 | + if(balance < -1 && data > temp->right->data) |
| 84 | + return rightRotate(temp); |
| 85 | + //Left Right Case--------Left Right Rotation |
| 86 | + if(balance > 1 && data > temp->left->data){ |
| 87 | + temp->left = leftRotate(temp->left); |
| 88 | + rightRotate(temp); |
| 89 | + } |
| 90 | + //Right Left Case--------Right Left Rotation |
| 91 | + if(balance < -1 && data < temp->right->data){ |
| 92 | + temp->right = rightRotate(temp->right); |
| 93 | + leftRotate(temp); |
| 94 | + } |
| 95 | + return temp; |
| 96 | +} |
| 97 | + |
| 98 | +void preOrder(Node *root) |
| 99 | +{ |
| 100 | + if(root != NULL) |
| 101 | + { |
| 102 | + cout<<root->data<<" "; |
| 103 | + preOrder(root->left); |
| 104 | + preOrder(root->right); |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +int main() |
| 109 | +{ |
| 110 | + Node *root = NULL; |
| 111 | + |
| 112 | + root = insert(root, 10); |
| 113 | + root = insert(root, 20); |
| 114 | + root = insert(root, 30); |
| 115 | + root = insert(root, 40); |
| 116 | + root = insert(root, 50); |
| 117 | + root = insert(root, 25); |
| 118 | + |
| 119 | + printf("Preorder traversal of the constructed AVL" |
| 120 | + " tree is \n"); |
| 121 | + preOrder(root); |
| 122 | + |
| 123 | + return 0; |
| 124 | +} |
| 125 | + |
0 commit comments