求教用VS2012编译不出来 大部分代码是老师给的 自己补了一些空 说是变量BT未初始化
#include <stdio.h>#include <iostream>
using namespace std;
#include <stdlib.h>
#include "string.h"
#define num 100
typedef char datatype;
typedef struct node
{
datatype data;
struct node *leftchild;
struct node *rightchild;
}BinTNode;
typedef BinTNode *BinTree;
//按照完全二叉树的顺序,构建二叉树
BinTree CreateBinTree(BinTree bt)
{/*Q[1..n]是BinTNode 类型的指针,front 和rear 是队头指针和队尾指针*/
BinTNode *Q[num];
BinTNode *s;
int front,rear;
char ch;
ch=getchar();
bt=NULL;
front=1;
rear=0;
while(ch!='#')
{
s=NULL;
if (ch!='@')
{
s=new BinTNode;
s->data=ch;
s->leftchild=NULL;
s->rightchild=NULL;
}
rear++;
Q[rear]=s;
if (rear==1)
bt=s;
else
{
if(s!=NULL && Q[front]!=NULL)
{
if(rear==2*front)
Q[front]->leftchild=Q[rear];
else
Q[front]->rightchild=Q[rear];
}
}
if(rear==2*front+1)
{
front++;
}
ch=getchar();
}
return bt;
}
//前序遍历二叉树
void preorder(BinTree bt)
{
if(bt)
{
cout<<bt->data;
preorder(bt->leftchild);
preorder(bt->rightchild);
}
}
//中序遍历二叉树
void inorder(BinTree bt)
{
if(bt)
{
inorder(bt->leftchild);
cout<<bt->data;
inorder(bt->rightchild);
}
}
//后序遍历二叉树
void postorder(BinTree bt)
{
if(bt)
{
postorder(bt->leftchild);
postorder(bt->rightchild);
cout<<bt->data;
}
}
//主函数
void main()
{
BinTree bt;
int xz=1;
while(xz)
{
cout<<endl;
cout<<" Binary Tree Traverse "<<endl;
cout<<"========================================"<<endl;
cout<<" 1.Create Binary Tree Structure "<<endl;
cout<<" 2.Pre Order "<<endl;
cout<<" 3.In Order "<<endl;
cout<<" 4.Post Order "<<endl;
cout<<" 0.Exit "<<endl;
cout<<" Please Slect: 0-4: "<<endl;
cin>>xz;
cout<<endl;
switch(xz)
{
case 1:
cout<<"input value for perfect binary tree: "<<endl;
bt=CreateBinTree(bt);
cout<<"the binary tree structure is created "<<endl;
break;
case 2:
cout<<"The pre order is: ";
preorder(bt);
cout<<endl;
break;
case 3:
cout<<"The in order is: ";
inorder(bt);
cout<<endl;
break;
case 4:
cout<<"The post order is: ";
postorder(bt);
cout<<endl;
break;
case 0:
xz=0;
return;
}
}
}