0

So I am working on a project to implement a heap and heapsort, but it should be implemented with nodes(with parent, left, right pointers) instead of the usual array implementation.

It seems pretty straightforward, but the one thing I can't figure out is how to figure out where to add a new item to the tree.

In an array implementation, you would simply add the item to the next empty spot in the array (array.length index). However with a node-only implementation, it's not that simple.

I'm sure I could write some algorithm that loops through the entire tree until it finds the next empty spot, but that seems like it would take a ton of unnecessary time.

Does anyone have any ideas?

johnsmith101
  • 179
  • 1
  • 2
  • 11
  • That's a pretty good summary of *why* a heap is implemented with an array. In theory, if you keep track of the number of items in the tree, then you know where the next empty spot is. But before you can add the new child, you'll still need to navigate from the root to the parent. – user3386109 Feb 02 '16 at 02:43
  • If your nodes keep track of the number of nodes under them, you can iterate downward and find the smaller subtree at each point. – ruakh Feb 02 '16 at 02:47
  • Is there a way to convert from the number of items in a tree to the exact traversal a node should take to get to the parent of its end position? Maybe I could use the fact that if there are an odd number of nodes, the next will be a left child, and vice versa – johnsmith101 Feb 02 '16 at 02:49
  • 2
    For example, let's say there are 40 nodes in the tree. That means there are 5 full rows with 1,2,4,8, and 16 nodes respectively, and 9 nodes on the last row. Since 9 is less than half the number of nodes available (32), the traversal goes left from the root. You now have a subtree consisting of 4 full rows, and a last row that can hold 16 nodes. Since 9 is more than half, you go right. At this point you have a subtree with 3 full rows, and 1 node on the last row... – user3386109 Feb 02 '16 at 03:02
  • You can use a queue to store the parent pointers of previous elements, and pop when a parent has 2 children. Extra space is needed, but would be fast for insertion. Even I am curious of a better way. – Aravind Feb 02 '16 at 03:12
  • @user3386109 where do you get 32 nodes available from? Other than that your explanation makes perfect sense – johnsmith101 Feb 02 '16 at 04:20
  • Starting from the first row (which has 1 node, the root), each row has twice as many nodes as the previous row. So if the tree has 5 full rows (1,2,4,8,16), then the next row has room for 32 nodes. In general, the first N rows contain `2^N - 1` nodes, and the last row has room for `2^N` nodes. – user3386109 Feb 02 '16 at 04:29
  • 1
    What @user3386109 suggested is the way to go. You wouldn't get anything for the BigOh of your insert operation, even if you could spot the exact location of the new node immediately; because you still need to bubble up the new value to its correct place in the heap and that is O(logn) already. – Selçuk Cihan Feb 03 '16 at 15:02

0 Answers0