Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.78 KB | None | 0 0
  1. void Binary_heap::insert(int data)
  2. {
  3.     if (!head)
  4.         head = new Node(data);
  5.     else
  6.         add_elem(data, head, height, position);
  7.     position++;
  8.     if (position == pow(2, height))
  9.     {
  10.         height++;
  11.         position = 0;
  12.     }
  13. }
  14.  
  15. void Binary_heap::add_elem(int data, Node* root, int height, int position)
  16. {
  17.     if (data > root->get_data())
  18.     {
  19.         int temp = data;
  20.         data = root->get_data();
  21.         root->set_data(temp);
  22.     }
  23.     if (!root->get_left() || !root->get_right())
  24.     {
  25.         if (!root->get_left())
  26.             root->set_left(new Node(data));
  27.         else
  28.             root->set_right(new Node(data));
  29.     }
  30.     else
  31.     {
  32.         if (position <= (pow(2, height)) / 2 - 1)
  33.         {
  34.             add_elem(data, root->get_left(), height - 1, position);
  35.         }
  36.         else
  37.         {
  38.             add_elem(data, root->get_right(), height - 1, position - pow(2, height - 1));
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement