数据结构中的有两个比较重要的算法。深度优先搜索和广度优先搜索。
二叉树中的深度搜索就是对一个分支进行遍历,而广搜就是一层一层的搜索。
下面通过代码进行讲解:
#include <iostream>
#include <queue>
using namespace std;
struct Node
{
Node *Left;
int Value;
Node *Right;
Node(int value=0, Node *left=NULL, Node *right=NULL):Value(value) , Left(left), Right(right) {};
};
Node *node[11];
void creat();
void DFS(Node*);
void BFS(Node*);
int main()
{
creat();
Node* Root = node[10];
cout<<"深搜结果为:"<<endl;
DFS(Root);
cout<<endl<<"广搜结果为:"<<endl;
BFS(Root);
return 0;
}
void creat()
{
node[1]=new Node(1