二叉树广度优先遍历+二叉树深度优先遍历
2016-08-05 21:17
351 查看
http://blog.csdn.net/zhonghua18517/article/details/28270291
http://blog.csdn.net/zhonghua18517/article/details/28238261
二叉树深度优先遍历就是先序遍历,广度优先遍历就是层次遍历.
二叉树的深度优先遍历和先序遍历结果一样的。 思想是采用栈, 首先将根结点压入栈,如果栈不为空,而后出栈并输出当前结点中值,而后先把右子树压入栈,再把左子树压入栈,再判断栈是否为空,循环.....
步骤如下:
(1) 树的根结点入栈
(2)判断栈是否为空,不为空,则出栈,并输出出栈树结点的值
(3)出栈树结点的右子树入栈
(4)出栈树结点的左子树入栈
(5)循环回到(2)
创建如上树。详见前面二叉树的创建。
代码如下:
[cpp] view
plain copy
// Tree_depth_breadth.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef int type;
//定义二叉树结点
typedef struct node
{
type data;
struct node *lchild;
struct node *rchild;
}node,*bTree;
//建立元素为树节点的栈
typedef struct stack
{
bTree tree;
struct stack *next;
}stack,*pStack;
//初始化栈
void InitStack(pStack &s)
{
s=(pStack)malloc(sizeof(stack));
if(NULL==s)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
s->next=NULL;
}
//进栈
void Push(pStack s,bTree data)
{
pStack temp=(pStack)malloc(sizeof(stack));
if(NULL==temp)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
temp->tree=data;
temp->next=s->next;
s->next=temp;
}
//出栈
pStack Pop(pStack s)
{
pStack temp=s->next;
if(NULL==temp)
{
cout<<"栈为空!"<<endl;
exit(-1);
}
s->next=temp->next;
return temp;
}
//深度优先遍历 类似于先序遍历
void DepthFS(bTree T,pStack s)
{
Push(s,T);
while(NULL!=s->next)
{
pStack temp=Pop(s);
cout<<temp->tree->data<<" ";
if(NULL!=temp->tree->rchild)
Push(s,temp->tree->rchild);
if(NULL!=temp->tree->lchild)
Push(s,temp->tree->lchild);
}
}
//先序遍历
void PreOrder(bTree T)
{
if(NULL!=T)
{
cout<<T->data<<" ";
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
//出栈
//创建二叉树
void CreateTree(bTree &T)
{
type ch;
cin>>ch;
if(0==ch)
{
T=NULL;
}
else
{
T=(bTree)malloc(sizeof(node));
if(NULL==T)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
T->data=ch;
CreateTree(T->lchild); //创建左子树
CreateTree(T->rchild); //创建右子树
}
}
int _tmain(int argc, _TCHAR* argv[])
{
bTree T=NULL;
pStack s=NULL;
CreateTree(T);
InitStack(s);
PreOrder(T);
cout<<endl;
DepthFS(T,s);
return 0;
}
结果:
==================================================================================
二叉树广义优先遍历为层遍历(从左到右)。思想就是采用队列,步骤如下:
(1) 树的根结点入队
(2)判断队列是否为空,不为空,则出队,并输出出队树结点的值
(3)出队树结点的左子树进入队列
(4)出队树结点的右子树进入队列
(5)循环回到(2)
创建如下二叉树:
代码如下:
[cpp] view
plain copy
// BreadthFS.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
typedef int type;
using namespace std;
//树结点
typedef struct node
{
type data;
struct node *lchild;
struct node *rchild;
}node,*bTree;
//链队结点
typedef struct queue
{
bTree tree;
struct queue *next;
}queue,*pQueue;
//队列表定义
typedef struct LQueue
{
pQueue front;//队首指针
pQueue rear; //队尾指针
}LQueue,*LinkQueue;
//二叉树的创建
void CreateTree(bTree &T)
{
type ch;
cin>>ch;
if(0==ch)
{
T=NULL;
}
else
{
T=(bTree)malloc(sizeof(node));
if(NULL==T)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
T->data=ch;
CreateTree(T->lchild); //创建左子树
CreateTree(T->rchild); //创建右子树
}
}
//初始化队列
void InitQueue(LinkQueue Q)
{
Q->front=(pQueue)malloc(sizeof(queue));
if(NULL==Q->front)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
Q->rear=(pQueue)malloc(sizeof(queue));
if(NULL==Q->rear)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
Q->front->next=NULL;
Q->rear->next=NULL;
}
//树结点入队
void EnterQueue(LinkQueue &Q,bTree data)
{
pQueue temp=(pQueue)malloc(sizeof(queue));
if(NULL==temp)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
temp->tree=data;
temp->next=NULL;
if(NULL==Q->rear->next)
{
Q->front->next=temp;
Q->rear->next=temp;
}
else
{
Q->rear->next->next=temp;
Q->rear->next=temp;
}
}
//树节点出队
pQueue DeleteQueue(LinkQueue Q)
{
if(NULL==Q->rear->next)
{
cout<<"队列为空!"<<endl;
exit(-1);
}
pQueue temp=Q->front->next;
if(NULL==temp->next) //如果只有一个元素
{
Q->front->next=NULL;
Q->rear->next=NULL;
}
else
{
Q->front->next=temp->next;
}
return temp;
}
//广义优先遍历 Breadth First Search
void BreadthFS(LinkQueue Q,bTree T)
{
EnterQueue(Q,T);//根结点入队
while(NULL!=Q->rear->next) //队列不为空
{
pQueue temp=DeleteQueue(Q);
cout<<temp->tree->data<<" ";
if(NULL!=temp->tree->lchild)
{
EnterQueue(Q,temp->tree->lchild);
}
if(NULL!=temp->tree->rchild)
{
EnterQueue(Q,temp->tree->rchild);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
bTree T=NULL;
LQueue Q;
CreateTree(T);
InitQueue(&Q);
BreadthFS(&Q,T);
return 0;
}
运行结果:
顶0
踩
http://blog.csdn.net/zhonghua18517/article/details/28238261
二叉树深度优先遍历就是先序遍历,广度优先遍历就是层次遍历.
二叉树的深度优先遍历和先序遍历结果一样的。 思想是采用栈, 首先将根结点压入栈,如果栈不为空,而后出栈并输出当前结点中值,而后先把右子树压入栈,再把左子树压入栈,再判断栈是否为空,循环.....
步骤如下:
(1) 树的根结点入栈
(2)判断栈是否为空,不为空,则出栈,并输出出栈树结点的值
(3)出栈树结点的右子树入栈
(4)出栈树结点的左子树入栈
(5)循环回到(2)
创建如上树。详见前面二叉树的创建。
代码如下:
[cpp] view
plain copy
// Tree_depth_breadth.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
typedef int type;
//定义二叉树结点
typedef struct node
{
type data;
struct node *lchild;
struct node *rchild;
}node,*bTree;
//建立元素为树节点的栈
typedef struct stack
{
bTree tree;
struct stack *next;
}stack,*pStack;
//初始化栈
void InitStack(pStack &s)
{
s=(pStack)malloc(sizeof(stack));
if(NULL==s)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
s->next=NULL;
}
//进栈
void Push(pStack s,bTree data)
{
pStack temp=(pStack)malloc(sizeof(stack));
if(NULL==temp)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
temp->tree=data;
temp->next=s->next;
s->next=temp;
}
//出栈
pStack Pop(pStack s)
{
pStack temp=s->next;
if(NULL==temp)
{
cout<<"栈为空!"<<endl;
exit(-1);
}
s->next=temp->next;
return temp;
}
//深度优先遍历 类似于先序遍历
void DepthFS(bTree T,pStack s)
{
Push(s,T);
while(NULL!=s->next)
{
pStack temp=Pop(s);
cout<<temp->tree->data<<" ";
if(NULL!=temp->tree->rchild)
Push(s,temp->tree->rchild);
if(NULL!=temp->tree->lchild)
Push(s,temp->tree->lchild);
}
}
//先序遍历
void PreOrder(bTree T)
{
if(NULL!=T)
{
cout<<T->data<<" ";
PreOrder(T->lchild);
PreOrder(T->rchild);
}
}
//出栈
//创建二叉树
void CreateTree(bTree &T)
{
type ch;
cin>>ch;
if(0==ch)
{
T=NULL;
}
else
{
T=(bTree)malloc(sizeof(node));
if(NULL==T)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
T->data=ch;
CreateTree(T->lchild); //创建左子树
CreateTree(T->rchild); //创建右子树
}
}
int _tmain(int argc, _TCHAR* argv[])
{
bTree T=NULL;
pStack s=NULL;
CreateTree(T);
InitStack(s);
PreOrder(T);
cout<<endl;
DepthFS(T,s);
return 0;
}
结果:
==================================================================================
二叉树广义优先遍历为层遍历(从左到右)。思想就是采用队列,步骤如下:
(1) 树的根结点入队
(2)判断队列是否为空,不为空,则出队,并输出出队树结点的值
(3)出队树结点的左子树进入队列
(4)出队树结点的右子树进入队列
(5)循环回到(2)
创建如下二叉树:
代码如下:
[cpp] view
plain copy
// BreadthFS.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
typedef int type;
using namespace std;
//树结点
typedef struct node
{
type data;
struct node *lchild;
struct node *rchild;
}node,*bTree;
//链队结点
typedef struct queue
{
bTree tree;
struct queue *next;
}queue,*pQueue;
//队列表定义
typedef struct LQueue
{
pQueue front;//队首指针
pQueue rear; //队尾指针
}LQueue,*LinkQueue;
//二叉树的创建
void CreateTree(bTree &T)
{
type ch;
cin>>ch;
if(0==ch)
{
T=NULL;
}
else
{
T=(bTree)malloc(sizeof(node));
if(NULL==T)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
T->data=ch;
CreateTree(T->lchild); //创建左子树
CreateTree(T->rchild); //创建右子树
}
}
//初始化队列
void InitQueue(LinkQueue Q)
{
Q->front=(pQueue)malloc(sizeof(queue));
if(NULL==Q->front)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
Q->rear=(pQueue)malloc(sizeof(queue));
if(NULL==Q->rear)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
Q->front->next=NULL;
Q->rear->next=NULL;
}
//树结点入队
void EnterQueue(LinkQueue &Q,bTree data)
{
pQueue temp=(pQueue)malloc(sizeof(queue));
if(NULL==temp)
{
cout<<"申请内存失败!"<<endl;
exit(-1);
}
temp->tree=data;
temp->next=NULL;
if(NULL==Q->rear->next)
{
Q->front->next=temp;
Q->rear->next=temp;
}
else
{
Q->rear->next->next=temp;
Q->rear->next=temp;
}
}
//树节点出队
pQueue DeleteQueue(LinkQueue Q)
{
if(NULL==Q->rear->next)
{
cout<<"队列为空!"<<endl;
exit(-1);
}
pQueue temp=Q->front->next;
if(NULL==temp->next) //如果只有一个元素
{
Q->front->next=NULL;
Q->rear->next=NULL;
}
else
{
Q->front->next=temp->next;
}
return temp;
}
//广义优先遍历 Breadth First Search
void BreadthFS(LinkQueue Q,bTree T)
{
EnterQueue(Q,T);//根结点入队
while(NULL!=Q->rear->next) //队列不为空
{
pQueue temp=DeleteQueue(Q);
cout<<temp->tree->data<<" ";
if(NULL!=temp->tree->lchild)
{
EnterQueue(Q,temp->tree->lchild);
}
if(NULL!=temp->tree->rchild)
{
EnterQueue(Q,temp->tree->rchild);
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
bTree T=NULL;
LQueue Q;
CreateTree(T);
InitQueue(&Q);
BreadthFS(&Q,T);
return 0;
}
运行结果:
顶0
踩
相关文章推荐
- .sam 文件介绍
- 数据结构实验之串一:KMP简单应用
- hdu 3333 求区间中不同的数的和
- HashSet源码解析&Map迭代器 给jdk写注释系列之jdk1.6容器(6)
- mysql外键实战
- [置顶] android 自定义可跳转的log
- HDU 3683--模拟+搜索
- POJ 2367Genealogical tree
- 强迫症的LaTeX学习笔记
- 动态图-二叉搜索树
- 异常值处理
- 一次图文并茂的渗透完整测试
- Memcache总结
- Linux Socket编程(网络)
- POJ1088(dp)
- 售货员的难题(codevs 2596)
- Java的数组集合概括
- uva 1213 Sum of Different Primes, dp
- c++ 文本处理基础函数
- 服务器防渗透--信息收集