•24
#include "stdio.h"
#include <math.h>
struct BTNode
{BTNode *left;
BTNode *right;
int value;
};
void print_data( struct BTNode *root )
{if ( root->left != NULL ) {
print_data( root->left );
}
printf( "%d\n", root->value );
if ( root->right != NULL ) {
print_data( root->right );
}
}
struct BTNode *new_node(int x, struct BTNode *y, struct BTNode *z)
{struct BTNode *w = new BTNode();
w->value = x;
w->left = y;
w->right = z;
return w;
}
struct BTNode *insert_node(struct BTNode *node, int x)
{if ( node == NULL ) {
return new_node(x, NULL, NULL);
}
else if ( x < node->value ) {
node->left = insert_node(node->left, x);
return node;
}
else if ( x > node->value ) {
node->right = insert_node(node->right, x);
return node;
}
else {
return node;
}
}
int _tmain()
{int ch;
BTNode *root;
root = new_node( 35, NULL, NULL );
insert_node( root, 46 );
insert_node( root, 21 );
insert_node( root, 13 );
insert_node( root, 61 );
insert_node( root, 40 );
insert_node( root, 69 );
print_data( root );
printf( "Enter キーを1,2回押してください. プログラムを終了します\n");
ch = getchar();
ch = getchar();
return 0;
}
最初は,節点を1個含む二分探索木を作り,
その後,残りの要素を挿入する