【问题标题】:Implement phone directory using two tries使用两次尝试实现电话目录
【发布时间】:2016-07-20 23:37:34
【问题描述】:
    I have encountered an interview question
    “Implement a phone directory using Data Structures”                

我想通过尝试解决它。​​通过尝试解决它,我尝试使用两次尝试,一个用于姓名,另一个用于电话号码, 但我遇到了困难。
假设,我必须添加三个条目(AB “112” BC “124” CD “225”) 那么如果我查询名称为“225”,我如何返回CD。 也就是这两个尝试如何联系起来。

    One approach I was thinking was taking two pointers in both the tries.
    These pointers will point to the first and last word in the other trie.
    For example,if the structures are as follows:

    Struct nametrie                            
    {                                                       
     Struct nametrie *child[26];
     struct phonetrie*head,*tail;
     struct phonetrie*root;       

     -----------    
    }                                                       

      Struct phonetrie
     {
             struct phonetrie*child[9];
             struct nametrie*head,*tail;
             struct nametrie*root;
        ----------- 
      }


    Then for AB “112”,  
    Name trie willstore head(1) and tail (2).

但我认为这种方法不适用于重复条目(一个名称和多个数字。)

Can someone please explain a good approach.I am not looking for code but good understanding of approach,may be via diagram or algorithm.

【问题讨论】:

    标签: string algorithm trie


    【解决方案1】:

    我不懂 C,所以我不能评论你的代码。

    使用尝试的想法是有效的。

    您似乎遗漏了节点在尝试中可以保存的数据

    树中的节点有两个主要组成部分

    1. 它拥有的数据可以是任何类型
    2. 子项(或左、右子项)或子项的任意组合的列表

    我们在这里要做的是,我们将向每个节点添加另一个字段并将其称为值“theValue”

    所以 trie 节点看起来像这样

    Class TrieNode{
    public char theChar;
    public String theValue;
    public List<TrieNode> children;
    }
    

    因此,对于正向查找(姓名到电话),您构建一个 Trie,并在与目录中的条目匹配的节点上将 theValue 设置为该条目。

    您将需要创建第二个尝试执行相同的反向查找(电话到姓名)

    举个例子,这个数据会是什么样子

    ( AB “112” AC “124” ACD “225”)

    //create nodes
    TrieNode root = new TrieNode();
    TrieNode A = new TrieNode();
    A.theChar = 'A';
    TrieNode B = new TrieNode();
    A.theChar = 'B';
    TrieNode C = new TrieNode();
    A.theChar = 'C';
    TrieNode C2 = new TrieNode();
    A.theChar = 'C';
    TrieNode D = new TrieNode();
    A.theChar = 'D';
    //link nodes together
    root.children = new ArrayList<>();
    root.children.add(A);
    A.children = new ArrayList<>();
    A.children.add(B);
    A.children.add(C);
    B.children = new ArrayList<>();
    B.children.add(C2);
    //fill the data
    B.theValue = "112";
    C.theValue = "124";
    C2.theValue = "225";
    

    现在你可以轻松遍历这个 Trie,当你到达一个节点并且想要检查值时,只需读取 theValue

    我希望清楚

    【讨论】:

    • :对于反向查找,它不起作用,即从数字到名称。为此,您必须创建另一个特里。如果您在该特里也包含值字段,那么相同的值是存储两次。一次在树中从根到叶节点,其他时间在第二次树中的值字段中
    • @KIMchi 完全正确。所以你会有重复,但在这两种情况下你都会得到快速的搜索访问
    • :也可以不重复吗?有什么建议吗?
    • 我无法特别想到,尽管我们创建了 2 次尝试,但节点指向的数据在每次尝试中都不同。并且根据我的理解,这是尝试中的常态(您将有超过 1 个重复尝试以获得部分搜索和搜索速度)
    • :我还有一个查询,假设有两个同名的号码,请问怎么解决?
    猜你喜欢
    • 1970-01-01
    • 2012-10-06
    • 1970-01-01
    • 1970-01-01
    • 2021-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多