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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++实现二叉树的相应操作

發布時間:2025/3/15 c/c++ 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++实现二叉树的相应操作 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. 二叉樹的遍歷:先序(遞歸、非遞歸),中序(遞歸、非遞歸),后序(遞歸、非遞歸)。

#include <iostream> #include <string> #include <stack>using namespace std;struct BiTree {int NodeData = 0;struct BiTree *pLeft = nullptr;struct BiTree *pRight = nullptr; };//遍歷二叉樹: void show(struct BiTree *pRoot, int n) {if (pRoot == nullptr){return;}else{show(pRoot->pLeft, n + 1);for (int i = 0; i < n; i++)cout << " ";cout << pRoot->NodeData << endl;show(pRoot->pRight, n + 1);}} //-------------------------------------------------------------- //遞歸中序遍歷: void RecMidTravel(struct BiTree *pRoot) {if (pRoot == nullptr){return;}else {if (pRoot->pLeft != nullptr){RecMidTravel(pRoot->pLeft);}cout << pRoot->NodeData << endl;if (pRoot->pRight != nullptr){RecMidTravel(pRoot->pRight);}} } //中序非遞歸 void MidTravel(struct BiTree *pRoot) {if (pRoot == nullptr){return;}else{struct BiTree *pcur = pRoot;stack<BiTree *> mystack;while (!mystack.empty() || pcur != nullptr){while (pcur != nullptr){mystack.push(pcur);pcur = pcur->pLeft; //左節點全部進棧 }if (!mystack.empty()){pcur = mystack.top();cout << pcur->NodeData << endl;mystack.pop(); //出棧pcur = pcur->pRight; //右節點 }}} } //-------------------------------------------------------------- //遞歸先序遍歷: void RecPreTravel(struct BiTree *pRoot) {if (pRoot == nullptr){return;}else{cout << pRoot->NodeData << endl;if (pRoot->pLeft != nullptr){RecPreTravel(pRoot->pLeft);}if (pRoot->pRight != nullptr){RecPreTravel(pRoot->pRight);}} } //先序非遞歸 void PreTravel(struct BiTree *pRoot) {if (pRoot == nullptr){return;}else{struct BiTree *pcur = pRoot;stack<BiTree *> mystack;while (!mystack.empty() || pcur != nullptr){while (pcur != nullptr){cout << pcur->NodeData << endl;mystack.push(pcur);pcur = pcur->pLeft; //左節點全部進棧 }if (!mystack.empty()){pcur = mystack.top();mystack.pop(); //出棧pcur = pcur->pRight; //右節點 }}} }//-------------------------------------------------------------- //遞歸后序遍歷: void RecPostTravel(struct BiTree *pRoot) {if (pRoot == nullptr){return;}else{if (pRoot->pLeft != nullptr){RecPostTravel(pRoot->pLeft);}if (pRoot->pRight != nullptr){RecPostTravel(pRoot->pRight);}cout << pRoot->NodeData << endl;} } //后序非遞歸 struct nosame //標識節點是否反復出現 {struct BiTree *pnode;bool issame; };void PostTravel(struct BiTree *pRoot) {if (pRoot == nullptr){return;}else{struct BiTree *pcur = pRoot;stack<nosame *> mystack; //避免重復出現nosame *ptemp;while (!mystack.empty() || pcur != nullptr){while (pcur != nullptr){nosame *ptr = new nosame;ptr->issame = true;ptr->pnode = pcur;//節點//cout << pcur->NodeData << endl; mystack.push(ptr);pcur = pcur->pLeft; //左節點全部進棧 }if (!mystack.empty()){ptemp = mystack.top();mystack.pop(); //出棧if (ptemp->issame == true) //第一次出現 {ptemp->issame = false;mystack.push(ptemp);pcur = ptemp->pnode->pRight;//跳到右節點 }else{cout << ptemp->pnode->NodeData << endl;//打印數據pcur = nullptr;}}}} }void main() {struct BiTree *pRoot;struct BiTree node1;struct BiTree node2;struct BiTree node3;struct BiTree node4;struct BiTree node5;struct BiTree node6;struct BiTree node7;struct BiTree node8;node1.NodeData = 1;node2.NodeData = 2;node3.NodeData = 3;node4.NodeData = 4;node5.NodeData = 5;node6.NodeData = 6;node7.NodeData = 7;node8.NodeData = 8;pRoot = &node1;node1.pLeft = &node2;node1.pRight = &node3;node2.pLeft = &node4;node2.pRight = &node5;node3.pLeft = &node6;node3.pRight = &node7;node4.pLeft = &node8;show(pRoot, 1);cout << "中序遞歸:" << endl;RecMidTravel(pRoot); //中序遞歸cout << "中序非遞歸:" << endl;MidTravel(pRoot); //中序非遞歸 cout << "先序遞歸:" << endl;RecPreTravel(pRoot);cout << "先序非遞歸:" << endl;PreTravel(pRoot); //先序非遞歸 cout << "后序遞歸:" << endl;RecPostTravel(pRoot);cout << "后序非遞歸:" << endl;PostTravel(pRoot); //后序非遞歸 cin.get(); }

