| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 980 人关注过本帖
标题:请教大家一个问题
只看楼主 加入收藏
yuxudaoren
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-3-24
收藏
 问题点数:0 回复次数:12 
请教大家一个问题

The following represents a sample tree saved in a file.

The children of each node has an extra '\t'

at the beginning and the children of each

node are directly under it.

The tree can be very deep and each node can have 0 to many child nodes.

===========================

Root

V:\

FolderX

FolderA1

FolderD2

FolderB

FolderW1

FolderB2

D:\

FolderM

FolderQ1

FolderC2

============================

Please implement a program to load the whole tree into the memory from the tree file. The program should not only load the tree file above, but also load other similar tree files.

The tree data structure should be similar as.

Class Tree

{

string name;

int level; // the depth of the node from the root

Array children; // each object in the Array is the type Tree.

}

And then sort the child nodes of each node in the ascending order using IComparer interface.

Then print the tree out in the same tree file format.

上面要求的将树载入内存可否看做是树的存储呢 这样定义一个结点类用左孩子——右兄弟的方法可以么? 谢谢~~~

搜索更多相关主题的帖子: following beginning represents 
2006-03-24 01:30
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 

差不多是用个自己作的树,递归一下就行了,不过节点类还是Tree类型,不只左右,应该可以无限多。

"level"这变量可要可不要,要是树不太深最好别要,因为如果需要作很多来回移动节点或插入的操作的话,改每个节点的level简直类死了。如果用VC里的Tree当然省了不少事,如果自己写每个孩子还要有一个指回家长的指针。

Class TreeNode
{
public:
int getLevel();
private:
string m_sName;
TreeNode *m_pParent; // NULL for root
vector<TreeNode> m_treeChildren;
};


http://myajax95./
2006-03-24 04:18
yuxudaoren
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-3-24
收藏
得分:0 
回复everajax
因为这个是树的存储 所以应该不用考虑树结点的插入和移动吧 我对vector这个不熟悉 能具体给出题中要求的树的栽入的实现部分么? 谢谢
2006-03-24 10:18
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 
要C++写得还是用VC写的?

http://myajax95./
2006-03-24 12:59
yuxudaoren
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-3-24
收藏
得分:0 

用c++就可以了 谢谢这位大侠

2006-03-24 18:18
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 

没作太仔细的error handling,如果tab乱了得话应该管一管。 code中用了vector,看着不顺眼的话就换成数组8。没调的太仔细,用什么错告诉我。

程序代码:

#include <iostream>
#include <sstream>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;


class CTreeNode
{
public:
CTreeNode();
CTreeNode(string sName, CTreeNode *ptreeParent);
string ParseChild(ifstream &ifs, int intLevel);
CTreeNode *GetParent() {return m_ptreeParent;}
string GetName() const {return m_sName;}
int GetChildNumber() const {return (int)m_vecChildren.size();}
int GetLevel();
string ToString();
private:
string m_sName;
CTreeNode *m_ptreeParent;
vector<CTreeNode> m_vecChildren;
};

CTreeNode::CTreeNode() : m_ptreeParent(NULL)
{
}

CTreeNode::CTreeNode(string sName, CTreeNode *ptreeParent)
: m_sName(sName)
, m_ptreeParent(ptreeParent)

{
}


int CTreeNode::GetLevel()
{
int intLevel = 0;
CTreeNode *pParent = m_ptreeParent;

while (pParent)
{
pParent = pParent->GetParent();
intLevel++;
}
return intLevel;
}

string CTreeNode::ParseChild(ifstream &ifs, int intLevel)
{
string sLine, sCopy, sTabs = \"\t\";
int intStart, intLevelCount;

for (intLevelCount = 0; intLevelCount < intLevel; intLevelCount++)
sTabs += '\t';

// error in stream test
while(true)
{
if (sLine.empty())
getline(ifs,sLine);
if (ifs.eof())
return \"\";
if ((intStart = sLine.find(sTabs)) < 0)
return sLine;

sCopy = sLine;
sCopy.erase(0, intStart+intLevel+1);
if (sCopy.at(0) == '\t') // TODO: exception handling here
return sLine;
m_vecChildren.push_back(CTreeNode(sCopy, this));
sLine = m_vecChildren[m_vecChildren.size()-1].ParseChild(ifs, intLevel+1);
}
}

string CTreeNode::ToString()
{
stringstream ssOutput;

ssOutput << GetName() << \", Child Number:\" << GetChildNumber() << endl;
for (int intChildCount = 0; intChildCount < m_vecChildren.size(); intChildCount++)
ssOutput << m_vecChildren[intChildCount].ToString();

return ssOutput.str();
}

ostream& operator << (ostream &o, CTreeNode& node)
{
return o << node.ToString();
}

int main(int argc, char* argv[])
{
string sLine;
CTreeNode *pNodeRoot;

ifstream ifs(\"lsttest.txt\");

if (!ifs)
{
cout << \"Cannot open file\" << endl;
return 0;
}

getline(ifs, sLine);
if (ifs.eof() || sLine.empty())
return 0;

// initialize parent.
pNodeRoot = new CTreeNode(sLine, NULL);
pNodeRoot->ParseChild(ifs, 0);

cout << *pNodeRoot;

delete pNodeRoot;

return 0;
}




http://myajax95./
2006-03-25 15:31
yuxudaoren
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-3-24
收藏
得分:0 
回复:(everajax)没作太仔细的error handling,如果...
刚刚看到大侠给写的代码,调试了一下,只显示press any key to continue    改了改还是不行 请帮忙看看  谢谢!~~
2006-03-25 23:04
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 
只认识TAB(\t)用空格是不行的,试试这个附件。
DLpL6QkW.txt (204 Bytes) 请教大家一个问题



http://myajax95./
2006-03-26 03:23
yuxudaoren
Rank: 1
等 级:新手上路
帖 子:63
专家分:0
注 册:2006-3-24
收藏
得分:0 
回复:(everajax)只认识TAB(\t)用空格是不行的,试试...
谢谢哥哥的回复  附件只是一个树型文件啊  如何操作  是把这个文件添加到代码里么?
2006-03-26 10:03
myajax95
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:30
帖 子:2978
专家分:0
注 册:2006-3-5
收藏
得分:0 

附件是所存储的树的格式。把它更名成"listtest.txt",就像程序里写的那样,放在正确的路径下。
ifstream ifs("lsttest.txt");
如果你们老师用别的符号表示TAB, 就把‘\t’换成该符号,例如' '。


http://myajax95./
2006-03-26 14:24
快速回复:请教大家一个问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013926 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved