#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 //创建二叉树利用先序创建 /* 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); } } //***********************二叉树路径应用举例(基于非递归后序遍历)***************************begin //求根节点到所有叶子结点的路径 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; } } } } //求根节点到指定结点的路径 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<<" "; } } //设置后序访问标志为未访问,即false void SetVisitFalse(_PNode pRoot) { if (NULL != pRoot) { pRoot->isVisit = false; SetVisitFalse(pRoot->left); SetVisitFalse(pRoot->right); } } //***********************二叉树路径应用举例(基于非递归后序遍历)***************************end int _tmain(int argc, _TCHAR* argv[]) { fstream fin("tree.txt"); _PNode pRoot = NULL; CreateBitree(pRoot, fin); SetVisitFalse(pRoot); cout<<"********************求根节点到所有叶子结点的路径***********************"<<endl; FindAllRootToLeafPath(pRoot); SetVisitFalse(pRoot); cout<<"********************求二叉树第一条最长的路径长度***********************"<<endl; FindLongestPath(pRoot); SetVisitFalse(pRoot); cout<<endl<<"*********************求根节点到指定结点的路径**************************"<<endl; FindRootToNodePath(pRoot, 13); //根结点到值为13的结点的路径 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