How to make this into an array 1-50

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

class Heap {
public:
    Heap();
    ~Heap();
    void insert(int element);
    int deletemin();
    void print();
    int size() { return heap.size(); }
private:
    int left(int parent);
    int right(int parent);
    int parent(int child);
    void heapifyup(int index);
    void heapifydown(int index);
private:
    vector<int> heap;
};

Heap::Heap()
{
}

Heap::~Heap()
{
}

void Heap::insert(int element)
{
    heap.push_back(element);
    heapifyup(heap.size() - 1);
}

int Heap::deletemin()
{
    int min = heap.front();
    heap[0] = heap.at(heap.size() - 1);
    heap.pop_back();
    heapifydown(0);
    return min;
}

void Heap::print()
{
    vector<int>::iterator pos = heap.begin();
    cout << "Heap = ";
    while ( pos != heap.end() ) {
        cout << *pos << " ";
        ++pos;
    }
    cout << endl;
}

void Heap::heapifyup(int index)
{    
   
    while ( ( index > 0 ) && ( parent(index) >= 0 ) &&
            ( heap[parent(index)] > heap[index] ) )
    {
        int tmp = heap[parent(index)];
        heap[parent(index)] = heap[index];
        heap[index] = tmp;
        index = parent(index);
    }
}

void Heap::heapifydown(int index)
{     
    
    int child = left(index);
    if ( ( child > 0 ) && ( right(index) > 0 ) &&
         ( heap[child] > heap[right(index)] ) )
    {
        child = right(index);
    }
    if ( child > 0 )
    {
        int tmp = heap[index];
        heap[index] = heap[child];
        heap[child] = tmp;
        heapifydown(child);
    }
}

int Heap::left(int parent)
{
    int i = ( parent << 1 ) + 1; // 2 * parent + 1
    return ( i < heap.size() ) ? i : -1;
}

int Heap::right(int parent)
{
    int i = ( parent << 1 ) + 2; // 2 * parent + 2
    return ( i < heap.size() ) ? i : -1;
}

int Heap::parent(int child)
{
    if (child != 0)
    {
        int i = (child - 1) >> 1;
        return i;
    }
    return -1;
}

int main()
{
    // Create the heap
    Heap* myheap = new Heap();
    myheap->insert(50);
    myheap->print();
    
    // Get priority element from the heap
    int heapSize = myheap->size();
    for ( int i = 0; i < heapSize; i++ )
        cout << "Get min element = " << myheap->deletemin() << endl;

    // Cleanup
    delete myheap;
}
How is this different from your other thread?
http://www.cplusplus.com/forum/general/138359/
Its is.. no one was helping me with that..
Last edited on
You will generally get help quicker when you narrow down your problem. Pasting the entire program with a description of "doesn't work" will generally get you very slow to zero help.

So, narrow down your problem (yes, this will require more work and research on your part, this is good for you :) ), write out a decent question, and then you will get more than enough help. And a lot of times, just the act of writing out a decent question will reveal the problem to you. There have been countless times I spent a good 15 minutes writing out a question only to get halfway done and end up uncovering the problem myself.

Being able to write a good question is essential for software developers. Nobody knows everything, so you will be consulting places like this and StackOverflow fairly regularly. Being able to write a good question will only help you. Read these guidelines and try to stick to them: http://stackoverflow.com/help/how-to-ask
Alright my question is i wrote the program to pick numbers from 1-50 and heap.. but for some reason the array is not functioning properly and its only using the number 50, not 1-50
Line 115: What is the purpose of this line? Is it to add 50 elements? Or to add one element with a value of 50? Your code for insert says the later.

If you intend to add 50 nodes, then you need a loop to add each of the nodes.
This program seems to be alright, but why I think you don't understand the code.
Is this the program you code it yourself?

You just add elements, myheap->insert(K);, as many as you want and you'll get your heap tree.
If you want to put all data into array just loop and use delete_min() put to array.
Or if you want to code new using only array, just use linklists in array or use sort function.
Last edited on
Topic archived. No new replies allowed.