¹ØÓڵݹ麯Êý¶þ²æÊ÷µÄÎÊÌâÇó´óÀнøÀ´¿´¿´
±¾Ìû×îºóÓÉ ¿Ç970527 ÓÚ 2022-12-13 14:06 ±à¼#include <stdio.h>
#include <stdlib.h>
typedef struct _nd{
int num;
struct _nd *left;
struct_nd *right;
} NODE;
void mbtree(NODE *p1, int num);
int main(void)
{
int data[] = {3, 6, 9, 1, 10, 7};
int i;
NODE *root, *top;
int dsize;
dsize = sizeof(data)/sizeof(int);
for (i = 0; i < dsize; i++) {
printf("%d ", data);
}
printf("\n");
root = (NODE *) malloc(sizeof(NODE));
if(root == NULL) {
printf("can not allocate memory\n");
exit(-1);
}
root->num = data;
root->left = NULL;
root->right = NULL;
top = root;
printf("root: %d\n", root->num);
for(i = 1; i < dsize; i++) {
printf("i= %d :", i);
mbtree(top, data);
}
return 0;
}
/* δÍê³É¥×¥í¥°¥é¥à */
void mbtree(NODE *p1, int num)
{
NODE *p2;
p2 = (NODE *)malloc(sizeof(NODE));
if(p2 == NULL) {
printf("can not allocate memory for mbree\n");
exit(-1);
}
p2->num = /*¤³¤³¤òÍê³É¤µ¤»¤è */;
p2->left = NULL;
p2->right = /*¤³¤³¤òÍê³É¤µ¤»¤è */;
/* ‚Ž¤Î´óС¤Ë¤è¤Ã¤Æ×óÓÒ¤ËÕñ¤ê·Ö¤±¤ë */
if (p1->num < num) { /* Ö÷¥Î©`¥É¤è¤ê´ó¤¤¤¤È¤ */
/* ÓÒ¤¬NULL¤Ê¤é¤½¤³¤ËФ¿¤Ê¥Î©`¥É¤ò¤Ö¤éϤ²¤ë */
if (p1->right == NULL) {
p1->right = /*¤³¤³¤òÍê³É¤µ¤»¤è */;
printf(/*¤³¤³¤òÍê³É¤µ¤»¤è */);
}
else /* NULL¤Ç¤Ê¤±¤ì¤ÐÓ҂ȤΥΩ`¥É¤ËÒÆ„Ó */
mbtree(/*¤³¤³¤òÍê³É¤µ¤»¤è */);
} else {
/* ×ó¤¬NULL¤Ê¤é¤½¤³¤ËФ¿¤Ê¥Î©`¥É¤ò¤Ö¤éϤ²¤ë */
/*¤³¤³¤òÍê³É¤µ¤»¤è */
}
}
3 6 9 1 10 7
root: 3
i= 1 :(p)3 (r)6
i= 2 :(p)6 (r)9
i= 3 :(p)3 (l)1
i= 4 :(p)9 (r)10
i= 5 :(p)9 (l)7
ÕâÊÇÕâµÀÌâ×îºóÊä³öµÄÄÚÈÝ£¬ÎҸ㲻¶®p2 = (NODE *)malloc(sizeof(NODE))Õâ¸ö¾¿¾¹ÊÇʲôÒâ˼£¬»¹ÓоÍÊÇÓ¦¸ÃÈçºÎ°ÑÕâ¸öº¯ÊýÌîÍê
rµÄÒâ˼ÊÇÓұߣ¬lµÄÒâ˼ÊÇ×ó±ß void mbtree(NODE *p1, int num)
{
NODE *p2;
p2 = (NODE *)malloc(sizeof(NODE));
if(p2 == NULL) {
printf("can not allocate memory for mbree\n");
exit(-1);
}
p2->num = num;
p2->left = NULL;
p2->right = NULL;
if (p1->num < num) {
if (p1->right == NULL) {
p1->right = num;
printf("(p)%d (r)%d\n",p1->num,num);
}
else
mbtree(p1->right,num);
}
else {
if (p1->right == NULL) {
p1->left = num;
printf("(p)%d (l)%d\n",p1->num,num);
}
else
mbtree(p1->left,num);
}
}
ÕâÊÇÎÒд³öÀ´µÄÕâ¸öº¯Êý£¬µ«ÊDz»ÖªµÀΪʲôֻÄÜËãµ½
3 6 9 1 10 7
root: 3
i= 1 :(p)3 (r)6
È»ºó¾Í³ö´íÁË£¬Çë´óÀаïæ¿´¿´ÄÄÀï³öÎÊÌâÁË ÕâµÀÌâµÄ˼·ÊÇ£¬ÏÈ´Ó3¿ªÊ¼²éÕÒÏÂÒ»¸öÊý£¬
3Ö®ºóÊÇ6£¬6±È3´ó£¬ÄÇô·ÅÔÚ3µÄÓұߣ¬È»ºóÏÂÒ»¸öÊý×ÖÊÇ9£¬9±È3´ó£¬µ«ÊÇ3µÄÓÒ±ßÓÐÊýÁË£¬ÐèÒªµ½6ÕâÒ»²ã£¬¾ÍµÝ¹é£¬È»ºó6ÕâÒ»²ã±È6´ó£¬·Åµ½6µÄÓұߣ¬
ÔÙµ½1£¬1±È3С£¬3µÄ×ó±ßûÓУ¬·ÅÔÚ3µÄ×ó±ß£¬
ÔÙµ½10£¬10±È3´ó£¬¿ÉÊÇ3µÄÓÒ±ßÓÐ6£¬µ½6ÕâÒ»²ã£¬±È6´ó£¬6µÄÓÒ±ßÓÐ9£¬ËùÒÔÔÙµ½9µÄÕâÒ»²ã£¬±È9´ó£¬9µÄÓÒ±ßÊÇnull·Å9µÄÓұߣ¬ÊÇÕâÑùµÄ˼· ¿Ç970527 ·¢±íÓÚ 2022-12-13 15:11
ÕâµÀÌâµÄ˼·ÊÇ£¬ÏÈ´Ó3¿ªÊ¼²éÕÒÏÂÒ»¸öÊý£¬
3Ö®ºóÊÇ6£¬6±È3´ó£¬ÄÇô·ÅÔÚ3µÄÓұߣ¬È»ºóÏÂÒ»¸öÊý×ÖÊÇ9£¬9±È3 ...
p1->right = num;////±àÒëÄÜͨ¹ýÂ𣿠±¾Ìû×îºóÓÉ jhq999 ÓÚ 2022-12-13 18:14 ±à¼
#include <stdio.h>
#include <stdlib.h>
typedef struct _nd
{
int num;
struct _nd *left;
struct_nd *right;
} NODE;
void mbtree(NODE *p1, int num);
int main(void)
{
int data[] = {3, 6, 9, 1, 10, 7};
int i;
NODE *root, *top;
int dsize;
dsize = sizeof(data)/sizeof(int);
for (i = 0; i < dsize; i++)
{
printf("%d ", data);
}
printf("\n");
root = (NODE *) malloc(sizeof(NODE));
if(root == NULL)
{
printf("can not allocate memory\n");
exit(-1);
}
root->num = data;
root->left = NULL;
root->right = NULL;
top = root;
printf("root: %d\n", root->num);
for(i = 1; i < dsize; i++)
{
printf("i= %d :", i);
mbtree(top, data);
}
return 0;
}
/* δÍê³É¥×¥í¥°¥é¥à */
void mbtree(NODE *p1, int num)
{
/* ‚Ž¤Î´óС¤Ë¤è¤Ã¤Æ×óÓÒ¤ËÕñ¤ê·Ö¤±¤ë */
if (p1->num < num) /* Ö÷¥Î©`¥É¤è¤ê´ó¤¤¤¤È¤ */
{
/* ÓÒ¤¬NULL¤Ê¤é¤½¤³¤ËФ¿¤Ê¥Î©`¥É¤ò¤Ö¤éϤ²¤ë */
if (p1->right == NULL)
{
NODE *p2;
p2 = (NODE *)malloc(sizeof(NODE));
if(p2 == NULL)
{
printf("can not allocate memory for mbree\n");
exit(-1);
}
p2->num = num;
p2->left = NULL;
p2->right = NULL;
p1->right = p2;
printf("%d's right is %d\n",p1->num,num);
}
else /* NULL¤Ç¤Ê¤±¤ì¤ÐÓ҂ȤΥΩ`¥É¤ËÒÆ„Ó */
mbtree(p1->right,num);
}
else
{
if (p1->left == NULL)
{
NODE *p2;
p2 = (NODE *)malloc(sizeof(NODE));
if(p2 == NULL)
{
printf("can not allocate memory for mbree\n");
exit(-1);
}
p2->num = num;
p2->left = NULL;
p2->right = NULL;
p1->left = p2;
printf("%d's left is %d\n",p1->num,num);
}
else /* NULL¤Ç¤Ê¤±¤ì¤ÐÓ҂ȤΥΩ`¥É¤ËÒÆ„Ó */
mbtree(p1->left,num);
}
}
3 6 9 1 10 7
root: 3
i= 1 :3's right is 6
i= 2 :6's right is 9
i= 3 :3's left is 1
i= 4 :9's right is 10
i= 5 :9's left is 7
Process returned 0 (0x0) execution time : 0.290 s
Press any key to continue.
jhq999 ·¢±íÓÚ 2022-12-13 18:12
Å£±Æѽ´óÀУ¡£¡ ¿Ç970527 ·¢±íÓÚ 2022-12-14 00:17
Å£±Æѽ´óÀУ¡£¡
±ð׿±£¬Ñ§ÍêÊ÷Äã¾ÍÃ÷°×ÁË
Ò³:
[1]