?    

?2. 獲取二叉樹節點個數:

//遞歸獲取二叉樹節點個數 int getNodeCount(BiTree *pRoot) {if (pRoot == nullptr){return 0;}else{return getNodeCount(pRoot->pLeft) + getNodeCount(pRoot->pRight) + 1;} }

    

3. 判斷二叉樹是否為完全二叉樹:

//判斷二叉樹是否為完全二叉樹 bool isCompleteBiTree(BiTree *pRoot) {if (pRoot == nullptr){return false;}else{queue<BiTree *> myq;myq.push(pRoot);bool mustHaveChild = false; //必須有子節點bool result = true; //結果while (!myq.empty()){BiTree *node = myq.front();//頭結點myq.pop(); //出隊if (mustHaveChild) //必須有孩子 {if (node->pLeft != nullptr || node->pRight != nullptr){result = false;break;}} else{if (node->pLeft != nullptr && node->pRight != nullptr){myq.push(node->pLeft);myq.push(node->pRight);}else if (node->pLeft != nullptr && node->pRight == nullptr){mustHaveChild = true;myq.push(node->pLeft);}else if (node->pLeft == nullptr && node->pRight != nullptr){result = false;break;}else{mustHaveChild = true;}}}return result;} }

    

4. 求二叉樹兩個節點的最小公共祖先:

//求二叉樹兩個節點的最小公共祖先 bool findnode(BiTree *pRoot, BiTree *node) //判斷節點是否在某個節點下 {if (pRoot == nullptr || node == nullptr){return false;}if (pRoot == node){return true;}bool isfind = findnode(pRoot->pLeft, node);if (!isfind){isfind = findnode(pRoot->pRight, node);}return isfind; }BiTree *getParent(BiTree *pRoot, BiTree *pChild1, BiTree *pChild2) {if (pRoot == pChild1 || pRoot == pChild2){return pRoot;}if (findnode(pRoot->pLeft, pChild1)){if (findnode(pRoot->pRight, pChild2)){return pRoot;} else{return getParent(pRoot->pLeft, pChild1, pChild2);}} else{if (findnode(pRoot->pLeft, pChild2)){return pRoot;}else{return getParent(pRoot->pRight, pChild1, pChild2);}} }

    

5. 二叉樹的翻轉:

//二叉樹的翻轉 BiTree *revBiTree(BiTree *pRoot) {if (pRoot==nullptr){return nullptr;}BiTree *leftp = revBiTree(pRoot->pLeft);BiTree *rightp = revBiTree(pRoot->pRight);pRoot->pLeft = rightp;pRoot->pRight = leftp; //交換return pRoot; }

    

6. 求二叉樹第k層的節點個數:

//求二叉樹第K層的節點個數 int getLevelConut(BiTree *pRoot, int k) {if (pRoot == nullptr || k < 1){return 0;}if (k == 1){return 1;}else{int left = getLevelConut(pRoot->pLeft, k - 1);int right = getLevelConut(pRoot->pRight, k - 1);return (left + right);} }

    

7. 求二叉樹中節點的最大距離(相距最遠的兩個節點之間的距離):

