Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void Binary_heap::insert(int data)
- {
- if (!head)
- head = new Node(data);
- else
- add_elem(data, head, height, position);
- position++;
- if (position == pow(2, height))
- {
- height++;
- position = 0;
- }
- }
- void Binary_heap::add_elem(int data, Node* root, int height, int position)
- {
- if (data > root->get_data())
- {
- int temp = data;
- data = root->get_data();
- root->set_data(temp);
- }
- if (!root->get_left() || !root->get_right())
- {
- if (!root->get_left())
- root->set_left(new Node(data));
- else
- root->set_right(new Node(data));
- }
- else
- {
- if (position <= (pow(2, height)) / 2 - 1)
- {
- add_elem(data, root->get_left(), height - 1, position);
- }
- else
- {
- add_elem(data, root->get_right(), height - 1, position - pow(2, height - 1));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement