Binary Tree Printing (Heap Sort)

I am having a ton of trouble using a heap sort to print from a binary tree. Can anyone help me?

This program is supposed to:
1. fill an array with 41 random numbers (the array[0] initialized to 0), sorted using a binary tree.
2. Make a duplicate of that array.
3. Then print the array in order from smallest to largest.

This is my output:

1
2
3
4
numbers[]:
3 -1075236808 43 94 57 73 94 87 70 78 41 93 36 64 68 87 63 91 83 84 68 69 50 37 27 68 59 27 30 60 31 36 63 30 16 23 28 30 22 12 24 12 
spare[]:
3 -1075236960 43 94 57 73 94 87 70 78 41 93 36 64 68 87 63 91 83 84 68 69 50 37 27 68 59 27 30 60 31 36 63 30 16 23 28 30 22 12 24 12 


This is the source code:

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
#include <iostream>

using namespace std;

void FillArray(int array[], int size);
void Duplicate(int first[], int second[], int size);
void Print(int array[], int size);

int main() {
        int size = 42;
        int numbers[42] = {0}, spare[42] = {0};

        FillArray(numbers, size);
        Duplicate(numbers, spare, size);

        cout << "numbers[]:\n";
        Print(numbers, size);

        cout << "\nspare[]:\n";
        Print(spare, size);

        return 0;
}

void FillArray(int array[], int size) {
        int parent = 0, son1 = 0, son2 = 0, smaller = 0;;

        for (int i = 1; i < size; i++) {
                array[i] = rand()%100+1;
                parent = i;
                while (parent / 2 > 0 && array[parent] < array[parent / 2]) {
                        swap(array[parent], array[parent / 2]);
                        parent = parent / 2;
                }
        }
}

void Duplicate(int first[], int second[], int size) {

        for (int i = 0; i < size; i++) {
                second[i] = first[i];
        }

}

void Print(int array[], int size) {
        int parent = 1, son1 = 0, son2 = 0, smaller = 0;

        while (size > 0) {
                cout << array[1] << " ";
                array[1] = array[size];
                size--;
                int done = 0;
                while (done = 0) {
                        son1 = parent * 2;
                        son2 = son1 + 1;
                        if (son1 > size) done = 1;
                        else if (son1 == size) {
                                if (array[parent] > array[son1]) {
                                        swap(array[parent], array[son1]);
                                        done = 1;
                                }
                        }
                        else {
                                smaller = son1;
                                if (array[son1] > array[son2])
                                        smaller = son2;
                                if (array[parent] > array[smaller])
                                        swap(array[parent], array[smaller]);
                        done = 1;
                        }
                }
        }
}
Last edited on
Heap sort is:
1) To make a heap from a set of elements. You do this in FillArray when you add a new element to the last position array[i] = rand()%100+1; and then surface the element unless the heap property is met.
2) To print the heap in the ascending order you must do pop_heap while the heap is not empty. Thus you always take the minimal element from the rest ones.

pop_heap() does the following:
1*.Print (for your purpose) element with index 0.
2. Current_element_index = 0;
3. While (current_element has children and at least one child is smaller) do
array[current_element_index] = mininal child of the node with current_element_index;
current_element index = index of that child;
end do;

So, the hole after removing the root element is drowned until it reaches the bottom of the heap.
Topic archived. No new replies allowed.