一张表格的数据结构?

3
我需要创建一个行数很多且列数大于两列的表格。请建议一个好的、快速的数据结构。我不会更新和删除该表格中的某些条目。我只会使用查找函数。
例如,我有一个表格:
 
   | 列1     | 列2     | 列3    |
a  | asd    | awd    | asfc  |
b  | asgf   | aasf   | asgfc |
我有:
String a = "列1"
String b = "b"
String c = find(a,b);
最终c的值应该是asgf

1
这完全取决于您要存储的数据。 您需要提供更多关于您想要做什么的信息。 - Config
查找条件是什么? - phoxis
我将在列中查找特定的条目。就像数据库一样。 - Devashish Dixit
我将拥有一行的“id”和“列名”,需要在该表中获取此特定条目的值。 - Devashish Dixit
多个哈希表?哈希表:id-column1,id-column2,id-column3。平均查找时间为O(1+n/k),最坏情况下为O(n)。 en.wikipedia.org/wiki/Hash_table - Viktor Lova
1个回答

0

你必须使用基于哈希表的关联数组(也称为字典)。查找的平均时间复杂度为O(1 + n/k),最坏情况下为O(n)。你必须将表组织成列的字典(以列名作为键),而列必须是值的字典(以行名作为键)。

更多信息:

http://en.wikipedia.org/wiki/Dictionary_(data_structure) http://en.wikipedia.org/wiki/Hash_table

C#示例:

using System;
using System.Collections;
using System.Collections.Generic;

namespace Test {
    public class Test
    {
        class Table {
            class Column {
                public Dictionary<string, string> cells;

                public Column() {
                    cells = new Dictionary<string, string>();
                }

                public string Find(string rowName) {
                    string resultValue;
                    if (cells.TryGetValue(rowName, out resultValue)) 
                        return resultValue;
                    else
                        throw new Exception("oops, no such cell");
                }
            }

            Dictionary<string, Column> columns;
            List<string> rowNames;

            public Table() {
                columns = new Dictionary<string, Column>();
                rowNames = new List<string>();
            }

            public void AddColumn(string columnName, params string[] values) {
                Column column = new Column();
                columns.Add(columnName, column);

                // fill new cells
                int counter = 0;
                foreach (string rowName in rowNames) {
                    if (counter < values.Length)
                        column.cells.Add(rowName, values[counter]);
                    else
                        column.cells.Add(rowName, "");
                    counter++;
                }
            }

            public void AddRow(string rowName, params string[] values) {
                rowNames.Add(rowName);

                // fill new cells
                int counter = 0;
                foreach (KeyValuePair<string, Column> columnPair in columns) {
                    Column column = columnPair.Value;
                    if (counter < values.Length)
                        column.cells.Add(rowName, values[counter]);
                    else
                        column.cells.Add(rowName, "");
                    counter++;
                }
            }

            public string Find(string columnName, string rowName) {
                Column resultColumn;
                if (columns.TryGetValue(columnName, out resultColumn))
                    return resultColumn.Find(rowName);
                else
                    throw new Exception("oops, no such cell");
            }
        }

        public static void Main()
        {
            Table table = new Table();
            table.AddRow("a");
            table.AddRow("b");
            table.AddColumn("column 1", "asd", "asgf");
            table.AddColumn("column 2", "awd", "aasf");
            table.AddColumn("column 3", "asfc", "asgfc");
            Console.WriteLine(table.Find("column 1", "b") );
        }
    }
}

P.S:示例不能防止添加重复的行和列。注:上述翻译仅供参考,可能因语境不同而不准确。 - Viktor Lova
当您为每个单元格和行分配非常大的对象时,似乎效率不高。此外,您在各处复制列名(作为键)。我宁愿创建一个带有内部列名数组和记录列表的类。而且,可以轻松地将列表替换为向量或链表以实现更快的查找。 - Paul-Sebastian Manole
嘿,FYI大O符号解释了“渐近上界”,因此是最坏情况。对于平均情况,您可以使用大theta来描述它。 - Matthias Michael Engh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接