Esempio n. 1
0
void
bfs(TreeNode* root)
{
	if (!root)
	{
		return;
	}

	typedef std::queue<TreeNode*> NodeQueue;
	NodeQueue nqueue;

	nqueue.push(root);
	while (!nqueue.empty())
	{
		NodeQueue::size_type qsize = nqueue.size();
		for (NodeQueue::size_type i = 0; i < qsize; ++i)
		{
			TreeNode* cnode = nqueue.front();
			nqueue.pop();

			std::cout<<cnode->val<<" ";
			if (cnode->left)
			{
				//std::cout<<"left child: "<<cnode->left->val<<"\n";
				nqueue.push(cnode->left);
			}
			if (cnode->right)
			{
				//std::cout<<"right child: "<<cnode->right->val<<"\n";
				nqueue.push(cnode->right);
			}
		}
		std::cout<<"\n";
	}
}
Esempio n. 2
0
 void Callback::constructNonZeroComponents( Graph          const & g
                                          , GraphVariables const & vars
                                          , IloNumArray            x_values
                                          , NodeSetVector        & nonZeroNodesComponents
                                          ) const {
   using NodeQueue = std::queue< Graph::Node >;
   NodeQueue queue;

   // -3 queued
   // -2 unvisited
   // -1 visited and zero
   // >= 0 visited and non-zero
   auto compMap = g.createNodeMap< int >( -2 );

   int compIdx = 0;
   for ( Graph::Node n : g.nodes() ) {
     if ( compMap[n] == -2 && intIsNonZero( x_values[vars.nodeToIndex[n]] ) ) {
       queue.push( n );
       compMap[n] = -3;
       nonZeroNodesComponents.emplace_back();

       // perform bfs
       while ( !queue.empty() ) {
         Graph::Node n = queue.front();
         assert( compMap[n] == -3 );

         queue.pop();
         compMap[n] = compIdx;

         nonZeroNodesComponents.back().insert( n );

         for ( Graph::Edge e : g.incEdges( n ) ) {
           Graph::Node nn = g.oppositeNode( n, e );

           if ( compMap[nn] == -2 ) {
             if ( intIsNonZero( x_values[vars.nodeToIndex[nn]] ) ) {
               queue.push( nn );
               compMap[nn] = -3;
             } else {
               compMap[nn] = -1;
             }
           }
         }
       }

       ++compIdx;
     }
   }
 }
Esempio n. 3
0
int main()
{
        try
        {
        NodeQueue<int> Queue;
        Queue.enqueue(10);
        Queue.enqueue(20);
        Queue.enqueue(30);
        std::cout<<"Top is: "<<Queue.front()<<'\n';
        Queue.dequeue();
        Queue.print();
        }
        catch(const std::string& e)
        {
             std::cout<<e<<'\n';
        }
}