Advertisement
JonathanA007

Tree 11.9 Latihan (Kelompok 1)

May 13th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.35 KB | Source Code | 0 0
  1. #include <algorithm>
  2. #include <cstdio>
  3. #include <iostream>
  4. #include <sstream>
  5. #define pow2(n) (1 << (n))
  6. using namespace std;
  7. struct avl_node {
  8.   int data;
  9.   struct avl_node *left;
  10.   struct avl_node *right;
  11. } *root;
  12. class avlTree {
  13.  public:
  14.   int height(avl_node *);
  15.   int diff(avl_node *);
  16.   avl_node *rr_rotation(avl_node *);
  17.   avl_node *ll_rotation(avl_node *);
  18.   avl_node *lr_rotation(avl_node *);
  19.   avl_node *rl_rotation(avl_node *);
  20.   avl_node *balance(avl_node *);
  21.   avl_node *insert(avl_node *, int);
  22.   void display(avl_node *, int);
  23.   avlTree() { root = NULL; }
  24. };
  25. // Height of AVL Tree
  26. int avlTree::height(avl_node *temp) {
  27.   int h = 0;
  28.   if (temp != NULL) {
  29.     int l_height = height(temp->left);
  30.     int r_height = height(temp->right);
  31.     int max_height = max(l_height, r_height);
  32.     h = max_height + 1;
  33.   }
  34.   return h;
  35. }
  36. // Height Difference
  37. int avlTree::diff(avl_node *temp) {
  38.   int l_height = height(temp->left);
  39.   int r_height = height(temp->right);
  40.   int b_factor = l_height - r_height;
  41.   return b_factor;
  42. }
  43. // Right- Right Rotation
  44. avl_node *avlTree::rr_rotation(avl_node *parent) {
  45.   avl_node *temp;
  46.   temp = parent->right;
  47.   parent->right = temp->left;
  48.   temp->left = parent;
  49.   return temp;
  50. }
  51. // Left- Left Rotation
  52. avl_node *avlTree::ll_rotation(avl_node *parent) {
  53.   avl_node *temp;
  54.   temp = parent->left;
  55.   parent->left = temp->right;
  56.   temp->right = parent;
  57.   return temp;
  58. }
  59. // Left - Right Rotation
  60. avl_node *avlTree::lr_rotation(avl_node *parent) {
  61.   avl_node *temp;
  62.   temp = parent->left;
  63.   parent->left = rr_rotation(temp);
  64.   return ll_rotation(parent);
  65. }
  66. // Right- Left Rotation
  67. avl_node *avlTree::rl_rotation(avl_node *parent) {
  68.   avl_node *temp;
  69.   temp = parent->right;
  70.   parent->right = ll_rotation(temp);
  71.   return rr_rotation(parent);
  72. }
  73. // Balancing AVL Tree
  74. avl_node *avlTree::balance(avl_node *temp) {
  75.   int bal_factor = diff(temp);
  76.   if (bal_factor > 1) {
  77.     if (diff(temp->left) > 0)
  78.       temp = ll_rotation(temp);
  79.     else
  80.       temp = lr_rotation(temp);
  81.   } else if (bal_factor < -1) {
  82.     if (diff(temp->right) > 0)
  83.       temp = rl_rotation(temp);
  84.     else
  85.       temp = rr_rotation(temp);
  86.   }
  87.   return temp;
  88. }
  89. // Insert Node Kedalam Tree
  90. avl_node *avlTree::insert(avl_node *root, int value) {
  91.   if (root == NULL) {
  92.     root = new avl_node;
  93.     root->data = value;
  94.     root->left = NULL;
  95.     root->right = NULL;
  96.     return root;
  97.   } else if (value < root->data) {
  98.     root->left = insert(root->left, value);
  99.     root = balance(root);
  100.   } else if (value >= root->data) {
  101.     root->right = insert(root->right, value);
  102.     root = balance(root);
  103.   }
  104.   return root;
  105. }
  106. // Menampilkan AVL Tree
  107. void avlTree::display(avl_node *ptr, int level) {
  108.   int i;
  109.   if (ptr != NULL) {
  110.     display(ptr->right, level + 1);
  111.     cout << endl;
  112.     if (ptr == root){
  113.         cout << "Root -> ";
  114.     }
  115.     for (i = 0; i < level && ptr != root; i++){
  116.         cout << "        ";
  117.     }
  118.     cout << ptr->data;
  119.     display(ptr->left, level + 1);
  120.   }
  121. }
  122. int main() {
  123.   avlTree avl;
  124.   int nilai;
  125.   cout << "Masukkan nilai data berikut:" << endl;
  126.   // sisip data 10 bilangan acak dari 0-99 ke dalam tree
  127.   srand(time(NULL));
  128.   for (int i = 0; i < 5; i++) {
  129.     nilai = rand() % 100;
  130.     cout << nilai << " ";
  131.     root = avl.insert(root, nilai);
  132.   }
  133.   avl.display(root, 1);
  134.   return 0;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement