码迷,mamicode.com
首页 > 其他好文 > 详细

广度优先搜索

时间:2014-05-26 14:49:14      阅读:247      评论:0      收藏:0      [点我收藏+]

标签:style   c   class   blog   code   java   

bubuko.com,布布扣
#include<iostream>
#include<vector>
#include<stack>
#include<string>
#include<queue>
#include<algorithm>
#include<numeric>
using namespace std;

class node{
public:
    int val;
    node* left;
    node* right;
    node():val(0),left(NULL),right(NULL){}
};

node* createTree()
{
    node* head = new node[14];
    for(int i = 0;i<10;i++)
    {
        head[i].val = i;
        if(2*i+1 < 10)
        head[i].left = head + 2*i + 1;
        if(2*i+2 < 10)
        head[i].right = head + 2*i + 2;
    }
    return head;
}

void GFS(node* root)
{
  node* r = root;
  queue<node*> q;
  q.push(root);
  while(!q.empty())
  {
      node* temp = q.front();
      cout<<temp->val<<" ";
      q.pop();
      if(temp->left != NULL)
      q.push(temp->left);
      if(temp->right != NULL)
      q.push(temp->right);
  }
}

int main()
{
    node* t = createTree();
    GFS(t); cout<<endl;
}
bubuko.com,布布扣

 

广度优先搜索,布布扣,bubuko.com

广度优先搜索

标签:style   c   class   blog   code   java   

原文地址:http://www.cnblogs.com/berkeleysong/p/3746421.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!