二叉樹
#include <stdio.h>
#include <stdlib.h>
//定義二叉樹的結點
typedef struct Node
{
char 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));
}
}
//遍歷二叉樹
void PreOrder(BiTree root){
/*先序遍歷二叉樹, root爲指向二叉樹(或某一子樹)根結點的指針*/
if(root != NULL){
printf("%c",root->data); /*輸出根節點*/
PreOrder(root->LChild); /*先序遍歷左子樹*/
PreOrder(root->RChild); /*先序遍歷右子樹*/
}
}
void InOrder(BiTree root)
/*中序遍歷二叉樹, root爲指向二叉樹(或某一子樹)根結點的指針*/
{ if (root!=NULL){
InOrder(root ->LChild); /*中序遍歷左子樹*/
printf("%c",root->data); /*訪問根結點*/
InOrder(root ->RChild); /*中序遍歷右子樹*/
}
}
void PostOrder(BiTree root)
/* 後序遍歷二叉樹,root爲指向二叉樹(或某一子樹)根結點的指針*/
{ if(root!=NULL){
PostOrder(root ->LChild); /*後序遍歷左子樹*/
PostOrder(root ->RChild); /*後序遍歷右子樹*/
printf("%c",root->data); /*訪問根結點*/
}
}
//遍歷二叉樹的葉子結點
void Pre(BiTree root )
{
if(root != NULL)
{
if(root ->LChild==NULL &&root->RChild==NULL)
{
printf("%c",root->data);
}
PreOrder(root ->LChild);//便利左子數
PreOrder(root ->RChild);//遍歷右子數
}
}
//統計葉子結點數
int LeafCount = 0;
void leaf(BiTree root)
{
if(root !=NULL)
{
leaf(root ->LChild);
leaf(root ->RChild);
if(root ->LChild ==NULL&& root ->RChild==NULL)
{
LeafCount++;
}
}
}
//二叉樹的高度
int depth = 1;
void PreTreeDepth(BiTree bt, int h)
{
if(bt != NULL)
{
if(h > depth)
{
depth = h;//如果節點層次大於depth,更新depth
}
PreTreeDepth(bt->LChild,h+1);
PreTreeDepth(bt->RChild,h+1);
}
}
int main()
{
BiTree t;
CreateBiTree(&t);
printf("先序二叉樹:");
PreOrder(t);
printf("\n");
printf("中序二叉樹:");
InOrder(t);
printf("\n");
printf("後序二叉樹:");
PostOrder(t);
printf("\n");
printf("葉子結點是:");
Pre(t);
printf("\n");
printf("葉子結點數是:");
leaf(t);
printf("%d\n",LeafCount);
PreTreeDepth(t,depth);
printf("二叉樹的高度是:%d\n",depth);
return 0;
}
#include <stdlib.h>
//定義二叉樹的結點
typedef struct Node
{
char 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));
}
}
//遍歷二叉樹
void PreOrder(BiTree root){
/*先序遍歷二叉樹, root爲指向二叉樹(或某一子樹)根結點的指針*/
if(root != NULL){
printf("%c",root->data); /*輸出根節點*/
PreOrder(root->LChild); /*先序遍歷左子樹*/
PreOrder(root->RChild); /*先序遍歷右子樹*/
}
}
void InOrder(BiTree root)
/*中序遍歷二叉樹, root爲指向二叉樹(或某一子樹)根結點的指針*/
{ if (root!=NULL){
InOrder(root ->LChild); /*中序遍歷左子樹*/
printf("%c",root->data); /*訪問根結點*/
InOrder(root ->RChild); /*中序遍歷右子樹*/
}
}
void PostOrder(BiTree root)
/* 後序遍歷二叉樹,root爲指向二叉樹(或某一子樹)根結點的指針*/
{ if(root!=NULL){
PostOrder(root ->LChild); /*後序遍歷左子樹*/
PostOrder(root ->RChild); /*後序遍歷右子樹*/
printf("%c",root->data); /*訪問根結點*/
}
}
//遍歷二叉樹的葉子結點
void Pre(BiTree root )
{
if(root != NULL)
{
if(root ->LChild==NULL &&root->RChild==NULL)
{
printf("%c",root->data);
}
PreOrder(root ->LChild);//便利左子數
PreOrder(root ->RChild);//遍歷右子數
}
}
//統計葉子結點數
int LeafCount = 0;
void leaf(BiTree root)
{
if(root !=NULL)
{
leaf(root ->LChild);
leaf(root ->RChild);
if(root ->LChild ==NULL&& root ->RChild==NULL)
{
LeafCount++;
}
}
}
//二叉樹的高度
int depth = 1;
void PreTreeDepth(BiTree bt, int h)
{
if(bt != NULL)
{
if(h > depth)
{
depth = h;//如果節點層次大於depth,更新depth
}
PreTreeDepth(bt->LChild,h+1);
PreTreeDepth(bt->RChild,h+1);
}
}
int main()
{
BiTree t;
CreateBiTree(&t);
printf("先序二叉樹:");
PreOrder(t);
printf("\n");
printf("中序二叉樹:");
InOrder(t);
printf("\n");
printf("後序二叉樹:");
PostOrder(t);
printf("\n");
printf("葉子結點是:");
Pre(t);
printf("\n");
printf("葉子結點數是:");
leaf(t);
printf("%d\n",LeafCount);
PreTreeDepth(t,depth);
printf("二叉樹的高度是:%d\n",depth);
return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.