實驗2.2:二叉樹遍歷的一些應用
阿新 • • 發佈:2019-01-10
題目:以實驗2.1的二叉連結串列為儲存結構,編寫程式實現求二叉樹結點個數、葉子結點個數、二叉樹的高度以及交換二叉樹所有左右子樹的操作。
部分程式碼:
求二叉樹結點個數:
//求二叉樹結點個數
int Size(BinaryTreeNode *t){
if(!t) return 0;
return Size(t->LChild) + Size(t->RChild) + 1;
}
求二叉樹葉子結點個數:
//求二叉樹葉子結點個數 int Leaf(BinaryTreeNode *t){ if(t==NULL) return 0; if((t->LChild == NULL) && (t->RChild == NULL)) return 1; return Leaf(t->LChild) + Leaf(t->RChild); }
求二叉樹的高度:
//求二叉樹的高度
int Depth(BinaryTreeNode *t){
if(!t) return 0;
// else return (1 + Depth(t->LChild) > Depth(t->RChild) ? Depth(t->LChild) : Depth(t->RChild));
else return 1 + max(Depth(t->LChild) , Depth(t->RChild));
}
交換二叉樹:
//交換二叉樹 void Exch(BinaryTreeNode *t){ if(t){ BinaryTreeNode *q = t->LChild; t->LChild = t->RChild; t->RChild = q; Exch(t->LChild); Exch(t->RChild); } }
完整程式:
#include<stdio.h> #include<stdlib.h> #include<algorithm> using namespace std; typedef char T; typedef struct BinaryTreeNode{ T Data; struct BinaryTreeNode *LChild, *RChild; }BinaryTreeNode; //先序遍歷構建二叉樹 BinaryTreeNode *PreCreateBt(BinaryTreeNode *t){ char ch; ch = getchar(); if(ch == '#'){ //輸入為#表示這裡建立空二叉樹,即遍歷演算法的空操作 t = NULL; } else{ t = (BinaryTreeNode *)malloc(sizeof(BinaryTreeNode)); t->Data = ch; //構造根結點 t->LChild = PreCreateBt(t->LChild); //構造左子樹 t->RChild = PreCreateBt(t->RChild); //構造右子樹 } return t; } //先序遍歷 void PreOrderTransverse(BinaryTreeNode *t){ if(t==NULL){ return; } printf("%c",t->Data); //列印輸出根結點,此處可以定義其他操作 PreOrderTransverse(t->LChild); //然後先序遍歷左子樹 PreOrderTransverse(t->RChild); //最後先序遍歷右子樹 } //中序遍歷 void InOrderTransverse(BinaryTreeNode *t){ if(t==NULL){ return; } InOrderTransverse(t->LChild); //中序遍歷根結點的左子樹 printf("%c",t->Data); //列印輸出根結點,此處可以定義其他操作 InOrderTransverse(t->RChild); //最後中序遍歷根結點的右子樹 } //後序遍歷 void PostOrderTransverse(BinaryTreeNode *t){ if(t==NULL){ return; } PostOrderTransverse(t->LChild); //後序遍歷根結點的左子樹 PostOrderTransverse(t->RChild); //然後後序遍歷根結點的右子樹 printf("%c",t->Data); //最後列印輸出根結點,此處可以定義其他操作 } //求二叉樹結點個數 int Size(BinaryTreeNode *t){ if(!t) return 0; return Size(t->LChild) + Size(t->RChild) + 1; } //求二叉樹葉子結點個數 int Leaf(BinaryTreeNode *t){ if(t==NULL) return 0; if((t->LChild == NULL) && (t->RChild == NULL)) return 1; return Leaf(t->LChild) + Leaf(t->RChild); } //求二叉樹的高度 int Depth(BinaryTreeNode *t){ if(!t) return 0; // else return (1 + Depth(t->LChild) > Depth(t->RChild) ? Depth(t->LChild) : Depth(t->RChild)); else return 1 + max(Depth(t->LChild) , Depth(t->RChild)); } //交換二叉樹 void Exch(BinaryTreeNode *t){ if(t){ BinaryTreeNode *q = t->LChild; t->LChild = t->RChild; t->RChild = q; Exch(t->LChild); Exch(t->RChild); } } int main(){ BinaryTreeNode *t = NULL; t = PreCreateBt(t); printf("The number of Nodes:%d\n",Size(t)); printf("The number of LeafNodes:%d\n",Leaf(t)); printf("The height of the tree:%d\n",Depth(t)); Exch(t); printf("\nAfter exchanging the BinaryTree:\n\n"); printf("PreOrderTransverse:\n"); PreOrderTransverse(t); printf("\n\nInOrderTransverse:\n"); InOrderTransverse(t); printf("\n\nPostOrderTransverse:\n"); PostOrderTransverse(t); printf("\n"); return 0; }
實驗結果:輸入ABEH###C#D##MN###,分別計算結點數、葉子結點數、高度,然後進行二叉樹交換,再分別先序、中序、後序遍歷。