patterncsharpMinor
Linked list implementation of binary heap insertion
Viewed 0 times
insertionimplementationheapbinarylistlinked
Problem
Is this a correct linked list implementation of binary heap insertion? This is not a homework exercise.
public BinaryHeap insert(int value , BinaryHeap node , int count)
{
//1. Insert into a binary tree breadth wise( satisfy complete binary property) (left to right)
//2. Once inserted ,fix the tree by running heapify every time a new value(non duplicate) is added.
//3. Count represents the total no of elements in the tree. Calculate the total no of parents for given set of elements.
int parent = (count - 1)/2;
if (node == null)
{
node = new BinaryHeap();
node.data = value;
}
else
{
if (((parent == 0) && (node.left == null)) || (parent % 2 == 1))
{
node.left = insert(value, node.left, parent);
Debug.Assert(node.left != null);
node.left.parent = node;
Heapify(node.left);
if (node.right != null) // if both left and right sub tree are filled
{
Debug.Assert(node.data node.data)
{
//swap the value
int temp = node.parent.data;
node.parent.data = node.data;
node.data = temp;
node = node.parent;
Heapify(node.parent);
return;
}
}
}// end of function heapifySolution
It's always worth tidying up indentation and removing random blank lines (e.g. after
Naming: in C# it's conventional that method names are in UpperCamelCase, but even if you choose not to follow the convention it's important to be consistent. Here
The name
It's not clear how
What does this represent? Elsewhere
Why? I need some comments somewhere in the code to say how the tree is structured. This looks like an attempt to keep it balanced, but I'm not convinced that it works. (I suspect it should check the parity of
I don't understand the necessity of all those asserts, but IMO it would be cleaner to remove the duplication by pulling them out of the cases as
or
The recursive
Why is
creates unnecessary indentation and vertical spacing, both of which make it harder to read the code. Rewrite as
{).Naming: in C# it's conventional that method names are in UpperCamelCase, but even if you choose not to follow the convention it's important to be consistent. Here
Heapify follows the convention but insert doesn't.The name
Heapify usually refers to taking an unstructured array of data and structuring it as a heap in linear time. The method implemented here should probably have a name like UpHeap.It's not clear how
insert is supposed to be used. Is it called passing the root of the tree? And where does count come from? It seems to me that BinaryHeap should be BinaryHeapNode and should be a private inner class of a public BinaryHeap which exposes public void Insert(int value).int parent = (count - 1)/2;What does this represent? Elsewhere
parent is a BinaryHeap, and it's obvious what it means, but as far as I can guess this variable is actually the size of the right subtree.if (((parent == 0) && (node.left == null)) || (parent % 2 == 1))Why? I need some comments somewhere in the code to say how the tree is structured. This looks like an attempt to keep it balanced, but I'm not convinced that it works. (I suspect it should check the parity of
count rather than parent). Maybe if I knew precisely what it was trying to do I could be convinced.I don't understand the necessity of all those asserts, but IMO it would be cleaner to remove the duplication by pulling them out of the cases as
if (node.left != null)
{
Debug.Assert(node.data < node.left.data);
}
if (node.right != null)
{
Debug.Assert(node.data < node.right.data);
}or
Debug.Assert(node.left == null || node.data < node.left.data);
Debug.Assert(node.right == null || node.data < node.right.data);The recursive
insert calls Heapify at each level that it touches. Heapify bubbles up. That means there's unnecessary duplication of work here.Why is
Heapify public?if (condition)
{
DoStuff();
}
else
{
if (otherCondition)
{
OtherStuff();
}
}creates unnecessary indentation and vertical spacing, both of which make it harder to read the code. Rewrite as
if (condition)
{
DoStuff();
}
else if (otherCondition)
{
OtherStuff();
}Code Snippets
int parent = (count - 1)/2;if (((parent == 0) && (node.left == null)) || (parent % 2 == 1))if (node.left != null)
{
Debug.Assert(node.data < node.left.data);
}
if (node.right != null)
{
Debug.Assert(node.data < node.right.data);
}Debug.Assert(node.left == null || node.data < node.left.data);
Debug.Assert(node.right == null || node.data < node.right.data);if (condition)
{
DoStuff();
}
else
{
if (otherCondition)
{
OtherStuff();
}
}Context
StackExchange Code Review Q#145217, answer score: 3
Revisions (0)
No revisions yet.