69,381
社区成员
发帖
与我相关
我的任务
分享
#include "Stdio.h"
#include "Conio.h"
#include "queue.c"
typedef struct BiTNode{
char data;
struct BiTNode * lchild,* rchild;
} BiTree ;
LinkQueue Q;
int main(void)
{
BiTree *p;
InitQueue(&Q);
CreatBiTree(&p);
Q2Post(&p) ;
printf ("preorder:");
Preorder(p);
printf ("\n");
printf ("inorder:");
inorderout(p);
printf ("\n");
printf ("postorder:");
Postorder(p);
printf ("\n");
getch();
return 0;
}
CreatBiTree(BiTree **T)
{
char ch;
scanf ("%c",&ch);
fflush (stdin);
if (ch==' ')
{
*T=NULL;
}
else
{
*T=(BiTree *)malloc(sizeof(BiTree));
if (!T)
printf ("overflow");
EnQueue (&Q,ch);
CreatBiTree(&(*T)->lchild);
CreatBiTree(&(*T)->rchild);
}
return 0;
}
Q2Post(BiTree **T)
{
if (*T)
{
Q2Post(&(*T)->lchild); /*后序遍历二叉树T的左子树*/
Q2Post(&(*T)->rchild); /*后序遍历二叉树T的右子树*/
DeQueue (&Q,&(*T)->data); /*OUT结点数据*/
}
}
/*li bian function */
Preorder(BiTree *T)
{
if (T)
{
printf ("%c",T->data); /*访问结点数据*/
Preorder(T->lchild); /*先序遍历二叉树T的左子树*/
Preorder(T->rchild); /*先序遍历二叉树T的右子树*/
}
}
inorderout(BiTree *T)
{
if(T)
{
inorderout(T->lchild); /*中序遍历二叉树T的左子树*/
printf ("%c",T->data); /*访问结点数据*/
inorderout(T->rchild); /*中序遍历二叉树T的右子树*/
}
}
Postorder(BiTree *T)
{
if (T)
{
Postorder(T->lchild); /*后序遍历二叉树T的左子树*/
Postorder(T->rchild); /*后序遍历二叉树T的右子树*/
printf ("%c",T->data); /*访问结点数据*/
}
}
#include "Stdio.h"
#include "Conio.h"
#define QElemType char
typedef struct QNode{
QElemType data;
struct QNode * next;
}QNode, *QueuePtr;
typedef struct {
QueuePtr front;
QueuePtr rear;
} LinkQueue;
InitQueue(LinkQueue *Q)
{
(*Q).rear=(QueuePtr)malloc(sizeof(QNode));
(*Q).front=(*Q).rear;
if (!(*Q).front)
{
printf ("overflow");
return -1;
}
(*Q).front->next=NULL;
return 0;
}
DestroyQueue(LinkQueue *Q)
{
while ((*Q).front)
{
(*Q).rear=(*Q).front->next;
free ((*Q).front);
(*Q).front=(*Q).rear;
}
return 0;
}
EnQueue (LinkQueue *Q ,QElemType e)
{
QueuePtr p;
p=(QueuePtr)malloc(sizeof(QNode)) ;
if (!p)
{
printf ("overflow");
return -1;
}
p->data=e;
p->next=NULL;
(*Q).rear->next=p;
(*Q).rear=p;
return 0;
}
DeQueue (LinkQueue *Q ,QElemType *e)
{
QueuePtr p;
if ((*Q).rear==(*Q).front)
return -1;
p=(*Q).front->next;
*e=p->data;
(*Q).front->next=p->next;
if ((*Q).rear==p)
(*Q).rear=(*Q).front;
free (p);
return 0;
}
CreatBiTree(BiTree **T)
{
char ch;
scanf ("%c",&ch);
fflush (stdin);
if (ch==' ')
{
*T=NULL;
}
else
{
*T=(BiTree *)malloc(sizeof(BiTree));
if (!T)
printf ("overflow");
CreatBiTree(&(*T)->lchild);
CreatBiTree(&(*T)->rchild);
(*T)->data=ch;
}
return 0;
}
/* 标准文档模板 */
#include "Stdio.h"
#include "Conio.h"
typedef struct BiTNode{
char data;
struct BiTNode * lchild,* rchild;
} BiTree ;
int main(void)
{
/* 此处添加你自己的代码 */
BiTree a;
BiTree *p;
p=&a;
CreatBiTree(&p);
Preorder(p);
printf ("\n");
inorderout(p);
printf ("\n");
Postorder(p);
getch();
return 0;
}
CreatBiTree(BiTree **T)
{
char ch;
scanf ("%c",&ch);
fflush (stdin);
if (ch==' ')
{
*T=NULL;
}
else
{
*T=(BiTree *)malloc(sizeof(BiTree));
if (!T)
printf ("overflow");
(*T)->data=ch;
printf ("%c\n",(*T)->data);
/* printf ("%d",T); */
CreatBiTree(&(*T)->lchild);
CreatBiTree(&(*T)->rchild);
}
return 0;
}
Preorder(BiTree *T)
{
if (T)
{
printf ("%c",T->data); /*访问结点数据*/
Preorder(T->lchild); /*先序遍历二叉树T的左子树*/
Preorder(T->rchild); /*先序遍历二叉树T的右子树*/
}
}
inorderout(BiTree *T)
{
if(T)
{
inorderout(T->lchild); /*中序遍历二叉树T的左子树*/
printf ("%c",T->data); /*访问结点数据*/
inorderout(T->rchild); /*中序遍历二叉树T的右子树*/
}
}
Postorder(BiTree *T)
{
if (T)
{
Postorder(T->lchild); /*后序遍历二叉树T的左子树*/
Postorder(T->rchild); /*后序遍历二叉树T的右子树*/
printf ("%c",T->data); /*访问结点数据*/
}
}
void main()
{
//bintree已经是一具指向树节点的指针了,你再声明成指针,那就是指针的指针了
//bintree *p,*t;
bintree tree;
//建立一个节点,并让树指南,以成为树根
tree = (bintree)malloc(sizeof(bintnode));
//*p=(bintnode *)malloc(sizeof(bintnode)); //这里不需要*
//根结点数据为空
//(*p)->data='0';
p->data = '0';
//建立左孩子
p->lchild = (bintnode*)malloc(sizeof(bintnode));
//....
}
typedef struct node{
///...
}node_t, *pnode_t;
typedef tree_t pnode_t;
tree_t mytree = (tree_t)malloc(sizeof(node_t));
mytree->lchild = (pnode_t)malloc(sizeof(node_t));
mytree->rchild = (pnode_t)malloc(sizeof(node_t));
mytree->data = 'x';
//....
//给个使用树的例子
void freetree(tree_t root)
{
if (root->lchild != NULL) freetree(root->lchild);
if (root->rchild != NULL) freetree(root->rchild);
free(root);
}