White Session - heap path -c language

Newer and more comprehensive "data structures and algorithms," the update site, more python, go, waiting for you teaching artificial intelligence: https://www.cnblogs.com/nickchen121/p/11407287.html

First, understand the meaning of problems

The insertion of a series of a given number of initially empty stack minor vertex H []. Then for any given index i, from the print path H [i] to the root node.

Sample input:

5 (tree nodes) 3 (number of i)

4623262410-- "node data

5 4 3 - "i value

Through the above examples, we can get the tree structure shown below:

By observing the tree, we can see sample output:

24 23 10

46 23 10

26 10

Second, the operation of the stack and represents

/* c语言实现 */

#define MAXN 1001
#define MINH -10001

int H[MAXN], size;

void Create()
{
  size = 0;
  H[0] = MINH; // 设置岗哨
}

void Insert(int X)
{
  // 将X插入H。这里省略检查堆是否已满的代码
  int i;
  for (i = ++size; H[i/2] > X; i /= 2)
    H[i] = H[i/2];
  H[i] = X;
}

Third, the main program

/* c语言实现 */

int main()
{
  读入n和m;
  根据输入序列建堆;
  对m个要求:打印到根的路劲;
  
  return 0;
}


int main()
{
  int n, m, x, i, j;
  
  scanf("%d %d", &n, &m);
  Create(); // 堆初始化
  for (i = 0; i < n; i++){ 
    // 以逐个插入方式建堆
    scanf("%d", &x);
    Insert(x);
  }
  
  for (i = 0; i < m; i++){
    scanf("%d", &j);
    printf("%d", H[j]);
    while (j > 1) {
      // 沿根方向输出各结点
      j /= 2;
      printf("%d", H[j]);
    }
    printf("\n");
  }
  return 0;
}

Guess you like

Origin www.cnblogs.com/nickchen121/p/11579245.html