iT邦幫忙

0

【資料結構】堆積樹(陣列法) 未完成

說明

晚點補

程式碼

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX 10
int index = 1,tree_arr[MAX] = {0},top = 1,stack[MAX] = {0};
int main() {
  basic();
  print();
}

函式

void Inorder_stack(int index):以中序輸出陣列樹,透過遞迴秀出

晚點補

void Inorder_stack(int index) {
  if (tree_arr[index] != 0) {
    if (index * 2 <= MAX) {
      Inorder_stack(2 * index);
    }
    if (tree_arr[index] != 0) {
      stack[++top] = tree_arr[index];
    }
    if (index * 2 + 1 <= MAX) {
      Inorder_stack(2 * index + 1);
    }
  }
}

int creat_arr(int num) :晚點補

int creat_arr(int num) {
  if (check_re(num) == true) {
    printf("重複");
    return 0;
  }
  if (index > MAX) {
    printf("堆疊已滿");
    return 0;
  }
  int new_index = index;
  tree_arr[index] = num;
  printf("%d=>%d\n", index, tree_arr[index]);
  if (index % 2 == 1 && tree_arr[index] > tree_arr[index - 1] &&
      index / 2 > 1) {
    printf("%d:%d vs %d:%d\n", index,tree_arr[index],index-1, tree_arr[index - 1]);
    int n = tree_arr[index];
    tree_arr[index] = tree_arr[index - 1];
    tree_arr[index - 1] = n;
  }
  while (tree_arr[index] > tree_arr[index / 2] && index != 1) {
    // printf("%d vs %d", tree_arr[index], tree_arr[index / 2]);
    int n = tree_arr[index];
    tree_arr[index] = tree_arr[index / 2];
    tree_arr[index / 2] = n;
    index /= 2;
  }
  index = new_index;
  index += 1;
  return 1;
}

結果

晚點補


尚未有邦友留言

立即登入留言