全心思齐网

二叉树中任意结点位置?

完全二叉树可以用数组存储int findPosition(int array[], int len, int data){int pos, tmp, high = 1;for(pos = 0; pos < len; pos ++)if (array[pos] == data)break;if (pos == len) //无此结点return -1;tmp = pos + 1; //有此结点while ((tmp = tmp >> 1) != 0)high = high << 1;pos = pos - high + 1;return pos;}

匿名回答于2023-09-16 14:08:28


相关知识问答