二分探索木を出力するプログラムについて質問です。

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
shion

二分探索木を出力するプログラムについて質問です。

#1

投稿記事 by shion » 4ヶ月前

コード:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct area {
  char name[20];
  int population;
  struct area *left;
  struct area *right;
};
void printTree(int indent, struct area *s);
struct area *addarea(struct area *ar, char *str, int num);
int main() {
  struct area   a1 = {"A", 250000, NULL, NULL},
                a2 = {"B", 190000, NULL, NULL},
                a3 = {"C", 400000, NULL, NULL},
                a4 = {"D", 170000, NULL, NULL},
                a5 = {"E", 240000, NULL, NULL},
                a6 = {"F", 430000, NULL, NULL};
  struct area *root = NULL;
  root = &a1;
  a1.left = &a2;
  a1.right = &a3;
  a2.left = &a4;
  a2.right = &a5;
  a3.right = &a6;
  char str[20];
  int num;
  printf("input a area name and its population : ");
  scanf("%s %d", str, &num);
  root = addarea(root, str, num);
  printTree(0, root);
  printf("\n");
  return 0;
}
void printTree(int indent, struct area *ar) {
  int i;
  if (ar == NULL)
    printf("NULL");
  else {
    printf("\n");
    for (i = 0; i < indent; i++) printf(" ");
    printf("(%-10s: population = %6d) ", ar->name, ar->population);
    printCitiesTree(indent + 5, ct->left);
    printf(", ");
    printCitiesTree(indent + 5, ct->right);
    printf(") ");
  }
}
struct area *addarea(struct area *ar, char *str, int num) {
  if (ar->population > num) {
    if (ar->left == NULL) {
      ar->left = malloc(sizeof(struct area));
      strcpy(ar->left->name, str);
      ar->left->population = num;
      return ar;
    } else {
      addarea(ar->left, str, num);
    }
  } else {
    if (ar->right == NULL) {
      ar->right = malloc(sizeof(struct area));
      strcpy(ar->right->name, str);
      ar->right->population = num;
      return ar;
    } else {
      addarea(ar->right, str, num);
    }
  }
}
上のプログラムについて質問です。
プログラムを実行すると、
input a area name and its population : G 40000

(D : population = 170000)
(G : population = 40000) NULL, NULL) , NULL)

このように二分木の一部しか出力されません。
どのようにしたら二分木の全体を出力できるでしょうか。

Meta3

Re: 二分探索木を出力するプログラムについて質問です。

#2

投稿記事 by Meta3 » 4ヶ月前

エラーで動かなかったのでコピペ間違いだけなをして a1--- a6 を表示 あとは教科書を見て
VisualStudio2019 c1.c

コード:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct area {
  char name[20];
  int population;
  struct area *left;
  struct area *right;
};
void printTree(int indent, struct area *s);
struct area *addarea(struct area *ar, char *str, int num);
int main() {
  struct area   a1 = {"A", 250000, NULL, NULL},
                a2 = {"B", 190000, NULL, NULL},
                a3 = {"C", 400000, NULL, NULL},
                a4 = {"D", 170000, NULL, NULL},
                a5 = {"E", 240000, NULL, NULL},
                a6 = {"F", 430000, NULL, NULL};
  struct area *root = NULL;
  root = &a1;
  a1.left = &a2;
  a1.right = &a3;
  a2.left = &a4;
  a2.right = &a5;
  a3.right = &a6;
printTree(0, root);

return 0;



  char str[20];
  int num;
  printf("input a area name and its population : ");
  scanf("%s %d", str, &num);
  root = addarea(root, str, num);
  printTree(0, root);
  printf("\n");
  return 0;
}
void printTree(int indent, struct area *ar) {
  int i;
  if (ar == NULL)
    printf("NULL");
  else {
    printf("\n");
    for (i = 0; i < indent; i++) printf(" ");
    printf("(%-10s: population = %6d) ", ar->name, ar->population);
    printTree(indent + 5, ar->left);
    printf(", ");
    printTree(indent + 5, ar->right);
    printf(") ");
  }
}
struct area *addarea(struct area *ar, char *str, int num) {
  if (ar->population > num) {
    if (ar->left == NULL) {
      ar->left = malloc(sizeof(struct area));
      strcpy(ar->left->name, str);
      ar->left->population = num;
      return ar;
    } else {
      addarea(ar->left, str, num);
    }
  } else {
    if (ar->right == NULL) {
      ar->right = malloc(sizeof(struct area));
      strcpy(ar->right->name, str);
      ar->right->population = num;
      return ar;
    } else {
      addarea(ar->right, str, num);
    }
  }
}

