C言語の問題を教えてください。答えがなく困っているのでコメントも付けていただけると助かります。
実行結果1のようになるようにしたいのですが実行結果2のようになってしまいます。
<問題>
実行結果例1になるように、下のプログラム中のprinttreeとmax_vを完成させなさい。
<実行結果1>
INSERT: 32 16 38 7 25 59 42 95 4 18 81
4
7
16
18
25
32
38
42
59
81
95
insert 23
4
7
16
18
23
25
32
38
42
59
81
95
The element 23 is found at 0x17d2170.
max_v is 95.
<実行結果2>
INSERT: 32 16 38 7 25 59 42 95 4 18 81
insert 23
The element 23 is found at 0x209d170.
<実行結果2のプログラム>
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
int data;
struct node *leftson;
struct node *rightson;
} Node;
Node *search (int x);
void new (Node **w);
void insert (int x);
void delete (int x);
void nfree (Node *n);
void printtree(Node *n, int h);
int max_v(Node *n);
Node *v, *root;
Node *search(int x){
v = root;
while(v != NULL){
if (v->data == x) {
return(v);
}
if (v->data > x) {
v = v->leftson;
}
else {
v = v->rightson;
}
}
printf ("The element is not in S.\n");
return (NULL); }
void new (Node **w) {
*w = (Node *)malloc(sizeof(Node));
(*w)->leftson = NULL;
(*w)->rightson = NULL;
}
void insert(int x){
Node *p = NULL;
Node *w;
v = root;
while(v != NULL) {
p = v;
if (v->data > x){
v = v->leftson;
} else {
v = v->rightson;
}
}
new(&w);
w->data = x;
if (p->data > x){
p->leftson = w;
}
else {
p->rightson = w;
}
}
void nfree(Node *n){
if(n->leftson != NULL) {
nfree(n->leftson);
}
if(n->rightson != NULL){
nfree(n->rightson);
}
free(n);
}
int main(void){
int i;
new(&root);
root->data = S[0];
printf("INSERT:%3d", S[0]);
for (i=1; i<11; i++){
insert(S[i]);
printf("%3d", S[i]);
}
printf("\n");
insert(23); printf("insert 23\n");
printf("The element 23 is found at %p.\n",search(23));
nfree(root);
return 0;
}