LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1130|回复: 10

请教大家一个问题 谢谢

[复制链接]
发表于 2006-3-24 00:58:40 | 显示全部楼层 |阅读模式
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.
上面要求的将树载入内存可否看做是树的存储呢 这样定义一个结点类用左孩子——右兄弟的方法可以么?  谢谢~~~
发表于 2006-3-24 01:44:04 | 显示全部楼层
tree类不是用Array children[]作为成员了么,实例化后的对象层次和从属关系都通过这个Array表示出来了。

读取时只要按行读取即可。增设一个栈保存level信息:栈元素保存字节点距父结点的“行偏移量”,栈深度表示level层数;增设一变量t_count保存当前行中的'\t'个数。

每读一行,检查行首:
t_count 每增加1, 则新压栈一个 ”行偏移量”;
t_count 每减少1,则退栈一个 “行偏移量”。
t_count 不变,则栈顶 “行偏移量加1”。

这样,根据栈的信息,所有对象都可以加入到正确位置。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-24 01:53:33 | 显示全部楼层
这位大哥的意思是说array中的变量是name的孩子,这个孩子又可以看做另一个树类中的name?
回复 支持 反对

使用道具 举报

发表于 2006-3-24 02:01:26 | 显示全部楼层
什么是另一个树类? 给定的只有一个Tree类而已。通过Tree类实例化出来的对象保存在数组中。

算法实现我上面简单说了一下。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-24 02:05:18 | 显示全部楼层
谢谢这位大哥 但是增加一个\t的话 不一定走过一行啊  因为同一行上有很多子结点啊
回复 支持 反对

使用道具 举报

发表于 2006-3-24 02:12:45 | 显示全部楼层
一行只有一个entry吧,瞧题目意思。
t_count是保存一行中行首的'\t'个数。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-24 02:19:02 | 显示全部楼层
呵呵  是我理解的有错误了  那哥哥说的T-count不变的时候栈顶的偏移量是代表结点么?
ps:能把哥哥的QQ告诉我么  好进一步讨教  不胜感激!~~
回复 支持 反对

使用道具 举报

发表于 2006-3-24 07:45:54 | 显示全部楼层
代表当前行entry在父结点的对象Array中的排行。
那父结点又在哪? 从栈底依次查到栈顶减1,就知道父结点位置了。
回复 支持 反对

使用道具 举报

 楼主| 发表于 2006-3-24 10:47:31 | 显示全部楼层
谢谢哥哥的回复 看了哥哥修改的帖子:t_count 不变,则栈顶 “行偏移量加1”。这样的话不是等于又读下一行了么 脑袋突然转不过来了 有点迷糊 哥哥能具体给一下实现部分么 谢谢~~~
回复 支持 反对

使用道具 举报

发表于 2006-3-24 12:15:51 | 显示全部楼层
算法伪代码:

  1. 0、增设全局量:
  2.      Stack S;  
  3.      int t_count;  
  4.      int level;
  5.      int line_offset;
  6.      Tree * father_node;

  7. 1、初始化:
  8.     t_count=0;   // root结点所在行首没有一个'\t'
  9.     level=0;     // root结点level
  10.     line_offset=1;           // root所在行偏移量
  11.     S=push(S , line_offset); //把root结点所在行偏移量压栈
  12.     Tree root_node("root",level, Tree *children[MAX]={NULL}); // 初始化root结点。
  13.     father_node= &root_node;
  14. 2、重复执行以下步骤,直到读完所有行。
  15.            读取一行,置 t 为 当前行首 '\t' 个数;
  16.            if (t>t_count){
  17.                 line_offset=1;
  18.                 push(S,line_offset);
  19.                 level++;
  20.                 t_count=t;
  21.                 Tree *new_entry=new Tree(current_entry_name, level, Tree *children[MAX]={NULL});
  22.                 father_node=get_father_node(S);  //从栈中获取father_node指针。
  23.                 father_node->children[line_offset]=new_entry;
  24.            } else if(t==t_count) {
  25.                pop(S,line_offset);
  26.                line_offset++;
  27.                push(S,line_offset);
  28.                Tree *new_entry=new Tree(current_entry_name, level, Tree *children[MAX]={NULL});
  29.                father_node->children[line_offset]=new_entry;
  30.            } else if(t<t_count) {
  31.                n=t_count-t;
  32.                进行n+1次pop(S,line_offset);
  33.                father_node=get_father_node(S);
  34.                Tree *new_entry=new Tree(current_entry_name, level, Tree *children[MAX]={NULL});
  35.                father_node->children[line_offset++]=new_entry;
  36.                push(S,line_offset);
  37.             }
复制代码


其中get_father_node(S) 函数表示从栈S中找出当前结点的父结点指针。 方法很简单,从根结点root_node开始,递归下降,下降的次数即S的深度(即S包含的元素个数);栈中的元素指明了每次从children[]偏移的位置。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表