`
leonzhx
  • 浏览: 793676 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论
阅读更多

1.  Key-value pair abstraction. (Associative array abstraction. Associate one value with each key)

    a)  Insert a value with specified key.

    b)  Given a key, search for the corresponding value.

 

2.  Symbol Table Applications:

 

3.  Basic symbol table API:

public class ST<Key, Value> {
    ST() {...} //create a symbol table
    void put(Key key, Value val) {...} //put key-value pair into the table (remove key from table if value is null)
    Value get(Key key) {...} //value paired with key (null if key is absent)
    void delete(Key key) {...} //remove key (and its value) from table
    boolean contains(Key key) {...} //is there a value paired with key?
    boolean isEmpty() {...} //is the table empty?
    int size() {...} //number of key-value pairs in the table
    Iterable<Key> keys() {...} //all the keys in the table
}

 

4.  Java requirements for eaquals method: For any non-null references x, y and z:

    a)  Reflexive: x.equals(x) is true.

    b)  Symmetric: x.equals(y) iff y.equals(x).

    c)  Transitive: if x.equals(y) and y.equals(z), then x.equals(z).

    d)  Non-null: x.equals(null) is false.

 

5.  Standard "recipe" for writting equals method for user-defined types.

    a)  Optimization for reference equality.

    b)  Check against null.

    c)  Check that two objects are of the same type and cast.

    d)  Compare each significant field:

        1)  if field is a primitive type, use ==

        2)  if field is an object, use equals()

        3)  if field is an array, apply to each entry (alternatively, use Arrays.equals(a, b) or Arrays.deepEquals(a, b) )

    Best practices.

    a)  No need to use calculated fields that depend on other fields.

    b)  Compare fields mostly likely to differ first.

    c)  Make compareTo() consistent with equals().

 

6.  Frequency counter implementation:

public class FrequencyCounter
{
    public static void main(String[] args)
    {
        int minlen = Integer.parseInt(args[0]);
        ST<String, Integer> st = new ST<String, Integer>();
        while (!StdIn.isEmpty())
        {
            String word = StdIn.readString();
            if (word.length() < minlen) continue;
            if (!st.contains(word)) st.put(word, 1);
            else st.put(word, st.get(word) + 1);
        }
        String max = "";
        st.put(max, 0);
        for (String word : st.keys())
            if (st.get(word) > st.get(max))
                max = word;
        StdOut.println(max + " " + st.get(max));
    }
}

 

7.  Elementary Implementation by an unordered linked list:

    a)  Data structure : Maintain an (unordered) linked list of key-value pairs.

    b)  Search : Scan through all keys until find a match.

    c)  Insert : Scan through all keys until find a match; if no match add to front.

 

8.  Elementary Implementation by an ordered linked list:

    a)  Data structure : Maintain an ordered array of key-value pairs.

    b)  Key point : Binary Search

    c)  Problem : To insert, need to shift all greater keys over.

    d)  Rank helper function. How many keys < k :

    

public Value get(Key key)
{
    if (isEmpty()) return null;
    int i = rank(key);
    if (i < N && keys[i].compareTo(key) == 0) return vals[i];
    else return null;
}

private int rank(Key key)
{//binary search
    int lo = 0, hi = N-1;
    while (lo <= hi)
    {
        int mid = lo + (hi - lo) / 2;
        int cmp = key.compareTo(keys[mid]);
        if (cmp < 0) hi = mid - 1;
        else if (cmp > 0) lo = mid + 1;
        else if (cmp == 0) return mid;
    }
    return lo;
}

 

9.  Ordered Symbol Table API :

public class ST<Key extends Comparable<Key>, Value>
{
    ST() {...} //create an ordered symbol table
    void put(Key key, Value val) {...} //put key-value pair into the table (remove key from table if value is null)
    Value get(Key key) {...} //value paired with key (null if key is absent)
    void delete(Key key) {...} //remove key (and its value) from table
    boolean contains(Key key) {...} //is there a value paired with key?
    boolean isEmpty() {...} //is the table empty?
    int size() {...} //number of key-value pairs
    Key min() {...} //smallest key
    Key max() {...} //largest key
    Key floor(Key key) {...} //largest key less than or equal to key
    Key ceiling(Key key) {...} //smallest key greater than or equal to key
    int rank(Key key) {...} //number of keys less than key
    Key select(int k) {...} //key of rank k
    void deleteMin() {...} //delete smallest key
    void deleteMax() {...} //delete largest key
    int size(Key lo, Key hi) {...} //number of keys in [lo..hi]
    Iterable<Key> keys(Key lo, Key hi) {...} //keys in [lo..hi], in sorted order
    Iterable<Key> keys() {...} //all keys in the table, in sorted order
}

 

 

 

 

  • 大小: 60.2 KB
分享到:
评论

相关推荐

    Basics.Of.Compiler.Design

    Chapter 4 Scopes and Symbol Tables Chapter 5 Interpretation Chapter 6 Type Checking Chapter 7 Intermediate-CodeGeneration Chapter 8 Machine-CodeGeneration Chapter 9 Register Allocation Chapter 10 ...

    Data.Structures.and.Algorithms.Made.Easy.epub

    Disjoint Sets ADT, Graph Algorithms, Sorting, Searching, Selection Algorithms [Medians], Symbol Tables, Hashing, String Algorithms, Algorithms Design Techniques, Greedy Algorithms, Divide and Conquer...

    C程序设计的抽象思维(源码)

    随书源码 PART ONE Preliminaries 1 1 An Overview of ANSI C ...11 Symbol Tables PART FOUR Recursive Lists 12 Recursive Lists 13 Trees 14 Expression Trees 15 Sets 16 Graphs 17 Looking Ahead to Java index

    Coding.Interview.Questions.3rd.Edition.epub )

    Symbol Tables Chapter 18. Hashing Chapter 19. String Algorithms Chapter 20. Algorithms Design Techniques Chapter 21. Greedy Algorithms Chapter 22. Divide and Conquer Algorithms Chapter 23. Dynamic ...

    算法第四版(algorithms),2011年新出版,算法设计力作

    3.1 Symbol Tables 362 3.2 Binary Search Trees 396 3.3 Balanced Search Trees 424 3.4 Hash Tables 458 3.5 Applications 486 CONTENTS vii 4 Graphs . . . . . . . . . . . . . . . . . . . . . . . 515 4.1 ...

    Script Inspector 3 v3.0.30

    Code changes are reflected instantly in its internal data structures, in the parse tree, and in the type model with symbol tables. Changes are then instantly reflected back to all your scripts… You...

    Introduction to Programming in Java 2nd Robert Sedgewick

    Algorithms and data structures: sort/search algorithms, stacks, queues, and symbol tables Applications from applied math, physics, chemistry, biology, and computer science Drawing on their extensive ...

    Introduction to Programming in Java, 2nd Edition (2017.4出版.EPUB格式)

    Algorithms and data structures: sort/search algorithms, stacks, queues, and symbol tables Applications from applied math, physics, chemistry, biology, and computer science Drawing on their extensive ...

    Python Data Structures and Algorithms [2017]

    Python Data Structures and ...Hashing and symbol tables Graphs and other algorithms Searching Sorting Selction Algorithms Design Ttechniques and Sstrategies Implementations, applications and tools

    ELF中文版手册.pdf

    ELF文件还包含符号表(SYMBOL TABLES),记录了文件内的函数和变量的定义和引用。这在链接过程中起着至关重要的作用,因为它允许链接器找到并解析不同文件间的依赖关系。此外,重定位信息(RELOCATION ENTRIES)用于...

    elf文件加载

    5. **符号表(Symbol Tables)**: 记录了全局和局部符号的信息,用于链接时解析引用。 6. **重定位表(Relocation Tables)**: 描述了如何修改代码或数据以适应不同的地址空间。 **二、ELF文件加载过程** 1. **...

    The art of computer programming

    第四卷分为两部分,第一部分《数组算术》(Arrays, Algebraic Manipulation, and Search Theory)讨论了数组运算和搜索理论,而第二部分《符号表、映射和其他数据结构》(Symbol Tables, Maps, and Other Data ...

    elf_readall.rar_elf

    1. **ELF文件结构**:ELF文件包含头部(Elf Header)、节区表(Section Headers)、节区数据、符号表(Symbol Tables)、重定位信息(Relocation Entries)等关键部分。理解这些结构对于解析ELF文件至关重要。 2. *...

    openfst_example_data_files.zip

    2. **构造FST**:"data_files"中可能包含构建FST的基本元素,如输入和输出符号表(symbol tables),它们定义了符号与整数ID的映射。用户可以学习如何使用这些符号表来创建具有特定输入输出关系的FST。 3. **FST...

    DWG文件中字符串信息自动提取的研究

    - **符号表(Symbol Tables)**:例如块表(AcDbBlockTable),用于管理块定义等。 - **对象词典(Object Dictionaries)**:用于存储各种类型的数据库对象。 - **实体(Entities)**:代表图形中的基本元素,如线、...

    算法ppt.zip

    9. **基础符号表(Elementary Symbol Tables)**:061_31ElementarySymbolTables.pdf 介绍了基础的符号表数据结构,用于存储键值对,支持查找、插入和删除操作,包括散列表和二叉搜索树等实现。 这些内容是计算机科学...

    autocad二次开发

    符号表(Symbol Tables)是AutoCAD数据库的核心组成部分之一,用于存储如图层(Layers)、线型(Linetypes)、标注样式(DimStyles)等非图形对象。符号表提供了对这些对象的访问和管理功能。 ##### 3. 事务处理 事务处理...

Global site tag (gtag.js) - Google Analytics