#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
typedef char datatype;
typedef struct node
{
datatype data;
struct node *lchild;
struct node *rchild;
}bitnode,*bitree;
//創建二叉鏈表
void createbitree(bitree *bt)
{
char ch;
ch=getchar();
if(ch='.')
bt=NULL;
else
{
*bt=(bitree)malloc(sizeof(bitnode));
(*bt)->data=https://bbs.csdn.net/topics/ch;
createbitree(&((*bt)->lchild));
createbitree(&((*bt)->rchild));
}
}
//先序遍歷二叉樹
void preorder(bitree root)
{
if(root!=NULL)
{
printf("%c",root->data); //訪問根節點
preorder(root->lchild); //先序遍歷左子樹
preorder(root->rchild); //先序遍歷右子樹
}
}
//中序遍歷二叉樹
void inorder(bitree root)
{
if(root!=NULL)
{
inorder(root->lchild); //先序遍歷左子樹
printf("%c",root->data); //訪問根節點
inorder(root->rchild); //先序遍歷右子樹
}
}
//后序遍歷二叉樹
void postorder(bitree root)
{
if(root!=NULL)
{
postorder(root->lchild); //先序遍歷左子樹
postorder(root->rchild); //先序遍歷右子樹
printf("%c",root->data); //訪問根節點
}
}
void main()
{
bitree p;
printf("請輸入所建立的二叉樹\n");
createbitree(&p);
printf("先序遍歷輸出二叉樹\n");
preorder(p);
printf("中序遍歷輸出二叉樹\n");
inorder(p);
printf("后序遍歷輸出二叉樹\n");
postorder(p);
}
哪兒錯了
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/208777.html
標籤:C++ 語言
上一篇:尋找漂亮的字串。。。求大神指點
下一篇:c語言陣列
