读书人

请求指导二分木,该怎么解决

发布时间: 2012-03-15 11:50:38 作者: rapoo

请求指导二分木
给出了条件添加完成程序 求100个节点,二分木高和木形。
自己组合后不知道错在哪里,请帮忙组运行。

C/C++ code
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define  NODE_NUM   100/添加定义和必要函数/int main(void){    struct BST_Node *T_root;    int i;    T_root=(struct BST_Node*)malloc(sizeof(struct BST_Node *));    T_root->left=T_root->right=NULL; T_root->value=0;    srand(RAND_SEED);    for(i=0;i<NODE_NUM; i++){                     insert_v(T_root,rand()/(double)RAND_MAX*NODE_NUM*10);    }    /添加求木的高和木形函数/}

条件 定义函数
struct BST_Node{
int value;
struct BST_Node *left;
struct BST_Node *right;
};
求高的函数
int compute_height(struct BST_Node *p){
int lh=0, rh=0;
if(p==NULL){ return 0; }
lh=compute_height(p->left);
rh=compute_height(p->right);
return Max(lh, rh)+1;
木形表示的函数
void tree_shape(struct BST_Node *p){
if(p==NULL){
return;
}
putchar('(');
if(p->left!=NULL){ tree_shape(p->left); }
printf("%d",p->value);
if(p->right!=NULL){ tree_shape(p->right); }
putchar(')');
}
节点的定义

void insert_v(struct BST_Node *p, int x){
struct BST_Node *nnode;
nnode=(struct BST_Node *)malloc(sizeof(struct BST_Node));
nnode->left=nnode->right=NULL;
nnode->value=x;
insert_n(p,nnode);
}
节点追加
void insert_n(struct BST_Node *p, struct BST_Node *x){
if(x==NULL){ return; }
if(x->value < p->value){
if(p->left==NULL){ p->left=x; }
else { insert_n(p->left, x); }
} else if( p->value < x->value){
if(p->right==NULL){ p->right=x; }
else {insert_n(p->right,x); }
}
}


[解决办法]
C/C++ code
#include<stdio.h>#include<stdlib.h>#include<malloc.h>#define NODE_NUM 100000#define RAND_SEED 0x1031000void insert_v(struct BST_Node*p,double x);void insert_n(struct BST_Node*p,struct BST_Node*x);int compute_height(struct BST_Node*p);void tree_shape(struct BST_Node*p);int Max(int lh,int rh);struct BST_Node{    int value;    struct BST_Node*left;    struct BST_Node*right;};int main(void){    struct BST_Node*T_root;        int i;        T_root=(struct BST_Node*)malloc(sizeof(struct BST_Node));        T_root->left=T_root->right=NULL;        T_root->value=0;        srand(RAND_SEED);        for(i=0;i<NODE_NUM;i++){            insert_v(T_root,rand()/(double)RAND_MAX*NODE_NUM*10);        }        printf("木高。\n",compute_height(T_root));        tree_shape(T_root);        free(T_root);        return(0);}void insert_n(struct BST_Node*p,struct BST_Node*x){    if(x==NULL){        return;}    if(x->value<p->value){        if(p->left==NULL){p->left=x;}        else{insert_n(p->left,x);}}    else if(p->value<x->value){        if(p->right==NULL){p->right=x;}        else{insert_n(p->right,x);}    }}void insert_v(struct BST_Node*p,double x){    struct BST_Node*nnode;    nnode=(struct BST_Node*)malloc(sizeof(struct BST_Node));    nnode->left=nnode->right=NULL;        nnode->value=x;        insert_n(p,nnode);}int compute_height(struct BST_Node*p){    int lh=0,rh=0;    if (p==NULL){return(0);}    lh=compute_height(p->left);    rh=compute_height(p->right);    return Max(lh,rh)+1;}void tree_shape(struct BST_Node*p){    if(p==NULL){        return;    }    putchar('(');    if(p->left!=NULL){tree_shape(p->left);}    printf("%d",p->value);    if(p->right!=NULL){tree_shape(p->right);}    putchar(')');}int Max(int lh,int rh){    int max=lh;    if(rh>max){max=rh;}    return(max);} 

读书人网 >C语言

热点推荐