二元树的生成、遍历、以及最短最长路径查询
来源:互联网 发布:手机登不了淘宝怎么办 编辑:程序博客网 时间:2024/05/29 11:09
二元树:
每个节点有两个子节点,左子节点和右子节点。
节点结构:
typedef struct NODE
{
char val;
NODE *left;
NODE *right;
} NODE ;
生成二元树:
利用递归算法,不断生成新的节点,并加入树中,‘#’代表空节点
NODE* TreeConstructor(){char ch;cin>>ch;NODE *root;if (ch=='#'){return NULL;}else{root=new NODE;root->val=ch;root->left=TreeConstructor();root->right=TreeConstructor();return root;}}
遍历二元树:
深度遍历二元树:利用stl容器stack实现,节点先进后出,先入右节点,后入左节点,即先进后出、先右后左;
步骤:
1,入根节点;
2,出栈顶元素;
3,若栈顶元素右孩子非空,入栈;
4,若栈顶元素左孩子非空,入栈;
5,若栈非空,转向2.
void depthFirstSearch(NODE *root)
{
stack<NODE*> NODE_stack;
NODE_stack.push(root);
NODE *node;
while(!NODE_stack.empty())
{
node=NODE_stack.top();
NODE_stack.pop();
cout<<node->val<<" ";
if (node->right!=NULL)
{
NODE_stack.push(node->right);
}
if (node->left!=NULL)
{
NODE_stack.push(node->left);
}
}
}
{
stack<NODE*> NODE_stack;
NODE_stack.push(root);
NODE *node;
while(!NODE_stack.empty())
{
node=NODE_stack.top();
NODE_stack.pop();
cout<<node->val<<" ";
if (node->right!=NULL)
{
NODE_stack.push(node->right);
}
if (node->left!=NULL)
{
NODE_stack.push(node->left);
}
}
}
广度遍历二元树:利用stl容器queue实现,节点先进先出,先入左节点,后入右节点,即先进先出、先左后右;
步骤:
1,入根节点;
2,出队首元素;
3,若队首元素左孩子非空,加入队尾;
4,若队首元素右孩子非空,加入队尾;
5,若队非空,转向2.
void breadFirstSearch(NODE *root){
queue<NODE*> NODE_queue;
NODE_queue.push(root);
NODE *node;
while(!NODE_queue.empty())
{
node=NODE_queue.front();
NODE_queue.pop();
cout<<node->val<<" ";
if (node->left!=NULL)
{
NODE_queue.push(node->left);
}
if (node->right!=NULL)
{
NODE_queue.push(node->right);
}
}
}
先序、中序、后序遍历:均采用递归算法;
最短路径:
方法一:利用递归算法
int minDepth(NODE *root){if (root==NULL) return 0;if (root->left==NULL&&root->right==NULL) return 1;int leftDepth=minDepth(root->left);int rightDepth=minDepth(root->right);if (leftDepth==0){return rightDepth+1;}if (rightDepth==0){return leftDepth+1;}return min(leftDepth,rightDepth)+1;}
假设有如下树:
递归演示图如下:
其中1,2,3,4,5,6指的是运行的步骤,由左向右依次调用minDepth函数,黑线为初始进入函数,函数为函数返回路线。
那么同理,求最长路径也可用递归的方法,代码如下:
int maxDepth(TreeNode* root) { if(root==NULL) return 0; if(root->left==NULL&&root->right==NULL) return 1; int left=maxDepth(root->left); int right=maxDepth(root->right); if(left==0) return right+1; if(right==0) return left+1; return max(left,right)+1; }
方法二:
对二叉树进行BFS,由于是按层遍历的,因此如果在某一层发现了一个叶子节点,那么就找到了最小深度,此时返回当前深度即可。
代码如下:
int minDepth1(NODE *root) { if (root == NULL) return 0; int depth = 1; int currentLevel = 1; int nextLevel = 0; queue<NODE*> NODE_queue;NODE_queue.push(root); while (!NODE_queue.empty()) { NODE *node = NODE_queue.front(); NODE_queue.pop(); currentLevel--; if (node->left == NULL && node->right == NULL) { return depth; } if (node->left != NULL) { NODE_queue.push(node->left); nextLevel++; } if (node->right != NULL) { NODE_queue.push(node->right); nextLevel++; } if (currentLevel == 0) { if (nextLevel != 0) { depth++; } currentLevel = nextLevel; nextLevel = 0; } } return depth; }完整代码如下:
binary_tree.h
#include <iostream>#include <queue>#include <stack>#include <vector>#include <algorithm>using namespace std;typedef struct NODE{char val;NODE *left;NODE *right;} NODE ;enum ORDER_MODE { ORDER_MODE_PREV = 0, ORDER_MODE_MID, ORDER_MODE_POST };//枚举,代表遍历树的方式(前序,中序,后序)NODE* TreeConstructor();void depthFirstSearch(NODE *root);void breadFirstSearch(NODE *root);int minDepth(NODE *root); int minDepth1(NODE *root); void printTree(ORDER_MODE method,NODE *root); void printTreeInPre(NODE *root) ; void printTreeInMid(NODE *root) ; void printTreeInPost(NODE *root) ;
main.cpp:
#include "binary_tree.h"int index=0;NODE* TreeConstructor(){char ch;cin>>ch;NODE *root;if (ch=='#'){return NULL;}else{root=new NODE;root->val=ch;root->left=TreeConstructor();root->right=TreeConstructor();return root;}}void depthFirstSearch(NODE *root){stack<NODE*> NODE_stack;NODE_stack.push(root);NODE *node;while(!NODE_stack.empty()){node=NODE_stack.top();NODE_stack.pop();cout<<node->val<<" ";if (node->right!=NULL){NODE_stack.push(node->right);}if (node->left!=NULL){NODE_stack.push(node->left);}}}void breadFirstSearch(NODE *root){queue<NODE*> NODE_queue;NODE_queue.push(root);NODE *node;while(!NODE_queue.empty()){node=NODE_queue.front();NODE_queue.pop();cout<<node->val<<" ";if (node->left!=NULL){NODE_queue.push(node->left);}if (node->right!=NULL){NODE_queue.push(node->right);}}}int minDepth(NODE *root){if (root==NULL) return 0;if (root->left==NULL&&root->right==NULL) return 1;int leftDepth=minDepth(root->left);int rightDepth=minDepth(root->right);if (leftDepth==0){return rightDepth+1;}if (rightDepth==0){return leftDepth+1;}return min(leftDepth,rightDepth)+1;} int minDepth1(NODE *root) { if (root == NULL) return 0; int depth = 1; int currentLevel = 1; int nextLevel = 0; queue<NODE*> NODE_queue;NODE_queue.push(root); while (!NODE_queue.empty()) { NODE *node = NODE_queue.front(); NODE_queue.pop(); currentLevel--; if (node->left == NULL && node->right == NULL) { return depth; } if (node->left != NULL) { NODE_queue.push(node->left); nextLevel++; } if (node->right != NULL) { NODE_queue.push(node->right); nextLevel++; } if (currentLevel == 0) { if (nextLevel != 0) { depth++; } currentLevel = nextLevel; nextLevel = 0; } } return depth; } void printTree(ORDER_MODE method,NODE *root) { if (ORDER_MODE_PREV==method) { printTreeInPre(root); } if (ORDER_MODE_MID==method) { printTreeInMid(root); } if (ORDER_MODE_POST==method) { printTreeInPost(root); } };void printTreeInPre(NODE *root) {if (root){cout<<root->val<<" ";printTreeInPre(root->left);printTreeInPre(root->right);}}void printTreeInMid(NODE *root){if (root){printTreeInMid(root->left);cout<<root->val<<" ";printTreeInMid(root->right);}}void printTreeInPost(NODE *root) {if (root){printTreeInPost(root->left);printTreeInPost(root->right);cout<<root->val<<" ";}}void main(){NODE *root;root=TreeConstructor();cout<<"深度遍历:";depthFirstSearch(root);cout<<endl<<"广度遍历:";breadFirstSearch(root);cout<<endl<<"先序遍历:";printTree(ORDER_MODE_PREV,root);cout<<endl<<"中序遍历:";printTree(ORDER_MODE_MID,root);cout<<endl<<"后序遍历:";printTree(ORDER_MODE_POST,root);cout<<endl<<"最小路径:";int min1= minDepth(root);cout<<min1<<" ";int min2=minDepth1(root);cout<<min2;}运行一次结果为:
0 0
- 二元树的生成、遍历、以及最短最长路径查询
- 图的遍历、最小生成树、最短路径
- 二叉树的最长/最短路径
- 数据结构:图的存储、图的遍历、最小生成树、最短路径、拓扑排序
- [数据结构]第六次作业:图的建立、遍历、最小生成树、最短路径
- [数据结构]--图(图的遍历,最小生成树,最短路径算法)
- 图的基本操作(遍历,寻找最短路径,最小生成树)
- 数据结构:图——图的遍历、最小生成树、最短路径算法
- 最小生成树与最短路径的区别以及实现方法
- 最小生成树与最短路径的区别以及实现方法
- 简单迷宫建立,路径的遍历,最短路径的生成
- 图的周游,最短路径和最小生成树
- 最小生成树,最短路径的基本算法
- 图的最小生成树与最短路径
- 求最小生成树和最短路径的总结
- 图的最短路径及最小生成树 模板
- 最小生成树和最短路径的对比
- 最小生成树与最短路径的区别
- 系统键盘在ios7 ios8上不同效果
- 学习日记java 泛型和泛型类
- Linux-常用的20条命令
- 对C++中虚成员函数的理解
- Random Forest随机森林算法
- 二元树的生成、遍历、以及最短最长路径查询
- OSPF的几个关键参数
- 关于vs2010 中c++输出字符串到txt文件中的格式问题
- 2-20
- unity 3D初学习 之 Ninja
- 组合和分解,溯源(创新技法7)
- windows平台下vlc编译之五:vlc-0.9.6的编译
- 口袋中球的取出顺序问题,比赛名单问题
- Xcode自动注释插件