Pergunta

#include <iostream>
using namespace std;



int getParent(int);
int getLeftChild(int);
int getRightChild(int);
void swap(int&, int&);
void heapify(int A[], int);
void build_heap(int A[]);
void printArray(int A[], int);
void heapInsert(int A[], int);

int main()

{
int parent;
int right;
int left;
int A[10]={4,1,3,2,16,9,10,14,8,7};



heapify(A,3);

cout << "Print Array A:" << endl;
printArray(A, 10);
cout << endl;
build_heap(A);
parent=getParent(5);
left=getLeftChild(3);
right=getRightChild(3);
cout<<"Parent of node 5 is the node " << parent << endl;
cout<<"Left child of node 3 is the node " << left << endl;
cout<<"Left child of node 3 is the node " << right <<endl << endl;
cout << "Print Heap A:" << endl;
printArray(A, 10);
cout << endl;
cout << "After inserting the number 20:" << endl;
heapInsert(A, 20);
build_heap(A);
printArray(A, 11);
cout << endl;
cout << "After inserting the number 17:" << endl;
heapInsert(A, 17);
build_heap(A);
printArray(A, 12);

system("pause");
return 0;


}

void heapInsert(int A[], int Item)
{
 int i = 10;
 A[i] ++;
 i = A[i];
  while (i > 1  &&  A[getParent(i)] < Item)
    {
        A[i] = A[getParent(i)];
        i = getParent(i);
    }
    A[i] = Item;
}

The problem comes when I try to increase the size of the heap and insert a new value using the heapInsert function call. All of my other functions execute fine, but I have no idea where to go from here.

Foi útil?

Solução

int A[10]={4,1,3,2,16,9,10,14,8,7};

and

int i = 10;
 A[i] ++;

The biggest index for array A is 9.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top