(修正)C言語 二分探索木

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

(修正)C言語 二分探索木

#1

投稿記事 by tyo-s- » 12年前

コード:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node_type{
char *data;
struct node_type *left;
struct node_type *right;
};
struct node_type *root;
char buff[128];




void insert(struct node_type *pt,char *word){
char *data;

if(pt == NULL){
pt = (struct node_type *)malloc(sizeof(struct node_type));
pt->data = (char *)malloc(sizeof(char)*(strlen(word)+1));
pt->left = NULL;
pt->right = NULL;
strcpy(pt->data,word);
root = pt->data;
printf("%s\n",pt->data);
printf("根を作る%s\n",root);
return;
}

//printf("%s\n",pt);
data = pt;

printf("root = %s\n",data);
printf("pt->left== %d\n",pt->left);
printf("pt->right== %d\n",pt->right);
if(strcmp(data,word) == 0){
printf("同じ文字\n");
return;
}

if(strcmp(data,word) < 0){
if(pt->right != NULL){
printf("右の子へ\n");
insert(pt->right,word);
return;
}
else{
pt = (struct node_type *)malloc(sizeof(struct node_type));
pt->data = (char *)malloc(sizeof(char)*(strlen(word)+1));
strcpy(pt->data,word);
pt->right = pt->data;
printf("右の子作成%s\n",pt->right);
return;
} 
}


if(strcmp(data,word) > 0){
if(pt->left != NULL){
printf("左の子へ\n");
insert(pt->left,word);
return;
}

else {

pt = (struct node_type *)malloc(sizeof(struct node_type));
pt->data = (char *)malloc(sizeof(char)*(strlen(word)+1));
strcpy(pt->data,word);
pt->left = pt->data;
printf("左の子作成%s\n",pt->left);
return;

}
}
return;
}


int count(struct node_type *pt){

if(pt==NULL)
return 0;


else
return count(pt->left)+count(pt->right)+1;

}




int main(){
int nodes;

root = NULL;

while(scanf("%s",buff) != EOF){
insert(root,buff);
}
nodes = count(root);
printf("%d\n",nodes);

exit(0);
}


入力単語:this is my pen your pen is over there

実行結果:
this
根を作るthis
root = this
pt->left== (null)
pt->right== (null)
左の子作成is
root = this
pt->left== (null)
pt->right== is
左の子作成my
root = this
pt->left== (null)
pt->right== is
左の子作成pen
root = this
pt->left== (null)
pt->right== is
右の子へ
root = is
pt->left== (null)
pt->right== my
右の子へ
root = my
pt->left== (null)
pt->right== pen
右の子へ
root = pen
pt->left== (null)
pt->right== (null)
右の子作成your
root = this
pt->left== (null)
pt->right== is
左の子作成pen
root = this
pt->left== (null)
pt->right== is
左の子作成is
root = this
pt->left== (null)
pt->right== is
左の子作成over
root = this
pt->left== (null)
pt->right== is
左の子作成there
9



といったように、根をthisとして次のisが左の子として作成しているのにpt->rightに入ってます。
どうしてこうなるんでしょうか?
また型についてもどうすれば良いか教えて頂きたいです。

アバター
softya(ソフト屋)
副管理人
記事: 11677
登録日時: 14年前
住所: 東海地方
連絡を取る:

Re: (修正)C言語 二分探索木

#2

投稿記事 by softya(ソフト屋) » 12年前

時間がなくてコードを追いかけれませんが、次々にトピックを立ち上げるのは回答者が困るのでフォーラムルール違反となりますので1つのトピックをご活用下さい。
http://dixq.net/board/board.html
今回は前のは消しておきました。
by softya(ソフト屋) 方針:私は仕組み・考え方を理解して欲しいので直接的なコードを回答することはまれですので、すぐコードがほしい方はその旨をご明記下さい。私以外の方と交代したいと思います(代わりの方がいる保証は出来かねます)。

non
記事: 1097
登録日時: 14年前

Re: (修正)C言語 二分探索木

#3

投稿記事 by non » 12年前

25行 root = pt->data;
左右の型が違います。
32行 data = pt;
ここも
non

アバター
みけCAT
記事: 6734
登録日時: 14年前
住所: 千葉県
連絡を取る:

Re: (修正)C言語 二分探索木

#4

投稿記事 by みけCAT » 12年前

コードをインデントしていただくと、見やすくなります。

コード:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

struct node_type{
	char *data;
	struct node_type *left;
	struct node_type *right;
};
struct node_type *root;
char buff[128];




void insert(struct node_type *pt,char *word){
	char *data;

	if(pt == NULL){
		pt = (struct node_type *)malloc(sizeof(struct node_type));
		pt->data = (char *)malloc(sizeof(char)*(strlen(word)+1));
		pt->left = NULL;
		pt->right = NULL;
		strcpy(pt->data,word);
		root = pt->data;
		printf("%s\n",pt->data);
		printf("根を作る%s\n",root);
		return;
	}

	//printf("%s\n",pt);
	data = pt;

	printf("root = %s\n",data);
	printf("pt->left== %d\n",pt->left);
	printf("pt->right== %d\n",pt->right);
	if(strcmp(data,word) == 0){
		printf("同じ文字\n");
		return;
	}

	if(strcmp(data,word) < 0){
		if(pt->right != NULL){
			printf("右の子へ\n");
			insert(pt->right,word);
			return;
		}
		else{
			pt = (struct node_type *)malloc(sizeof(struct node_type));
			pt->data = (char *)malloc(sizeof(char)*(strlen(word)+1));
			strcpy(pt->data,word);
			pt->right = pt->data;
			printf("右の子作成%s\n",pt->right);
			return;
		} 
	}


	if(strcmp(data,word) > 0){
		if(pt->left != NULL){
			printf("左の子へ\n");
			insert(pt->left,word);
			return;
		}

		else {

			pt = (struct node_type *)malloc(sizeof(struct node_type));
			pt->data = (char *)malloc(sizeof(char)*(strlen(word)+1));
			strcpy(pt->data,word);
			pt->left = pt->data;
			printf("左の子作成%s\n",pt->left);
			return;

		}
	}
	return;
}


int count(struct node_type *pt){

	if(pt==NULL)
		return 0;


	else
		return count(pt->left)+count(pt->right)+1;

}




int main(){
	int nodes;

	root = NULL;

	while(scanf("%s",buff) != EOF){
		insert(root,buff);
	}
	nodes = count(root);
	printf("%d\n",nodes);

	exit(0);
}
複雑な問題?マシンの性能を上げてOpenMPで殴ればいい!(死亡フラグ)

tyo-s-

Re: (修正)C言語 二分探索木

#5

投稿記事 by tyo-s- » 12年前

softya(ソフト屋)様、ご迷惑をおかけしました。次質問する時は気をつけます。
non様、やはり型の違いのせいで結果がおかしかったです。解決できました。ありがとうございました。
みけCAT様、すいませんでした。次質問する時は気をつけます。

閉鎖

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