//求二叉樹中節點的最大距離 struct res //用以遞歸間傳遞距離 {int maxDistance = 0;int maxDepth = 0; };res getMaxDistance(BiTree *pRoot) {if (pRoot == nullptr){res r1;return r1;}res leftr = getMaxDistance(pRoot->pLeft);res rightr = getMaxDistance(pRoot->pRight);res last; //最終結果last.maxDepth = max(leftr.maxDepth + 1, rightr.maxDepth + 1);//求最大深度last.maxDistance = max(max(leftr.maxDistance, rightr.maxDistance), leftr.maxDepth + rightr.maxDepth + 2);//求最大距離return last; }

    

8. 判斷二叉樹是否為平衡二叉樹:

//判斷二叉樹是否為平衡二叉樹: bool isAVL(BiTree *pRoot, int & depth) //需要引用來傳遞數據 {if (pRoot == nullptr){depth = 0;return true;}int leftdepth = 0;int rightdepth = 0;bool left = isAVL(pRoot->pLeft, leftdepth);bool right = isAVL(pRoot->pRight, rightdepth);if (left && right && abs(leftdepth - rightdepth) <= 1){depth = 1 + (leftdepth > rightdepth ? leftdepth : rightdepth);//深度return true;}else{return false;} }

        

?

轉載于:https://www.cnblogs.com/si-lei/p/9547016.html

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的C++实现二叉树的相应操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: a级片一级片 | 色偷偷av男人的天堂 | 五月婷婷视频在线观看 | 野外性满足hd | 四十路av | 少妇特殊按摩高潮惨叫无码 | 91视频精选| 夜夜操网址| 天天干天天天天 | 日韩美女激情视频 | 999精品一区 | 日本xxxxwwwww| 欧美一二区视频 | 亚洲aⅴ网站 | 蜜臀av88| 欧美日韩精品一区二区在线播放 | 亚洲三级网站 | 欧美人禽杂交狂配 | 隔壁人妻偷人bd中字 | 五月天久久婷婷 | 亚洲色图五月天 | 欧美日韩国产中文 | 91热久久 | 97超碰97| 五月伊人网 | 一级性生活毛片 | 国产欧美一区二区三区精品酒店 | 亚洲精品久久久 | 超碰pron | 俄罗斯av片 | 国产a级大片 | 99性趣网 | 成人91视频 | 日本热久久 | 亚洲人成电影一区二区在线 | 色www亚洲国产张柏芝 | 国产日产精品一区二区三区四区 | 最近中文字幕mv免费高清在线 | 精品人妻一区二区三区视频 | 欧洲精品一区二区三区久久 | 国产日韩一级片 | 制服丝袜先锋 | 国产精品美女久久久久久久 | 乱子伦一区二区三区 | 欧美丰满老熟妇aaaa片 | 日韩精品免费一区二区三区竹菊 | 亲子乱aⅴ一区二区三区 | 日本全黄裸体片 | 国产精品5区 | 欧美h在线观看 | 日韩精品一区二区三区高清免费 | 成人网在线视频 | 久久久久久久偷拍 | 国产精品国产三级国产a | 性欧美video另类hd尤物 | 四虎av网站| 欧美在线激情 | 国产在线观看免费高清 | 香港a毛片 | 女性向av免费网站 | 国产精品一区二区在线看 | 精品久久久久久久 | 自拍偷拍在线视频 | 影音先锋男人资源网站 | www九色| 自拍偷拍av | 蜜桃视频导航 | 精品国产影院 | 亚洲av中文无码乱人伦在线观看 | 爱爱免费小视频 | 欧美日韩综合网 | 一区二区乱子伦在线播放 | 黄色大片视频 | 免费在线看a | 小泽玛利亚一区二区三区 | 特色黄色片| 国产一区91精品张津瑜 | 国产成人av一区二区三区在线观看 | 精品一区二区久久久 | 亚洲天堂一 | 亚洲奶水xxxx哺乳期 | 国产中文字幕久久 | 高清性爱视频 | 欧美一级射 | 国产第一网站 | 国产精品婷婷午夜在线观看 | 久久婷婷网站 | 牛牛影视一区二区三区 | 国产女主播一区 | 国产老头老太作爱视频 | 黄色激情视频网站 | 欧美少妇一区二区三区 | 全球av在线 | 中国爆后菊女人的视频 | 精品国模一区二区三区欧美 | 影音先锋中文字幕在线 | 永久免费未满蜜桃 | 亚洲自拍成人 | 日韩国产综合 |