#include
#include
struct tree
{
struct tree *lchild;
int data;
struct tree *rchild;
};
typedef struct tree treenode;
typedef treenode *bintree;
bintree insert_node(bintree root,int node)
{
bintree newnode;
bintree currentnode;
bintree parentnode;
newnode=(bintree)malloc(sizeof(treenode));
newnode->data=node;
newnode->rchild=NULL;
newnode->lchild=NULL;
if(root==NULL)
return newnode;
else
{
currentnode=root;
while(currentnode!=NULL)
{
parentnode=currentnode;
if(currentnode->data>node)
currentnode=currentnode->lchild;
else
currentnode=currentnode->rchild;
}
if(parentnode->data>node)
parentnode->lchild=newnode;
else
parentnode->rchild=newnode;
}
return root;
}
bintree create_tree(int data[],int len)
{
bintree root;
int i;
root=NULL;
for(i=0;i root=insert_node(root,data[i]);
return root;
}
void preorder(bintree point)
{
if(point!=NULL)
{
printf("%-3d",point->data);
preorder(point->lchild);
preorder(point->rchild);
}
}
void inorder(bintree point)
{
if(point!=NULL)
{
inorder(point->lchild);
printf("%-3d",point->data);
inorder(point->rchild);
}
}
void postorder(bintree point)
{
if(point!=NULL)
{
postorder(point->lchild);
postorder(point->rchild);
printf("%-3d",point->data);
}
}
void main()
{
bintree root=NULL;
int i,index;
int value;
int x;
int nodelist[20];
clrscr();
printf("please input the elements of binary tree(Exit for 0):\n\n");
index=0;
scanf("%d",&value);
printf("qing xuan ze ni xiang yong de bian li fa :\n");
printf("0 dai biao xianxu bianli ,\n");
printf("1 dai biao zhong xu bian li \n" );
printf("2 dai biao houbian li:\n ");
printf("\n");
while(value!=0)
{
nodelist[index]=value;
index++;
scanf("%d",&value);
}
scanf("%d",&x);
switch (x)
{
case 0:
root=create_tree(nodelist,index);
printf("the inorder reaversal result is:[");
inorder(root);
printf("]\n\n");
printf("\n\n");
printf("\n");
printf("\n");
getch();
case 1:
root=create_tree(nodelist,index);
printf("the preorder reaversal result is:[");
preorder(root);
printf("]\n\n");
printf("\n\n");
printf("\n");
printf("\n");
getch();
case 2:
root=create_tree(nodelist,index);
printf("the postorder reaversal result is:[");
postorder(root);
printf("]\n\n");
printf("\n\n");
printf("\n");
printf("\n");
getch();
} ;
getch();
}
用来遍历小树的,可是为什么能在WIN_TC中运行,却不能在C++中运行呢,老是说不能打开一个叫
error C2065: 'clrscr' : undeclared identifier
这个错误,这是为什么?
是我代码写错了吗?
--------------------next---------------------
阅读(1054) | 评论(0) | 转发(0) |