C語言數(shù)據(jù)結構二叉樹簡單應用
C語言數(shù)據(jù)結構二叉樹簡單應用
在計算機科學中,二叉樹是每個節(jié)點最多有兩個子樹的樹結構。通常子樹被稱作“左子樹”(left subtree)和“右子樹”(right subtree),接下來我就在這里給大家介紹一下二叉樹在算法中的簡單使用:
我們要完成總共有
(1)二叉樹的創(chuàng)建
(2)二叉樹的先中后序遞歸遍歷
(3)統(tǒng)計葉子結點的總數(shù)
(4)求樹的高度
(5)反轉二叉樹
(6)輸出每個葉子結點到根節(jié)點的路徑
(7)輸出根結點到每個葉子結點的路徑。
定義二叉樹結點類型的結構體
typedef struct node{
char data;
struct node *Lchild;
struct node *Rchild;
}BiTNode,*BiTree;
int cnt=0;//統(tǒng)計葉子節(jié)點個數(shù)
二叉樹的創(chuàng)建
BiTNode *Create(){ //二叉樹的先序建立
char ch;
BiTNode *s;
ch=getchar();
if(ch=='#')erchashu
return NULL;
s=(BiTNode *)malloc(sizeof(BiTNode));
s->data=ch;
s->Lchild=Create();
s->Rchild=Create();
return s;
}
二叉樹的先序、中序、后序遞歸遍歷
void PreOrder(BiTree root){ //前序遍歷
if(root){
printf("%c ",root->data);
PreOrder(root->Lchild);
PreOrder(root->Rchild);
}
}
void InOrder(BiTree root){ //中序遍歷
if(root){
InOrder(root->Lchild);
printf("%c ",root->data);
InOrder(root->Rchild);
}
}
void PostOrder(BiTree root){ //后序遍歷
if(root){
PostOrder(root->Lchild);
PostOrder(root->Rchild);
printf("%c ",root->data);
}
}
統(tǒng)計葉子結點個數(shù):
void LeafCountNode(BiTree root){ //統(tǒng)計葉子結點個數(shù)
if(root){
if(!root->Lchild && !root->Rchild)
cnt++;
LeafCountNode(root->Lchild);
LeafCountNode(root->Rchild);
}
}
輸出各個葉子結點值:
void IInOrder(BiTree root){ //輸出各個葉子結點值
if(root){
IInOrder(root->Lchild);
if(!root->Lchild && !root->Rchild)
printf("%c ",root->data);
IInOrder(root->Rchild);
}
}
求樹的高度:
int PostTreeDepth(BiTree root){ //求樹的高度
int h1,h2,h;
if(root==NULL){
return 0;
}
else{
h1=PostTreeDepth(root->Lchild);
h2=PostTreeDepth(root->Rchild);
h=(h1>h2?h1:h2)+1;
return h;
}
}
反轉二叉樹:
void MirrorTree(BiTree root){ //二叉樹鏡像樹
BiTree t;
if(root==NULL)
return;
else{
t=root->Lchild;
root->Lchild=root->Rchild;
root->Rchild=t;
MirrorTree(root->Lchild);
MirrorTree(root->Rchild);
}
}
輸出每個葉子結點到根節(jié)點的路徑:
void OutPutPath(BiTree root,char path[],int len){ //輸出每個葉子結點到根節(jié)點的路徑
if(root){
if(!root->Lchild && !root->Rchild){
printf("%c ",root->data);
for(int i=len-1;i>=0;i--)
printf("%c ",path[i]);
printf("\n");
}
path[len]=root->data;
OutPutPath(root->Lchild,path,len+1);
OutPutPath(root->Rchild,path,len+1);
}
}
輸出根到每個葉子結點的路徑:
void PrintPath(BiTree root,char path[],int l){ //輸出根到每個葉子結點的路徑
int len=l-1;
if(root){
if(root->Lchild==NULL && root->Rchild==NULL){
path[len]=root->data;
for(int i=9;i>=len;i--)
printf("%c ",path[i]);
printf("\n");
}
path[len]=root->data;
PrintPath(root->Lchild,path,len);
PrintPath(root->Rchild,path,len);
}
}
測試代碼:
int main(void){
int h,len;
char path[20];
BiTree root;
root=Create();
// PreOrder(root);
// printf("\n");
// InOrder(root);
// printf("\n");
// PostOrder(root);
// printf("\n");
// LeafCountNode(root);
// printf("葉子結點個數(shù)為:%d\n",cnt);
// IInOrder(root);
h=PostTreeDepth(root);
printf("樹的高度為:High=%d\n",h);
// PrintTree(root,0);
// MirrorTree(root);
// PrintTree(root,0);
// OutPutPath(root,path,0);
// PrintPath(root,path,10);
return 0;
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
欄 目:C語言
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/1555.html
您可能感興趣的文章
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用函數(shù)刪除字符
- 04-02c語言的正則匹配函數(shù) c語言正則表達式函數(shù)庫
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對數(shù)怎么表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段函數(shù)
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排序法函數(shù)
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段函數(shù)
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求階乘


閱讀排行
本欄相關
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11ajax實現(xiàn)頁面的局部加載
- 01-10C#中split用法實例總結
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改


