コード例 #1
0
 vector<int> postorderTraversal(TreeNode *root) {
     vector<int> answer;
     stack<stackNode> emptyStack;
     fullStack.swap(emptyStack);
     if (root == NULL)
     {
         return answer;
     }
     fullStack.push(stackNode(root, 0));
     while(!fullStack.empty())
     {
         stackNode currNode = fullStack.top();
         fullStack.pop();
         if (currNode.state == 0)
         {
             fullStack.push(stackNode(currNode.node, 1));
             if (currNode.node->right != NULL)
             {
                 fullStack.push(stackNode(currNode.node->right, 0));
             }
             if (currNode.node->left != NULL)
             {
                 fullStack.push(stackNode(currNode.node->left, 0));
             }
         }
         else
         {
             answer.push_back(currNode.node->val);
         }
     }
     return answer;
 }
コード例 #2
0
ファイル: Node.cpp プロジェクト: sdasgup3/parallel-sudoku
// non-recursive algorithm with adaptive grain size control
void Node::sequentialColoringHelper(bool& wait, bool& solutionFound, std::vector<vertex>& result)
{
  while(!stackForSequential.empty())
  {
    sequentialCurr = CkTimer();

    // Check if the sequential algorithm has run for 'timeout' seconds. If yes,
    // then return and call rerun() entry method. That would execute the
    // remaining stack.
    if(sequentialCurr - sequentialStart > timeout)
    {
#ifdef DEBUG
      CkPrintf("Timeout in sequential coloring. Stack size=%d\n", stackForSequential.size());
#endif
      thisProxy.rerun();
      wait = true;
      return;
    }

    stackNode curr_node_ = stackForSequential.top().first;
    std::stack<int> removedVertices = stackForSequential.top().second;
    stackForSequential.pop();  // remove from stack

    // vertex removal step
    curr_node_.uncolored_num_ -= curr_node_.vertexRemoval(removedVertices);

    // coloring found
    if(curr_node_.uncolored_num_==0)
    {
      solutionFound = true;
      curr_node_.mergeRemovedVerticesBack(removedVertices);
      result = curr_node_.node_state_;
      return;
    }

    int vIndex = curr_node_.getNextConstrainedVertex();
    CkAssert(vIndex!=-1);

    // Value Ordering
    // temporary stack to invert the priority queue ordering
    std::stack<stackNode> tmp;
    pq_type priorityColors = curr_node_.getValueOrderingOfColors(vIndex);
    while(!priorityColors.empty()){
      std::pair<int,int> p = priorityColors.top();
      priorityColors.pop();
      std::vector<vertex> new_state = curr_node_.node_state_;
      CkAssert(p.first < chromaticNum_);
      int verticesColored = curr_node_.updateState(new_state, vIndex, p.first, true);
      CkAssert(verticesColored >= 1);
      tmp.emplace(stackNode(new_state, curr_node_.uncolored_num_ - verticesColored));
    }

    while(!tmp.empty())
    {
      stackForSequential.push(std::make_pair(tmp.top(), removedVertices));
      tmp.pop();
    }
  }

}
コード例 #3
0
	QuadGraph* BuildTree(int i, int j, vector<vector<char>>& board){
		QuadGraph* pRoot = new QuadGraph(pair<int, int>(i, j));
		vector<QuadGraph*> stackNode(1, pRoot);
		board[i][j] = '1'; // mark gray;

		while (!stackNode.empty())
		{
			QuadGraph* pNode = stackNode.back();
			stackNode.pop_back();
			board[pNode->m_ijCood.first][pNode->m_ijCood.second] = '1'; // mark gray;

			for (int k = 0; k < 4; ++k){
				int m = pNode->m_ijCood.first;
				int n = pNode->m_ijCood.second;
				switch (k)
				{
				case 0:
					m = m - 1;
					n = n;
					break;
				case 1:
					m = m;
					n = n - 1;
					break;
				case 2:
					m = m;
					n = n + 1;
					break;
				case 3:
					m = m + 1;
					n = n;
					break;
				default:
					printf("OOPS!!! sth wrong here.\n");
					break;
				}

				if (m < 0 || m >= board.size() || n < 0 || n >= board[0].size()){
					pNode->m_bFlip = false;
					continue;
				}

				if (board[m][n] != 'O'){
					continue;
				}

				QuadGraph* pNext = new QuadGraph(pair<int, int>(m, n));
				pNode->AddSubNode(pNext);
				stackNode.push_back(pNext);
			}

			board[pNode->m_ijCood.first][pNode->m_ijCood.second] = '2'; // mark black;
		}

		//pRoot->m_bFlip = bFlip;
		return pRoot;
	}
コード例 #4
0
	void traversalGraphSet(QuadGraph& g, char ch, vector<vector<char> > & board){
		vector<QuadGraph*> stackNode(1, &g);
		while (!stackNode.empty()){
			QuadGraph* pNode = stackNode.back();
			stackNode.pop_back();
			board[pNode->m_ijCood.first][pNode->m_ijCood.second] = ch;
			stackNode.insert(stackNode.end(), pNode->m_subNodes.begin(), pNode->m_subNodes.end());
		}
	}
コード例 #5
0
ファイル: Node.cpp プロジェクト: sdasgup3/parallel-sudoku
bool Node::sequentialColoring(bool& wait)
{
  sequentialStart = CkTimer();
  // stackForSequential = Stack which holds stackNodes objects. Each stackNode
  // object represents a node of the state space search. stackNode class is
  // similar to the node class. Second item in the pair is the stack of vertices
  // that were removed (in order) leading upto this point in the state space
  // search
  stackForSequential.emplace(std::make_pair(stackNode(node_state_, uncolored_num_), std::stack<int>()));
  bool solutionFound = false; 

  // if a solution is found in sequentialColoringHelper function,
  // the vertices in node_state_ are updated (colored)
  sequentialColoringHelper(wait, solutionFound, node_state_);
  return solutionFound;
}
コード例 #6
0
// Push an item to stack
void push(struct Stack *s, struct binaryTreeNode *new_data)
{
    // allocate node
    struct stackNode *new_node = stackNode(new_data);

    if (new_node == NULL)
    {
         printf("Stack overflow\n");
         exit(0);
    }

    // put in the data
    new_node->data = new_data;

    //link the old list off the new node
    new_node->next = s->top;

    //move the head to point to the new node
    s->top = new_node;
}
コード例 #7
0
	bool getGraphStatus(QuadGraph& g){
#if 0
		if (!g.m_bFlip)
			return false;
		else{
			bool ret = true;
			for (int i = 0; ret && i < g.m_subNodes.size(); ++i){
				ret &= getGraphStatus(*g.m_subNodes[i]);
			}
			return ret;
		}
#endif
		vector<QuadGraph*> stackNode(1, &g);
		while (!stackNode.empty()){
			QuadGraph* pNode = stackNode.back();
			stackNode.pop_back();

			if (!pNode->m_bFlip)
				return false;

			stackNode.insert(stackNode.end(), pNode->m_subNodes.begin(), pNode->m_subNodes.end());
		}
		return true;
	}