Algorithm-symbol table, algorithm symbol
Definition:A symbol table is a data structure that stores key-value pairs. It supports two types of operations: insert (put), storing a new set of key-value pairs into a table; search (get ), obtain the corresponding value based on the given key.
1. Ordered symbol table
2. Cost Model
Search cost model:When learning the implementation of the symbol table, we will count the number of comparisons (equivalence tests or comparison of keys ). When the inner loop is not compared (very small), we will count the number of visits to the array.
3. Use Cases for symbol tables
/*** Symbol table use case */public class FrequencyCounter {public static void main (String [] args) {int minlen = Integer. parseInt (args [0]); // minimum key length ST <String, Integer> st = new ST <String, Integer> (); // construct the symbol table and calculate the frequency while (! StdIn. isEmpty () {String word = StdIn. readString (); // ignore the shorter word if (word. length () <minlen) {continue;} if (! St. contains (word) {st. put (word, 1);} else {st. put (word, st. get (word) + 1) ;}/// find the most frequently occurring word 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 ));}}
【Source code download]