?? 實習題1.txt
字號:
#include "bitree.h"
void Visit(char ch)
{
printf("%c ",ch);
}
void PreOrder(BiTree root)
/*先序遍歷二叉樹, root為指向二叉樹(或某一子樹)根結點的指針*/
{
if (root!=NULL)
{
Visit(root->data); /*訪問根結點*/
PreOrder(root->LChild); /*先序遍歷左子樹*/
PreOrder(root->RChild); /*先序遍歷右子樹*/
}
}
void InOrder(BiTree root)
/*中序遍歷二叉樹, root為指向二叉樹(或某一子樹)根結點的指針*/
{
if (root!=NULL)
{
InOrder(root->LChild); /*中序遍歷左子樹*/
Visit(root->data); /*訪問根結點*/
InOrder(root->RChild); /*中序遍歷右子樹*/
}
}
void PostOrder(BiTree root)
/* 后序遍歷二叉樹,root為指向二叉樹(或某一子樹)根結點的指針*/
{
if(root!=NULL)
{
PostOrder(root->LChild); /*后序遍歷左子樹*/
PostOrder(root->RChild); /*后序遍歷右子樹*/
Visit(root->data); /*訪問根結點*/
}
}
void main()
{
BiTree T;
printf("請輸入二叉樹序列:\n");
CreateBiTree(&T);
printf("先序遍歷序列為:");
PreOrder(T);
printf("\n中序遍歷序列為:");
InOrder(T);
printf("\n后序遍歷序列為:");
PostOrder(T);
getch();
}
//其中的bitree.h內容如下//
#include <stdio.h>
#include <malloc.h>
#include <conio.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=ch;
CreateBiTree(&((*bt)->LChild)); //生成左子樹
CreateBiTree(&((*bt)->RChild)); //生成右子樹
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -