// tNode.h
 
#pragma once
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Property
{
public:
	
	Property(int value, int num)
	{
		iValue = value;
		iChildNum = num;
	}
	
	Property()
	{
		
		
	}
	
	~Property() {}
public:
	
	void init()
	{
		setValue();
		setChildNum();
	}
	
	
	int getValue()
	{
		return iValue;
	}
	
	int getChildNum()
	{
		return iChildNum;
	}
protected:
	void setValue()
	{
		
		cout << "该节点的值为:" << endl;
		cin >> iValue;
	}
	void setChildNum()
	{
		
		cout << "该节点的子节点个数为:" << endl;
		cin >> iChildNum;
	}
private:
	int iValue;
	int iChildNum;
	vector<int> viArrary;
};
struct TreeNode
{
	vector<TreeNode*> vTChildren;
	Property* pNature;
};
struct SumPara 
{
	vector<int> vecs;
};
void CreateTree(TreeNode*& tNode, SumPara sp)
{
	
	Property* prop=new Property();
	
	if (prop->getValue() == -1)
	{
		return;
	}
	
	tNode = new TreeNode;
	tNode->pNature = prop;
	prop->init();
	tNode->vTChildren.resize(tNode->pNature->getChildNum(), NULL);
	
	for (int i = 0; i < tNode->pNature->getChildNum(); ++i)
	{
		
		CreateTree(tNode->vTChildren[i]);
	}
}
void VisitTree(TreeNode* tNode)
{
	if (!tNode)
	{
		return;
	}
	cout << tNode->pNature->getValue() << endl;
	int iSize = tNode->vTChildren.size();
	for (int i = 0; i < iSize;++i)
	{
		VisitTree(tNode->vTChildren[i]);
	}
}
 
// main.cpp
 
#include "tNode.h"
void main()
{
	TreeNode* tn = NULL;
	cout << "创建一棵树" << endl;
	CreateTree(tn);
	cout << "访问结果为:" << endl;
	VisitTree(tn);
	cout << "路径搜索为:" << endl;
	vector<TreeNode*> vPath;
	SeekMinCost(tn, vPath);
	system("pause");
}
                
                
                
        
    
转载请注明原文地址:https://blackberry.8miu.com/read-1727.html