Skip to main content
deleted 1 character in body
Source Link
Nathaniel
  • 18.5k
  • 2
  • 31
  • 59

To be honest, that's quite a bad definition…

What I would recommand is understanding the abstract representation of a heap with a binary tree before trying to work on the practical implementation in an array.

A nearly-perfect tree is a binary tree such that all levels are filled with nodes, except maybe for the last level, that is filled from the left. Without labels, there is only one nearly-perfect tree of any given size, because when adding a new node, there is only one possible position (either the next position from the left on the last level, or as a left child of the leftmost leaf if the last level is full).

A tourney tree (or often called a tree with the heap property) is a binary tree such that each node is greater or equal than its children.

A heap is a nearly-perfect tree and a tourney tree.

Once that's defined, a very usual way to implement heaps areis using an array:

  • the root is at index $0$;
  • the left child of the root is at index $1$;
  • the right child of the root is at index $2$;
  • the left child of the left child of the root is at index $3$;
  • the right child of the left child of the root is at index $4$;
  • the left child of the node at index $i$ is at index $2i + 1$;
  • the right child of the node at index $i$ is at index $2i + 2$.

So an element at index $i$ is, by definition, greater or equal than, if they exist, its children, at indices $2i+1$ and $2i+2$.

Here, the condition "if they exist" is obviously necessary to consider only finite arrays.

To be honest, that's quite a bad definition…

What I would recommand is understanding the abstract representation of a heap with a binary tree before trying to work on the practical implementation in an array.

A nearly-perfect tree is a binary tree such that all levels are filled with nodes, except maybe for the last level, that is filled from the left. Without labels, there is only one nearly-perfect tree of any given size, because when adding a new node, there is only one possible position (either the next position from the left on the last level, or as a left child of the leftmost leaf if the last level is full).

A tourney tree (or often called a tree with the heap property) is a binary tree such that each node is greater or equal than its children.

A heap is a nearly-perfect tree and a tourney tree.

Once that's defined, a very usual way to implement heaps are using array:

  • the root is at index $0$;
  • the left child of the root is at index $1$;
  • the right child of the root is at index $2$;
  • the left child of the left child of the root is at index $3$;
  • the right child of the left child of the root is at index $4$;
  • the left child of the node at index $i$ is at index $2i + 1$;
  • the right child of the node at index $i$ is at index $2i + 2$.

So an element at index $i$ is, by definition, greater or equal than, if they exist, its children, at indices $2i+1$ and $2i+2$.

Here, the condition "if they exist" is obviously necessary to consider only finite arrays.

To be honest, that's quite a bad definition…

What I would recommand is understanding the abstract representation of a heap with a binary tree before trying to work on the practical implementation in an array.

A nearly-perfect tree is a binary tree such that all levels are filled with nodes, except maybe for the last level, that is filled from the left. Without labels, there is only one nearly-perfect tree of any given size, because when adding a new node, there is only one possible position (either the next position from the left on the last level, or as a left child of the leftmost leaf if the last level is full).

A tourney tree (or often called a tree with the heap property) is a binary tree such that each node is greater or equal than its children.

A heap is a nearly-perfect tree and a tourney tree.

Once that's defined, a very usual way to implement heaps is using an array:

  • the root is at index $0$;
  • the left child of the root is at index $1$;
  • the right child of the root is at index $2$;
  • the left child of the left child of the root is at index $3$;
  • the right child of the left child of the root is at index $4$;
  • the left child of the node at index $i$ is at index $2i + 1$;
  • the right child of the node at index $i$ is at index $2i + 2$.

So an element at index $i$ is, by definition, greater or equal than, if they exist, its children, at indices $2i+1$ and $2i+2$.

Here, the condition "if they exist" is obviously necessary to consider only finite arrays.

Source Link
Nathaniel
  • 18.5k
  • 2
  • 31
  • 59

To be honest, that's quite a bad definition…

What I would recommand is understanding the abstract representation of a heap with a binary tree before trying to work on the practical implementation in an array.

A nearly-perfect tree is a binary tree such that all levels are filled with nodes, except maybe for the last level, that is filled from the left. Without labels, there is only one nearly-perfect tree of any given size, because when adding a new node, there is only one possible position (either the next position from the left on the last level, or as a left child of the leftmost leaf if the last level is full).

A tourney tree (or often called a tree with the heap property) is a binary tree such that each node is greater or equal than its children.

A heap is a nearly-perfect tree and a tourney tree.

Once that's defined, a very usual way to implement heaps are using array:

  • the root is at index $0$;
  • the left child of the root is at index $1$;
  • the right child of the root is at index $2$;
  • the left child of the left child of the root is at index $3$;
  • the right child of the left child of the root is at index $4$;
  • the left child of the node at index $i$ is at index $2i + 1$;
  • the right child of the node at index $i$ is at index $2i + 2$.

So an element at index $i$ is, by definition, greater or equal than, if they exist, its children, at indices $2i+1$ and $2i+2$.

Here, the condition "if they exist" is obviously necessary to consider only finite arrays.