コード:

Copyright (C) Microsoft Corporation.  All rights reserved.

/out:c1.exe
c1.obj

c:\b>c1.exe

(A         : population = 250000)
     (B         : population = 190000)
          (D         : population = 170000) NULL, NULL) ,
          (E         : population = 240000) NULL, NULL) ) ,
     (C         : population = 400000) NULL,
          (F         : population = 430000) NULL, NULL) ) )
c:\b>

アバター
あたっしゅ
記事: 451
登録日時: 10年前
住所: 東京23区
連絡を取る:

Re: 二分探索木を出力するプログラムについて質問です。

#3

投稿記事 by あたっしゅ » 4ヶ月前

コード:

//
//
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//
//
//
struct area {
  char name[20];
  int population;
  
  struct area *left;
  struct area *right;
};
//
//
//
void
printTree(int indent, struct area *ar)
{
  int i;

  if (ar == NULL) {
    //printf("NULL");
  } else {
    //printCitiesTree(indent + 5, ct->left);
    printTree(indent + 5, ar->left);

    for (i = 0; i < indent; i++) printf(" ");
    printf("(%-10s: population = %6d) \n", ar->name, ar->population);

    //printCitiesTree(indent + 5, ct->right);
    printTree(indent + 5, ar->right);
  }
}


//struct area*
void
addarea(struct area *ar, char *str, int num)
{
  if (ar->population > num) {
    if (ar->left == NULL) {
      ar->left = malloc(sizeof(struct area));
      strcpy(ar->left->name, str);
      ar->left->population = num;

      //return ar;
      return;
    } else {
      addarea(ar->left, str, num);
    }
  } else {
    if (ar->right == NULL) {
      ar->right = malloc(sizeof(struct area));
      strcpy(ar->right->name, str);
      ar->right->population = num;

      //return ar;
      return;
    } else {
      addarea(ar->right, str, num);
    }
  }
}


int
main() 
{
  struct area   a1 = {"A", 250000, NULL, NULL},
                a2 = {"B", 190000, NULL, NULL},
                a3 = {"C", 400000, NULL, NULL},
                a4 = {"D", 170000, NULL, NULL},
                a5 = {"E", 240000, NULL, NULL},
                a6 = {"F", 430000, NULL, NULL};

  struct area *root = NULL;

  root = &a1;
  a1.left = &a2;
  a1.right = &a3;
  a2.left = &a4;
  a2.right = &a5;
  a3.right = &a6;

  printTree(0, root);
  printf("\n");

  char str[20];
  int num;

  printf("input a area name and its population : ");
  scanf("%s %d", str, &num);

  //root = addarea(root, str, num);
  addarea(root, str, num);
  printTree(0, root);
  printf("\n");

  return 0;
}


// end.

https://www.onlinegdb.com/ で確認。
VTuber:
東上☆海美☆(とうじょう・うみみ)
http://atassyu.rosx.net/vtuber/index.html
レスがついていないものを優先して、レスすみみ。時々、見当外れなレスしみみ。

中の人:
手提鞄あたっしゅ、[MrAtassyu]
http://ameblo.jp/mratassyu/
Pixiv: 666303
手提鞄屋魚有店(てさげかばんやうおありてん)

返信

“C言語何でも質問掲示板” へ戻る