示例#1
0
void
PathSum<_Ty, _Compare, _TreeNode>::getPathsOfSumUp(PNode<_Ty> root,
                                                   std::vector<_Ty> prev,
                                                   _Ty prev_sum,
                                                   _Ty const &sum,
                                                   std::vector<std::vector<_Ty>> &res)
{
    if (root != nullptr)
    {
        auto &curr = prev;
        curr.push_back(root->value());

        auto &curr_sum = prev_sum;
        curr_sum += root->value();

        if (path_type_ == PathType::Whole)
        {
            if (root->left() == nullptr
                && root->right() == nullptr
                && compare_(curr_sum, sum))
                res.push_back(curr);
        }
        else        // path_type_ == PathType::Part
        if (compare_(curr_sum, sum))
            res.push_back(curr);


        getPathsOfSumUp(root->left(), curr, curr_sum, sum, res);
        getPathsOfSumUp(root->right(), curr, curr_sum, sum, res);
    }
}
示例#2
0
void main()
{
	std::cout<< "\n PNode<T> class demonstration";
	std::cout<< "\n ============================\n";
	//initialize
	PNode<std::string > root("root");
	PNode<std::string > child1("child1");
	PNode<std::string > child2("child2");
	PNode<std::string > grandchild11("grandchild11");
	PNode<std::string > grandchild21("grandchild21");
	PNode<std::string > grandchild22("grandchild22");
	PNode<std::string > grandgrandchild211("grandgrandchild211");
	PNode<std::string > child3("child3");
	
	//test adding
	child1.add(&grandchild11);
	grandchild21.add(&grandgrandchild211);
	child2.add(&grandchild21);
	child2.add(&grandchild22);
	root.add(&child1);
	root.add(&child2);
	root.add(&child3);
	WalkTree(&root);
	
	//test removing, mark operation
	std::cout<<"\n\n removing first child";
	root.remove(&child1);
	std::cout<<"\n unmarked all nodes";
	root.setMarkState(true);
	child1.setMarkState(true);
	grandchild11.setMarkState(true);
	child2.setMarkState(true);
	grandchild21.setMarkState(true);
	grandchild22.setMarkState(true);
	grandgrandchild211.setMarkState(true);
	child3.setMarkState(true);

	//show the tree
	WalkTree(&root);
	std::cout<<"\n\n";
	
	std::cout<<"===========================XMLValues Test==================================="<<std::endl;
	XMLValue xmlv(XMLValue::TAG, "MyTag");
	Attribute attr1("ATTR1","1");
	Attribute attr2("ATTR2","2");
	Attribute attr3("ATTR3","3");
	xmlv.AddAttr("ATTR1","1");
	xmlv.AddAttr("ATTR2","2");
	xmlv.AddAttr("ATTR3","3");

	//test == operator
	std::cout<<"Attribute Compare:\n"<<"attr1 == attr1: "<< (attr1 == attr1) <<"; attr2 == attr1: "<< (attr2 == attr1)<<std::endl;

	//test search and find attributes
	for(size_t i=0; i< xmlv.attributes().size();++i)
		std::cout<<xmlv.attributes().at(i).AttrName.c_str()<<"  "<<xmlv.attributes().at(i).AttrValue.c_str()  <<std::endl;
	std::cout<<"Find ATTR2's index: "<<xmlv.Find("ATTR2","2")<<std::endl;
	std::cout<<"Find ATTR4's index: "<<xmlv.Find("ATTR4","4")<<std::endl;
	//test removing attributes
	xmlv.RemoveAttr("ATTR2","2");
	std::cout<<"====================ATTR2 Removed==========================\n";
	for(size_t i=0; i< xmlv.attributes().size();++i)
		std::cout<<xmlv.attributes().at(i).AttrName.c_str()<<"  "<<xmlv.attributes().at(i).AttrValue.c_str()  <<std::endl;
	std::cout<<"=================Nodes with XMLValues======================"<<std::endl;
	PNode<XMLValue> xmlroot(xmlv);
	PNode<XMLValue> xmlcld1(XMLValue(XMLValue::TAG,"xmlChild1"));
	PNode<XMLValue> xmlcld2(XMLValue(XMLValue::TAG,"xmlChild2"));
	xmlroot.add(&xmlcld1);
	xmlroot.add(&xmlcld2);
	PNode<XMLValue> *pTemp;

	//test searching through the tree
	while (pTemp = xmlroot.nextUnmarkedChild())
	{
		std::cout<<"The child of root: "<<pTemp->value().TagName()<<std::endl; 
		pTemp->setMarkState(true);
	}

	std::cout<<"=================Children of Nodes======================"<<std::endl;
	for (size_t i = 0; i < xmlroot.getChildren().size(); i++)
		std::cout<<"a Child of ROOT: "<<xmlroot.getChildren()[i].TagName()<<std::endl;
	::system("pause");
}