我建立二叉树有什么问题啊
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
//用了一个小时,头脑里面基本上有了思路
//弄明白了队列这种数据结构
using namespace std;
typedef struct QElementType
{
char parent;
char side;
char myself;
}QElementType;
typedef struct QNode{
QElementType data;
struct QNode *next;
//struct QNode *lchild;
//struct QNode *rchild;
}QNOde,*QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
}LinkQueue;
struct BinTree
{
char mydata;
struct BinTree *lchild;
struct BinTree *rchild;
};
//先序遍历二叉树
void PreTree(BinTree* &B,QElementType e)
{
BinTree *t = (BinTree *)malloc(sizeof(BinTree));
t->mydata = e.myself;
if(B == NULL)
return;
if(B->mydata == e.parent)
{
if(e.side == 'L')
{
B->lchild = t;
}
if(e.side == 'R')
{
B->rchild = t;
}
}
PreTree(B ->lchild,e);
PreTree(B ->rchild,e);
}
void PreTree1(BinTree *B)
{
if(B == NULL)
return;
printf("%c",B->mydata);
PreTree1(B->lchild);
PreTree1(B->rchild);
}
void InitQueue(LinkQueue &Q)
{
Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));
if(!Q.front) exit(0);
Q.front-> next = NULL;
return;
}
void EnQueue(LinkQueue &Q,QElementType e)
{
QueuePtr p = (QueuePtr)malloc(sizeof(QNode));
if(!p) exit(0);
p->data = e;
p->next = NULL;
Q.rear -> next = p;
Q.rear = p;
}
QElementType GetHead(LinkQueue &Q)
{
QElementType e;
e = Q.front->next->data;
return e;
}
QElementType DeQueue(LinkQueue &Q)
{
QueuePtr p =NULL;
if(Q.front == Q.rear)
exit(0);
p = (QueuePtr)malloc(sizeof(QNode));
p = Q.front -> next;
QElementType e;
e = p -> data;
Q.front->next = p->next;
if(Q.rear == p) Q.rear = Q.front;
free(p);
return e;
}
int main()
{
LinkQueue Q;
InitQueue(Q);
BinTree *B = NULL;
char charray[10];
char ch;
do
{
cout << "Please input three character to discribe children-node:\n";
scanf("%s",charray);
QElementType e;
e.myself = charray[0];
e.parent = charray[1];
e.side = charray[2];
EnQueue(Q,e);
if(e.parent == '#')
B->mydata = e.myself;
else{
cout <<"cdskcvsd";
PreTree(B,e);
}
cout << "Do you want to continue your input?\n";
scanf("%c",&ch);
}while(ch == 'Y');
cout << "woshitiancxai";
PreTree1(B);
return 0;
}
do
{
cout << "Please input three character to discribe children-node:\n";
scanf("%s",charray);
QElementType e;
e.myself = charray[0];
e.parent = charray[1];
e.side = charray[2];
EnQueue(Q,e);
if(e.parent == '#')
B->mydata = e.myself;
else{
cout <<"cdskcvsd";
PreTree(B,e);
}
cout << "Do you want to continue your input?\n";
scanf("%c",&ch);
}while(ch == 'Y');