请教这段二叉树建立的代码有什么有关问题吗
请问这段二叉树建立的代码有什么问题吗
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
typedef struct BiNode
{
int data;
BiNode *Lchild;
BiNode *Rchlid;
}BiNode;
BiNode *InIt_BT()
{
BiNode *bt;
bt = (BiNode *)malloc(sizeof(BiNode));
if (!bt)
{
exit(0);
}
else
{
return bt;
}
}
void Creat(BiNode *bt)
{
int DT;
scanf("%d",&DT);
if(DT == '#')
{
bt = NULL;
}
else
{
bt = InIt_BT();
bt->data = DT;
Creat(bt->Lchild);
Creat(bt->Rchlid);
}
}
void PreOrder(BiNode *bt)
{
if (!bt)
{
return ;
}
else
{
cout<<bt->data;
PreOrder(bt->Lchild);
PreOrder(bt->Rchlid);
}
}
int main()
{
BiNode *bt;
bt = InIt_BT();
Creat(bt);
PreOrder(bt);
return 0;
}
------解决思路----------------------
------解决思路----------------------
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
typedef struct BiNode
{
int data;
BiNode *Lchild;
BiNode *Rchlid;
}BiNode;
BiNode *InIt_BT()
{
BiNode *bt;
bt = (BiNode *)malloc(sizeof(BiNode));
if (!bt)
{
exit(0);
}
else
{
return bt;
}
}
void Creat(BiNode *bt)
{
int DT;
scanf("%d",&DT);
if(DT == '#')
{
bt = NULL;
}
else
{
bt = InIt_BT();
bt->data = DT;
Creat(bt->Lchild);
Creat(bt->Rchlid);
}
}
void PreOrder(BiNode *bt)
{
if (!bt)
{
return ;
}
else
{
cout<<bt->data;
PreOrder(bt->Lchild);
PreOrder(bt->Rchlid);
}
}
int main()
{
BiNode *bt;
bt = InIt_BT();
Creat(bt);
PreOrder(bt);
return 0;
}
------解决思路----------------------
#include<iostream>
#include<stdlib.h>
#include<malloc.h>
using namespace std;
typedef struct BiNode {
int data;
BiNode *Lchild;
BiNode *Rchlid;
}BiNode;
BiNode * Creat() {
int dt;
BiNode *bt;
bt = (BiNode *)malloc(sizeof(BiNode));
if (!bt) {
exit(0);
}
cin >> dt;
if(dt == 0) {
bt = NULL;
} else {
bt->data = dt;
bt->Lchild = Creat();
bt->Rchlid = Creat();
}
return bt;
}
void PreOrder(BiNode *bt) {
if (!bt)
return;
cout << bt->data << ' ';
PreOrder(bt->Lchild);
PreOrder(bt->Rchlid);
}
int main() {
BiNode *bt = Creat();
PreOrder(bt);
return 0;
}
------解决思路----------------------
BiNode * Creat() {
int dt;
BiNode *bt;
bt = new(BiNode);
if (!bt) {
exit(0);
}
cin >> dt;
if(dt == 0) {
bt = NULL;
} else {
bt->data = dt;
bt->Lchild = Creat();
bt->Rchlid = Creat();
}
return bt;
}
void PreOrder(BiNode *bt) {
if (!bt)
return;
cout << bt->data << ' ';
PreOrder(bt->Lchild);
PreOrder(bt->Rchlid);
delete bt;
}