C語言數(shù)據(jù)結(jié)構(gòu)二叉樹簡單應(yīng)用
C語言數(shù)據(jù)結(jié)構(gòu)二叉樹簡單應(yīng)用
在計(jì)算機(jī)科學(xué)中,二叉樹是每個(gè)節(jié)點(diǎn)最多有兩個(gè)子樹的樹結(jié)構(gòu)。通常子樹被稱作“左子樹”(left subtree)和“右子樹”(right subtree),接下來我就在這里給大家介紹一下二叉樹在算法中的簡單使用:
我們要完成總共有
(1)二叉樹的創(chuàng)建
(2)二叉樹的先中后序遞歸遍歷
(3)統(tǒng)計(jì)葉子結(jié)點(diǎn)的總數(shù)
(4)求樹的高度
(5)反轉(zhuǎn)二叉樹
(6)輸出每個(gè)葉子結(jié)點(diǎn)到根節(jié)點(diǎn)的路徑
(7)輸出根結(jié)點(diǎn)到每個(gè)葉子結(jié)點(diǎn)的路徑。
定義二叉樹結(jié)點(diǎn)類型的結(jié)構(gòu)體
typedef struct node{
char data;
struct node *Lchild;
struct node *Rchild;
}BiTNode,*BiTree;
int cnt=0;//統(tǒng)計(jì)葉子節(jié)點(diǎn)個(gè)數(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)計(jì)葉子結(jié)點(diǎn)個(gè)數(shù):
void LeafCountNode(BiTree root){ //統(tǒng)計(jì)葉子結(jié)點(diǎn)個(gè)數(shù)
if(root){
if(!root->Lchild && !root->Rchild)
cnt++;
LeafCountNode(root->Lchild);
LeafCountNode(root->Rchild);
}
}
輸出各個(gè)葉子結(jié)點(diǎn)值:
void IInOrder(BiTree root){ //輸出各個(gè)葉子結(jié)點(diǎn)值
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;
}
}
反轉(zhuǎn)二叉樹:
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);
}
}
輸出每個(gè)葉子結(jié)點(diǎn)到根節(jié)點(diǎn)的路徑:
void OutPutPath(BiTree root,char path[],int len){ //輸出每個(gè)葉子結(jié)點(diǎn)到根節(jié)點(diǎn)的路徑
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);
}
}
輸出根到每個(gè)葉子結(jié)點(diǎn)的路徑:
void PrintPath(BiTree root,char path[],int l){ //輸出根到每個(gè)葉子結(jié)點(diǎn)的路徑
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("葉子結(jié)點(diǎn)個(gè)數(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語言構(gòu)建基本的二叉樹數(shù)據(jù)結(jié)構(gòu)
- 舉例講解C語言程序中對二叉樹數(shù)據(jù)結(jié)構(gòu)的各種遍歷方式
- C語言 數(shù)據(jù)結(jié)構(gòu)平衡二叉樹實(shí)例詳解
- C語言 數(shù)據(jù)結(jié)構(gòu)之中序二叉樹實(shí)例詳解
- C語言數(shù)據(jù)結(jié)構(gòu)之線索二叉樹及其遍歷
- C語言數(shù)據(jù)結(jié)構(gòu)之二叉樹的非遞歸后序遍歷算法
- C語言實(shí)現(xiàn)二叉樹遍歷的迭代算法
- C語言 二叉樹的鏈?zhǔn)酱鎯?shí)例
- C語言二叉樹的非遞歸遍歷實(shí)例分析
- C語言實(shí)現(xiàn)找出二叉樹中某個(gè)值的所有路徑的方法
- C語言數(shù)據(jù)結(jié)構(gòu)之平衡二叉樹(AVL樹)實(shí)現(xiàn)方法示例
相關(guān)文章
C語言popen函數(shù)調(diào)用其他進(jìn)程返回值示例詳解
這篇文章主要為大家介紹了C語言popen函數(shù)調(diào)用其他進(jìn)程返回值示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-09-09
C 語言基礎(chǔ)實(shí)現(xiàn)青蛙跳臺階和漢諾塔問題
這篇文章我們九里講講C 語言基礎(chǔ)實(shí)現(xiàn)青蛙跳臺階和漢諾塔問題,感興趣的小伙伴可以參考下面文章的具體內(nèi)容2021-09-09
Qt實(shí)戰(zhàn)之實(shí)現(xiàn)圖片瀏覽器
這篇文章主要為大家詳細(xì)介紹了如何利用Qt實(shí)現(xiàn)簡易的圖片瀏覽器,文中的示例代碼講解詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴可以了解一下2023-03-03
C++生成隨機(jī)數(shù)的實(shí)現(xiàn)代碼
這篇文章主要介紹了C++生成隨機(jī)數(shù)的實(shí)現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

