Breadth-first traverse

来源:互联网 发布:mac平面设计用什么软件 编辑:程序博客网 时间:2024/06/06 12:22

/*related question ::how to traverse a tree in layer ~~~


when you code ; you have to consider three things ~~

what if there is 1

what if there is 0;

what if there is full;

these are special questions we need carefully consider ~~



FIVE QUESTIONS :

what problems do you meet ?

what more experience can you acquire from this code ?


1 = carefully about the == in assign value;

2 = the && judging 

for instance if(condition1 && condition2)

if condition is false; compiler will not consider condition 2; 

3 = every time when you use the parameter below; you should consider whether the py == 0 ; 

py->y = y;

4 = do not afraid of using logical judgement in code 

5 = feel free to use the int array to record the status 


6 = not consider too much;

   in the linked list ; for pointers such as p_cur = p_cur->pnextQueueNode;
  when we give the p_cur->data 
  we must consider about the p_cur = null;
  this maybe the return or break condition;
  in the traverse operation; what we should do is only cosider the last one which is null; 
  sometimes we consider too much things;


// write on the paper first ; then on the code;


/*while give the big boundary and then in the loop give the more specific details*/

*/

#include <iostream>

#include <fstream>

using namespace std;

#define MAXV 1000



// store whether the node is visited or not;

/*

isProcessed[i] = 

 0 : there is no node at i; 

-1 : there is one node at here; and not visited;

 1 : this node is visited

*/

int isProcessed[MAXV] = {0}; // is not visit is 1; otherwise 0;

typedef struct edgenode

{// this is for the 

int y;

int weight;

struct edgenode*  next;

};

typedef  edgenode * p_QueueNode;

//template <typename T>

class myQueue

{

struct QueueNode

{

p_QueueNode pcontentEdgeNode;

QueueNode * pnextQueueNode;

};

QueueNode * p_queue_head;

QueueNode * p_queue_rear;

int isfirst;

int queueLength;

public:

myQueue()

{

isfirst = 1;

queueLength = 0;

p_queue_head = p_queue_rear = NULL;

}

void traverse()

{

return;

QueueNode * p_cur = p_queue_head;

cout << "ququq travers" << endl;

if (!p_cur)

{ // if p_cur is empty;

cout << "queue empty" << endl;

return;

}

do

{

if (!p_cur)

{

cout << "queue empty" << endl;

return;

}

cout << " in the queue " << p_cur->pcontentEdgeNode->y << endl;

p_cur = p_cur->pnextQueueNode;

}while (p_cur != p_queue_rear && p_cur != NULL);  //is there more condition to consider ?

cout << endl;

}

void enqueue(edgenode * p)

{

++queueLength;

QueueNode * p_newQueueNode = new QueueNode;

p_newQueueNode->pcontentEdgeNode = p;





if (queueLength == 1 || isfirst == 1)

{

this->p_queue_head = p_newQueueNode;

this->p_queue_rear = p_newQueueNode;

p_newQueueNode->pnextQueueNode = NULL;



isfirst = 0;

}



this->p_queue_rear->pnextQueueNode = p_newQueueNode;

p_newQueueNode->pnextQueueNode = NULL;

this->p_queue_rear=p_newQueueNode;





}

edgenode * dequeue()

{

if (queueLength)

{

--queueLength;


edgenode * temp_result = this->p_queue_head->pcontentEdgeNode;// get the edgenode pointer;

QueueNode * temp_del = this->p_queue_head;  // record the old head pointer for delete;



this->p_queue_head = this->p_queue_head->pnextQueueNode;// let the head point to the next node;



delete temp_del;



return temp_result;

}

else

{

return 0;

}


}

int getLength()

{

return this->queueLength;

}

bool isEmpty()

{

return ( this->getLength() ==0);

}

};

typedef struct

{

edgenode * edges[MAXV + 1];

int degree[MAXV + 1];// nodes number in each row;

int nvertices;// num of vertices

int ndeges;//num of edges

bool directed;

}graph;



/// join y to the x;

void ini_graph(graph * & grph, bool directed)

{

grph = new graph;

for (int i = 0; i < MAXV + 1; ++i)

{

grph->degree[i] = 0;

grph->edges[i] = 0;

}



grph->directed = directed;

grph->ndeges = 0;

grph->nvertices = 0;



}

void insert(graph *g, int x, int y, bool directed)

{

// mark that there is one node here;

isProcessed[x] = -1;

isProcessed[y] = -1;



if (g->edges[x] == 0)

{ // we must give a node for this place;

edgenode * px = (edgenode *)malloc(sizeof(edgenode));

g->edges[x] = px;

px->next = NULL;

px->y = x;

}

edgenode * py = (edgenode *)malloc(sizeof(edgenode));



edgenode * tempp = g->edges[x];

edgenode * p1 = 0;

while (tempp)

{

p1 = tempp;

tempp = tempp->next;

}

//p1 store the last node in this entry;



p1->next = py;

py->next = NULL;

py->y = y; // 



++g->ndeges;   //the number of the graph plus one;



++g->nvertices; //the number of the vertex plus one;



if (!directed)// i

insert(g, y, x, false);

++g->degree[x];

}

void readFrmFile(bool directed, graph * pg)

{

int x, y;

ifstream outf;

outf.open("graph.txt");

if (!outf)

{

cout << "file is wrong " << endl;

}

while (outf >> x >> y)

{

insert(pg, x, y, 1);

}

}

int isAllProceesd(int a[])

{//1 all is visited; 0 not all is visited;

int sum = 0;

for (int i = 0; i < MAXV; ++i)

{

if (a[i] == -1)

return i;

}

return -11;

}

void iniMap(graph * & grph, bool directed)

{

ini_graph(grph, directed);

readFrmFile(directed, grph);

}

int isFind[MAXV] = { 0 }; // 0 is not find ; 1 is found;

void bfs(graph * pg)

{

myQueue mq;

//get the first node;

int ip = isAllProceesd(isProcessed);

edgenode * pfirst = pg->edges[ip];

//

mq.enqueue(pfirst);

edgenode * cur = pfirst;

isFind[pfirst->y] = 1;

while (!mq.isEmpty())

{

//dequeue

mq.traverse();

edgenode * pForVisit = mq.dequeue();

mq.traverse();



//visit

if (isProcessed[pForVisit->y] == -1){

cout << "pForVisit->y << pForVisit->weight" << pForVisit->y <<" --> "<< pForVisit->weight << endl;

isProcessed[pForVisit->y] = 1;

}

//put all its connection into the value;

edgenode * pforConn = pg->edges[pForVisit->y];// colomn transfer;

while (pforConn)

{

if (isFind[pforConn->y] == 0){

mq.enqueue(pforConn);

isFind[pforConn->y] = 1;

}

pforConn = pforConn->next;

}

mq.traverse();

}

}



// test stub=========================================================================


#include <iostream>

#include "graph.h"using namespace std;

int main(){/*graph * pg = 0;// ini_graph(pg, 1);// readFrmFile(1, pg);myQueue mq;edgenode textNode[100];for (int i = 0; i < 100; ++i){textNode[i].weight = i;textNode[i].next = NULL;textNode[i].y = i;mq.enqueue(&textNode[i]);}//for (int i = 0; i < 100; ++i)while (!mq.isEmpty()){cout << mq.dequeue()->weight << endl;}*/graph * pg;//void ini_graph(graph * & grph, bool directed);iniMap(pg, 1);bfs(pg);cout << "asdfasdf" << endl;system("pause");return 0;}

原创粉丝点击