Min heap without recursion The code reads in integer array from user input and builds a minimum heap via an iterative scheme.
Are there any edge cases in which this approach would fail?
For the following test case/command sequence:
1 4 3 10 12 6 -1
remove
remove
print

My output is 4 6 10 12 instead of 4 10 6 12, is this an invalid implementation?
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
#include &lt;stdexcept&gt;
#include &lt;cmath&gt;

int readheap(int* theheap) {
    std::string num;
    int val = 0;
    int size = 0;
    std::cout &lt;&lt; &quot;Enter the elements of heap&quot; &lt;&lt; std::endl;
    std::getline(std::cin, num);
    std::istringstream iss(num);    
    while (iss &gt;&gt; val) {
        if (val != ' ' &amp;&amp; val &gt; 0){
            theheap[size] = val;
            ++size;
        }
    }
    if(size &lt;= 1) {
        throw std::runtime_error(&quot;Invalid user input&quot;);
    }

for (int k = 1; k &lt; size; ++k) {

        if (theheap[k] &gt; theheap[(k - 1) / 2])  { 
            int j = k; 

            while (theheap[j] &gt; theheap[(j - 1) / 2])  { 
                std::swap(theheap[j], theheap[(j - 1) / 2]); 
                j = (j - 1) / 2; 
            }
        }
    }
    for (int k = size - 1; k &gt; 0; --k) {
        std::swap(theheap[0], theheap[k]);           

        int j = 0, index; 

        do { 
            index = (2 * j + 1);  

            if (theheap[index] &lt; theheap[index + 1] &amp;&amp; index &lt; (k - 1)) 
                ++index; 

            if (theheap[j] &lt; theheap[index] &amp;&amp; index &lt; k) 
                std::swap(theheap[j], theheap[index]); 

            j = index; 

        } while (index &lt; k); 
    } 
    std::cout &lt;&lt; &quot;Size of heap is &quot; &lt;&lt; size &lt;&lt; '\n';
    return size;
}

void heapRemove(int* theheap, int&amp; size) {
    for(int i=0; i&lt;size-1; ++i){
        theheap[i] = theheap[i+1];
    }
    int* x = theheap;
    x = nullptr;
    delete x;
    size--; 
    for (int k = 1; k &lt; size; ++k) {

        if (theheap[k] &gt; theheap[(k - 1) / 2])  { 
            int j = k; 

            while (theheap[j] &gt; theheap[(j - 1) / 2])  { 
                std::swap(theheap[j], theheap[(j - 1) / 2]); 
                j = (j - 1) / 2; 
            }
        }
    }
    for (int k = size - 1; k &gt; 0; --k) {  
        std::swap(theheap[0], theheap[k]);      

        int j = 0, index; 

        do { 
            index = (2 * j + 1);  

            if (theheap[index] &lt; theheap[index + 1] &amp;&amp; index &lt; (k - 1)) 
                ++index; 

            if (theheap[j] &lt; theheap[index] &amp;&amp; index &lt; k) 
                std::swap(theheap[j], theheap[index]); 

            j = index; 

        } while (index &lt; k); 
    }
}

void heapPrint(int* theheap, int size) {
    for (int i = 0; i &lt; size; ++i){
        std::cout &lt;&lt; theheap[i] &lt;&lt; ' ';
    }
    std::cout &lt;&lt; '\n';
}

int main() {
    int* theheap = new int[10];
    int size = readheap(theheap);
    heapPrint(theheap, size);
    heapRemove(theheap, size);
    heapPrint(theheap, size);
    std::cout &lt;&lt; size &lt;&lt; std::endl;
    return 0;
}
Memory over-runs

readheap is non-size-aware, so it's wide open to an overrun error (or even a deliberate overrun attack). Pass in a size, or use a sized data structure like a vector.

Indentation

You should fix it up for this line:

for (int k = 1; k &lt; size; ++k) {


Order-of-operations

Neither of these expressions need parentheses:

index = (2 * j + 1);  

index &lt; (k - 1)


Deleting a null?

x = nullptr;
delete x;


Does that actually run without crashing?

Const arguments

void heapPrint(int* theheap, int size) {


should be

void heapPrint(const int *theheap, int size) {