【问题标题】:Hierarchical outlining with five numeric levels — how to insert sibling or child rows and adjust existing records?具有五个数字级别的分层大纲 - 如何插入同级或子行并调整现有记录?
【发布时间】:2019-07-17 11:09:15
【问题描述】:

我有一个带有“点”(或大纲)层次结构的表: 字段为 L1 L2 L3 L4 L5(L = 级别)

例如:

1.0.0.0.0
1.1.0.0.0
1.1.1.0.0
1.1.2.0.0
1.2.0.0.0

如果我想在 1.1.1.0.0 插入同级,我应该得到一个 1.1.2.0.0 的新行 - 并且已经存在的 1.1.2.0.0 应该调整到 1.1.3.0.0 等.

如果我想插入一个子 1.1.1.0.0,我应该得到一个 1.1.1.1.0 的新行,不需要调整,因为该级别不存在同级。

我为此创建了程序代码 - 但它正在变成意大利面条 - 我想要一个带有处理这些插入和调整的类的 OOP 解决方案。

任何人都可以推荐伪代码来处理这两种类型的插入以及对现有“行”的必要调整吗?

任何帮助或建议将不胜感激!

【问题讨论】:

  • 每个对象都有自己的 L1 到 L5 字段。只需为兄弟姐妹添加一个链表实现,例如:Item1.1 具有对 Item1.2 的引用,并且对于父级也保留对第一个子级的引用。然后,在添加元素时,您可以遍历每个级别的while Lx not y 之类的结构,当达到最低级别时,像调整任何链表一样调整兄弟姐妹。
  • 谢谢 Mat - 但我不熟悉“链表实现”的概念 - 你能提供一个 C# 中的简单示例吗?
  • This 应该让您进行简单的了解。对于真正的实现,您可以简单地使用LinkedList<T> 类并调用现有方法AddAfter()
  • 再次感谢,Mat - 甚至不知道 LinkedList 存在!让我深入研究一下这如何适用于我的场景......
  • 你真的应该给出一些你的代码,关于这个问题有太多的事情可以假设。

标签: c# oop document hierarchy outlining


【解决方案1】:

我认为给你 cmets 的人并没有真正理解这个问题。您已经有一个表,因此使用 LinkedList 只会做一个表。您确实需要向该方法传递要插入的行和要插入的字段。仅添加一个值为 1.1.1.0.0 的新行并不能提供足够的信息来重新编号。

下面的代码我使用了一个DataTable,每列一个Field。为了简单地编写代码,我假设索引是整数。代码不是很复杂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Outlining outling = new Outlining();
            outling.Add(2,2);
            outling.Add(0, 2);
            outling.Add(5, 2);
        }
    }
    public class Outlining
    {
        public DataTable dt = null;

        public Outlining()
        {
            dt = new DataTable();
            dt.Columns.Add("L1", typeof(int));
            dt.Columns.Add("L2", typeof(int));
            dt.Columns.Add("L3", typeof(int));
            dt.Columns.Add("L4", typeof(int));
            dt.Columns.Add("L5", typeof(int));

            dt.Rows.Add(new object[] { 1, 0, 0, 0, 0 });
            dt.Rows.Add(new object[] { 1, 1, 0, 0, 0 });
            dt.Rows.Add(new object[] { 1, 1, 1, 0, 0 });
            dt.Rows.Add(new object[] { 1, 2, 0, 0, 0 });
        }
        public void Add(int at, int level)
        {
            DataRow newRow = dt.Rows.Add();
            if (at < dt.Rows.Count - 1)
            {
                //move row if not last row
                dt.Rows.Remove(newRow);
                dt.Rows.InsertAt(newRow, at);
            }
            newRow.BeginEdit();
            newRow.ItemArray = dt.Rows[at + 1].ItemArray.Select(x => (object)x).ToArray();
            newRow.EndEdit();

            Renumber(at, level);
        }
        public void Renumber(int rowInsertIndex, int level)
        {
            for (int row = rowInsertIndex; row < dt.Rows.Count - 1; row++)
            {
                Boolean match = true;
                //check if columns to left still match, if no we are done
                for (int i = 0; i < level - 1; i++)
                {
                    if (dt.Rows[i][level] != dt.Rows[i + 1][level])
                    {
                        match = false;
                        break;
                    }
                }
                if (!match) break;
                dt.Rows[row + 1][level] = ((int)(dt.Rows[row + 1][level])) + 1;
            }
        }

    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-08-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-13
    • 1970-01-01
    • 2020-01-15
    相关资源
    最近更新 更多