表的数据结构?

时间:2012-06-05 11:02:15

标签: data-structures tabular

我需要创建一个包含许多行和两列以上的表。建议一个良好和快速的数据结构。我不会更新和删除该表中的一些条目。我将只使用查找功能 例如,我有一张表:

 
   | column 1 | column 2 | column 3|
a  | asd      | awd      | asfc    |
b  | asgf     | aasf     | asgfc   |

我有:

String a = "column 1"
String b = "b"
String c = find(a,b);

最后,c中的值应为asgf

1 个答案:

答案 0 :(得分:0)

您必须使用基于哈希表的关联数组(所谓的词典)。查找的平均时间复杂度 - 最坏情况下为O(1 + n / k)和O(n)。您必须将表组织为列的字典(列名为键)。并且Column必须是值的字典(以行名作为键)

更多信息:

http://en.wikipedia.org/wiki/Dictionary_(data_structurehttp://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") );
        }
    }
}