Editing Binary heap

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 56: Line 56:
  
 
==Implementation==
 
==Implementation==
A binary heap is almost always implemented using an [[array]]. Generally the root is placed at index one of the array, and if an element is placed at index <math>k</math>, then its left child is placed at <math>2k</math> and its right at <math>2k+1</math>. Using this system, the root's left child goes at index 2, the root's right child at 3, the root's left child's left child at 4, and so on; indices from <math>2^n</math> to <math>2^{n+1}-1</math> are occupied by elements of depth <math>n</math>, and no space is wasted.
+
A binary heap is almost always implemented using an [[array]]. Generally the root is placed at index zero of the array, and if an element is placed at index <math>k</math>, then its left child is placed at <math>2k</math> and its right at <math>2k+1</math>. Using this system, the root's left child goes at index 2, the root's right child at 3, the root's left child's left child at 4, and so on; indices from <math>2^n</math> to <math>2^{n+1}-1</math> are occupied by elements of depth <math>n</math>, and no space is wasted.
  
 
[[Category:Pages needing diagrams]]
 
[[Category:Pages needing diagrams]]

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)