二叉树路径应用举例(基于非递归后序遍历)
生活随笔
收集整理的這篇文章主要介紹了
二叉树路径应用举例(基于非递归后序遍历)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
#include "stdafx.h"
#include <iostream>
#include <fstream>using namespace std;typedef struct _Node
{int data;struct _Node *left;struct _Node *right;bool isVisit; //后序遍歷標志(非遞歸)
_Node(){data = 0;left = NULL;right = NULL;isVisit = false;}
}Node, *_PNode;#define MAXSIZE 100//創(chuàng)建二叉樹利用先序創(chuàng)建
/*1/ \2 3/ \ / 4 3 6/ \ \ / \7 8 9 10 11/ \12 13/14*/
void CreateBitree(_PNode &pNode, fstream &fin)
{int dat;fin>>dat;if(dat==0){pNode = NULL;}else {pNode = new Node();pNode->data=dat; CreateBitree(pNode->left, fin); CreateBitree(pNode->right, fin);}
}//***********************二叉樹路徑應(yīng)用舉例(基于非遞歸后序遍歷)***************************begin//求根節(jié)點到所有葉子結(jié)點的路徑
void FindAllRootToLeafPath(_PNode pRoot)
{_PNode pTree = pRoot;_PNode s[MAXSIZE];int top = 0;while (top > 0 || NULL != pTree){while (NULL != pTree){if (NULL == pTree->left && NULL == pTree->right){for (int i = 1; i <= top; i++){cout<<s[i]->data<<" ";}cout<<pTree->data<<" "<<endl;}s[++top] = pTree;pTree = pTree->left;}if (top > 0){pTree = s[top];if (pTree->isVisit){top--;pTree = NULL;}else{pTree->isVisit = true;pTree = pTree->right;}}}
}//求根節(jié)點到指定結(jié)點的路徑
void FindRootToNodePath(_PNode pRoot, int key)
{_PNode pTree = pRoot;_PNode s[MAXSIZE];int top = 0;while (top > 0 || NULL != pTree){while (NULL != pTree){if (pTree->data == key){for (int i = 1; i <= top; i++){cout<<s[i]->data<<" ";}cout<<pTree->data<<" "<<endl;}s[++top] = pTree;pTree = pTree->left;}if (top > 0){pTree = s[top];if (pTree->isVisit){top--;pTree = NULL;}else{pTree->isVisit = true;pTree = pTree->right;}}}
}//求二叉樹第一條最長的路徑長度
void FindLongestPath(_PNode pRoot)
{_PNode pTree = pRoot;_PNode s[MAXSIZE] = {0};_PNode tmp[MAXSIZE] = {0}; //存放最長路徑int top = 0;int longest = 0;while (top > 0 || NULL != pTree){while (NULL != pTree){s[++top] = pTree;pTree = pTree->left;}if (top > 0){pTree = s[top];if (pTree->isVisit){if (top > longest){longest = top;for (int i = 1; i <= top; i++){tmp[i] = s[i];}}top--;pTree = NULL;}else{pTree->isVisit = true;pTree = pTree->right;}}}for (int i = 1; i <= longest; i++){cout<<tmp[i]->data<<" ";}
}//設(shè)置后序訪問標志為未訪問,即false
void SetVisitFalse(_PNode pRoot)
{if (NULL != pRoot){pRoot->isVisit = false;SetVisitFalse(pRoot->left);SetVisitFalse(pRoot->right);}
}//***********************二叉樹路徑應(yīng)用舉例(基于非遞歸后序遍歷)***************************endint _tmain(int argc, _TCHAR* argv[])
{fstream fin("tree.txt");_PNode pRoot = NULL;CreateBitree(pRoot, fin);SetVisitFalse(pRoot);cout<<"********************求根節(jié)點到所有葉子結(jié)點的路徑***********************"<<endl;FindAllRootToLeafPath(pRoot);SetVisitFalse(pRoot);cout<<"********************求二叉樹第一條最長的路徑長度***********************"<<endl;FindLongestPath(pRoot);SetVisitFalse(pRoot);cout<<endl<<"*********************求根節(jié)點到指定結(jié)點的路徑**************************"<<endl;FindRootToNodePath(pRoot, 13); //根結(jié)點到值為13的結(jié)點的路徑
cout<<endl;return 0;
}
運行界面如下:
建造二叉樹的tree.txt文件如下:
1 2 4 7 12 0 0 0 8 0 13 14 0 0 0 3 0 9 0 0 3 6 10 0 0 11 0 0 0轉(zhuǎn)載于:https://www.cnblogs.com/venow/archive/2012/08/24/2653969.html
總結(jié)
以上是生活随笔為你收集整理的二叉树路径应用举例(基于非递归后序遍历)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 批量离线下载迅雷快传资源
- 下一篇: 人工智能:一种现代方法汇总