最近学习c++,越看越觉得以前所学只是皮毛.这几天正好有空闲就写点小算法玩玩.
多叉树的层次遍历 这个在网上有完整程序的好像不多.这次我就把写的贴出来,
有兴趣的朋友一起来研究下.
TreeNode.h 文件
#ifndef __TREENODE_
#define __TREENODE_
#include "StdAfx.h"
#include
#include
#include
#include
using namespace std;
class TreeNode
{
private:
long selfID;
string nodeName;
list *p_childList;
public:
TreeNode();
~TreeNode();
/*向当前节点中插入一个子节点*/
void insertChildNode(TreeNode *treeNode);
/*遍历树,层次遍历*/
void LevelTraverse() ;
//判断某个节点是否为叶子节点
bool isLeaf();
//返回当前节点的孩子集合
list * getChildList();
long getSelfId();
void setSelfId(long selfID);
string getNodeName();
void setNodeName(string &nodeName);
};
//返回当前节点的孩子集合
inline list * TreeNode::getChildList()
{
return p_childList;
}
inline long TreeNode::getSelfId()
{
return selfID;
}
inline void TreeNode::setSelfId(long selfID)
{
this->selfID = selfID;
}
inline string TreeNode::getNodeName()
{
return nodeName;
}
inline void TreeNode::setNodeName(string &nodeName)
{
this->nodeName = nodeName;
}
#endif
TreeNode.cpp 文件
#include "stdafx.h"
#include "TreeNode.h"
TreeNode::TreeNode()
{
selfID = 0 ;
nodeName = "";
p_childList = NULL;
}
TreeNode::~TreeNode()
{
delete p_childList;
}