`
leonzhx
  • 浏览: 768310 次
  • 性别: 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 ...

    Introduction to Programming in Java: An Interdisciplinary Approach, 2nd Edition

    Programming skills are ...Algorithms and data structures: sort/search algorithms, stacks, queues, and symbol tables Applications from applied math, physics, chemistry, biology, and computer science

    Crafting a Compiler by Charles N. Fisher Richard J. LeBlanc 带目录版

    8 Symbol Tables and Declaration Processing 279 9 Semantic Analysis 343 10 Intermediate Representations 391 11 Code Generation for a Virtual Machine 417 12 Runtime Support 445 13 Target Code Generation...

    算法第四版(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 ...

    Mastring Perl

    symbol tables and typeglobs - How Perl keeps track of package variables and how you can use that mechanism for some powerful Perl tricks; define subroutines on the fly and turn the tables on normal ...

    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

    basics of compiler design

    4 Symbol Tables 5 Type Checking 6 Intermediate-Code Generation 7 Machine-Code Generation 8 Register Allocation 9 Function calls 10 Analysis and optimisation 11 Bootstrapping a compiler

    Crafting a Compiler

    8 Symbol Tables and Declaration Processing 279 9 Semantic Analysis 343 10 Intermediate Representations 391 11 Code Generation for a Virtual Machine 417 12 Runtime Support 445 13 Target Code Generation...

    Math_into_LaTeX-4.zip

    Samples for the book `(More) Math into LaTeX', 4th edition. ... - Math and Text Symbol Tables. The samples are in the public domain, the Short Course is copyrighted, but can be freely distributed.

    A Lock-Free Wait-Free Hash Table - Slides (070221_LockFreeHash)-计算机科学

    • Used for symbol tables, DB caching, networkaccess, url caching, web content, etc • Crucial for Large Business Applications─ &gt; 1MLOC • Used in Very heavily multi-threaded apps─ &gt; 1000 ...

    Ldpc编码的介绍与应用

    Abstract— Efficient implementations of the sum-product algorithm (SPA) for decoding low-density parity-check (LDPC) codes using log- likelihood ratios (LLR) as messages between symbol and parity-...

    easyload 9.0

    SYMBOL A MapInfo Symbol clause (if the layer contains only points); or a Symbol clause followed by a Pen clause (indicating styles for linear features) followed by another Pen clause (indicating ...

    哈希算法ppt

    Dictionary: Dynamic-set data structure for storing items indexed using keys. Supports operations Insert, Search, and ...Memory-management tables in operating systems. Large-scale distributed systems.

    NIST SP800-87r2.pdf

    Federal departments and independent agencies are invited to assist in the maintenance of this publication by submitting copies of newly implemented tables of organization that affect any listing....

Global site tag (gtag.js) - Google Analytics