I am debugging a Binary Tree Heap code. It hung at getNodeIndex() when it is hit the top of the heap, which is called by percolateUp(). Please teach me why this happened and how to fix it? Thank you!
The output is:
after it insert 0, and move 0 to the top, it show inorder, then it prompted:
N1: the node index is 005479E0 The node value is 0
Print Node index after getNodeIndex: 005479E0
// data fields
T value;
node<T>* parent;
node<T>* leftChild;
node<T>* rightChild;
};
template<class T> int node<T>::size()
// count number of elements in subtree rooted at node
{
int count = 1;
if (leftChild != NULL)
count += leftChild->size();
if (rightChild != NULL)
count += rightChild->size();
return count;
}
template<class T>
void node<T>::insert(node<T>* newNode)
// insert a new element into a binary search tree
{
//if (newElement < value)
if (newNode->value < value)
if (leftChild != NULL)
leftChild->insert(newNode);
else {
newNode->parent = this;
leftChild = newNode;
}
else
if (rightChild != NULL)
rightChild->insert(newNode);
else {
newNode->parent = this;
rightChild = newNode;
}
}
template <class T>
node<T> * node<T>::merge(node<T> * left, node<T> * right)
// merge two subtrees into one
{
if (left == NULL)
return right;
if (right == NULL)
return left;
node<T> * child = merge(left, right->leftChild);
child->parent = right;
right->leftChild = child;
return right;
}
if (root->rightChild != NULL){
postorder(root->rightChild, leadings);
}
cout << leadings << root->value << endl;
}
template<typename T>
node<T>* getNodeIndex(node<T>* heap, T svalue)
{
if (heap->value == svalue)
{
cout << "N1: The node index is " << heap << " The node value is: " << heap->value << "reach heap root. " << endl;
HIT_HEAP_ROOT = true;
exit 0;
}
else if ( svalue < heap->value)
{
if (heap->leftChild != NULL)
{
if (heap->leftChild->value == svalue)
{
cout << "N2: The node index is " << heap->leftChild << " The node value is: " << heap->leftChild->value << endl;
return heap->leftChild;
}
else
return getNodeIndex(heap->leftChild, svalue);
}
}
else if (svalue > heap->value)
{
if (heap->rightChild != NULL)
{
if (heap->rightChild->value == svalue)
{
cout << "N3: The node index is " << heap->rightChild->parent << "The node value is: " << heap->rightChild->value << endl;
return heap->rightChild;
}
else
return getNodeIndex(heap->rightChild, svalue);
}
}
}
template<typename T>
node<T>* getParentIndex(node<T>* heap, T svalue)
{
if (svalue == heap->value)
{
cout << "P1: The parent index is " << heap->parent << " parent value is: " << heap->parent->value << "reach heap root. " << endl;
HIT_HEAP_ROOT = true;
exit 0;
}
else if (svalue < heap->value )
{
if (heap->leftChild != NULL)
{
if (heap->leftChild->value == svalue)
{
cout << "P2: The parent index is " << heap->leftChild->parent << " parent value is: " << heap->leftChild->parent->value << endl;
return heap->leftChild->parent;
}
else
return getParentIndex(heap->leftChild, svalue);
}
}
else if (svalue > heap->value)
{
if (heap->rightChild != NULL)
{
if (heap->rightChild->value == svalue)
{
cout << "P3: The parent index is " << heap->rightChild->parent << " parent value is: " << heap->rightChild->parent->value << endl;
return heap->rightChild->parent;
}
else
return getParentIndex(heap->rightChild, svalue);
}
cout << "Please type in an int as random seeds." << endl;
cin >> seed;
srand(seed);
heap1 = new node<int>((int)100);
data[0] = 100;
for (index = 1; index <= 10; index++)
{
data[index] = rand() % 100 + 1;
// insert1(index);
heap1->insert(new node<int>(data[index]));
cout << "insert " << data[index] << "at position " << index << " ";
}
cout << "The heap1 tree inorder is: " << endl;
inorder(heap1, "");
// cout << "The heap1 tree postorder is: " << endl;
// postorder(heap1, "");
cout << "The size of heap1 tree is " << heap1->size() << " nodes. " << endl;
//int dataIndex=5;
//int dataValue = data[dataIndex];
int dataValue = 0;
cout << "\n\n\n" << endl;
cout << "the inserted value is " << dataValue << endl;
I found the problem. I should use return 0 instead of exit 0 since the function return type is int. If just use return with a int value then it report syntax error. Fixed.