日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

二叉树02

發(fā)布時(shí)間:2024/7/19 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉树02 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

為了加強(qiáng)記憶我又寫了一遍二叉樹(shù)的基本代碼。比上一次多了一點(diǎn)點(diǎn)功能。

1 #include<stdio.h> 2 #include<iostream> 3 using namespace std; 4 struct Bitree{//二叉樹(shù)的結(jié)構(gòu) 5 int val; 6 Bitree *lchild,*rchild; 7 }; 8 void creat(Bitree * &T)//創(chuàng)建樹(shù) 9 { 10 int ch; 11 scanf("%d",&ch); 12 if(ch==0) 13 T=NULL; 14 else 15 { 16 T=new Bitree; 17 T->val=ch; 18 creat(T->lchild); 19 creat(T->rchild); 20 } 21 } 22 void qshow(Bitree *T)//前序遍歷一棵樹(shù) 23 { 24 if(T!=NULL) 25 { 26 printf("%d\t",T->val); 27 qshow(T->lchild); 28 qshow(T->rchild); 29 } 30 } 31 void zshow(Bitree *T)//中序遍歷一棵樹(shù) 32 { 33 if(T!=NULL) 34 { 35 zshow(T->lchild); 36 printf("%d\t",T->val); 37 zshow(T->rchild); 38 } 39 } 40 void hshow(Bitree *T)//后序遍歷一棵樹(shù) 41 { 42 if(T!=NULL) 43 { 44 hshow(T->lchild); 45 hshow(T->rchild); 46 printf("%d\t",T->val); 47 } 48 } 49 void copy(Bitree *T,Bitree * &T1)//復(fù)制一棵樹(shù) 50 { 51 if(T==NULL) 52 T1=NULL; 53 else 54 { 55 T1=new Bitree; 56 T1->val=T->val; 57 copy(T->lchild,T1->lchild); 58 copy(T->rchild,T1->rchild); 59 } 60 } 61 void delete_(Bitree * &T,int e)//刪除一個(gè)值為e的結(jié)點(diǎn)及其所有孩子 62 { 63 if(T==NULL) 64 return; 65 else 66 { 67 if(T->val==e) 68 T=NULL; 69 else 70 { 71 delete_(T->lchild,e); 72 delete_(T->rchild,e); 73 } 74 } 75 } 76 int countyz(Bitree *T)//統(tǒng)計(jì)葉子結(jié)點(diǎn)數(shù)目 77 { 78 int count1,count2; 79 if(T==NULL) 80 return 0; 81 if(T->lchild==NULL&&T->rchild==NULL) 82 return 1; 83 else 84 { 85 count1=countyz(T->lchild); 86 count2=countyz(T->rchild); 87 } 88 return count1+count2; 89 } 90 int maxdeep(Bitree *T)//樹(shù)的最大深度 91 { 92 if(T==NULL) 93 return 0; 94 else 95 { 96 int rlength,llength; 97 rlength=maxdeep(T->rchild)+1; 98 llength=maxdeep(T->lchild)+1; 99 return llength>rlength?llength:rlength; 100 } 101 } 102 int mindeep(Bitree *T)//二叉樹(shù)的最小深度 103 { 104 if(T==NULL) 105 return 0; 106 if(T->lchild==NULL&&T->rchild==NULL) 107 return 1; 108 else if(T->lchild!=NULL) 109 return mindeep(T->lchild)+1; 110 else if(T->rchild!=NULL) 111 return mindeep(T->rchild)+1; 112 else 113 { 114 int rl=mindeep(T->rchild)+1; 115 int ll=mindeep(T->lchild)+1; 116 return rl>ll?ll:rl; 117 } 118 } 119 /*int main() 120 { 121 Bitree *T,*T1; 122 creat(T); 123 qshow(T); 124 printf("\n"); 125 copy(T,T1); 126 qshow(T1); 127 printf("\n"); 128 delete_(T,3); 129 qshow(T); 130 printf("\n"); 131 printf("%d\n",countyz(T1)); 132 printf("%d\n",maxdeep(T1)); 133 printf("%d\n",mindeep(T1)); 134 return 0; 135 } */

小白在成長(zhǎng)

轉(zhuǎn)載于:https://www.cnblogs.com/97-ly/p/6840101.html

總結(jié)

以上是生活随笔為你收集整理的二叉树02的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。