#include <stdio.h>
//#include <malloc.h>
#include <stdlib.h>
//#define MAXNUM 10;//error C2143: syntax error : missing ']' before ';//宏定义后不应有该分号;
#define MAXNUM 10 //左、右节点的最大数目
/* // error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 前向引用
typedef struct{
TNode* left;
TNode* right;
int value;
}TNode;
*/
struct TNode{
TNode* left;
TNode* right;
int value;
};
TNode* root=NULL;
void append(int N);
void PrintBiTreeMax(TNode*L);
int main( ){
append(63);
append(45);
append(32);
append(77);
append(96);
append(21);
append(17);
PrintBiTreeMax(root);
return 0;
}
void append(int N){
TNode * NewNode=(TNode*)malloc(sizeof(TNode));
NewNode->value=N;
NewNode->left=NULL;// 要初始化啊
NewNode->right=NULL;
if (root==NULL)
{
root=NewNode;
return ;
}
else
{
TNode* temp;
temp=root;
while (N >= temp->value &&temp->left!=NULL ||N<temp->value &&temp->right!=NULL)
{
while (N >= temp->value &&temp->left!=NULL){
temp=temp->left;
}
while(N<temp->value &&temp->right!=NULL){
temp=temp->right;
}
}
if (N>=temp->value)
{
temp->left=NewNode;
}
else
{
temp->right=NewNode;
}
return ;
}
}
void PrintBiTreeMax(TNode * L){
int left1[MAXNUM];
int right1[MAXNUM];
TNode* temp;
int i;
int lenLeft=0;
int lenRight=0;
//for(int i=0,temp=L->left;temp!=NULL;temp=temp->left){ //error C2227: left of '->value' must point to class/struct/union/generic type ,
// type is 'int' 重新定义的int i=0,temp=L->left 将使得temp为int 型
for( i=0,temp=L->left;temp!=NULL;temp=temp->left){//
left1[i++]=temp->value;
lenLeft++;
}
for (i=0,temp=L->right;temp!=NULL;temp=temp->right)
{
right1[i++]=temp->value;
lenRight++;
}
for (int i=lenLeft-1;i>=0;i--)
{
printf("%6d",left1[i]);
}
printf("%6d",root->value);
for (int i=0;i<lenRight;i++)
{
printf("%6d",right1[i]);
}
}
debug 总结:
#define后不能加分号
//#define MAXNUM 10;//error C2143: syntax error : missing ']' before ';//宏定义后不应有该分号;
struct 变量分配完内存空间后,要记得初始化
TNode * NewNode=(TNode*)malloc(sizeof(TNode));
NewNode->value=N;
NewNode->left=NULL;// 要初始化啊
NewNode->right=NULL;
否则下述比较总为TRUE
temp=root;
while (N >= temp->value &&temp->left!=NULL ||N<temp->value &&temp->right!=NULL)
{
for( int i,temp) 意味着temp重新被定义为int型,将覆盖原始的temp 引用
TNode* temp;
int i;
int lenLeft=0;
int lenRight=0;
//for(int i=0,temp=L->left;temp!=NULL;temp=temp->left){ //error C2227: left of '->value' must point to class/struct/union/generic type ,
// type is 'int' 重新定义的int i=0,temp=L->left 将使得temp为int 型
for( i=0,temp=L->left;temp!=NULL;temp=temp->left){//
输出结果
96 77 63 45 32 21 17
63(root)
77 45
96 32
21
17