public void Add(int newKey) //Add newKey to the heap { int i = stack.Length(); // index of the new key stack.Push(newKey); //add the into the while (i >= 2) { if (stack[i] > stack[i / 2])// The left child never smaller than the right one //Thus exchange the node with its left child { Exch(i, i / 2); i /= 2; } else { break; } } Debug.Assert(isVerified()); }
StackByResizeableArray <int> stack;// where the data is stored //Left child of stack[i]: stack[2*i] //Right child of stack[i]: stack[2*i+1] BinaryHeap(int root) { stack = new StackByResizeableArray <int>(); stack.Push(0); stack.Push(root); }