3.1 symbol table
The main purpose of a symbol table is to associate a key with a value. In this example, you can insert a key-value pair into the symbol table and then find the corresponding value from all the key-value pairs in the symbol table. To implement a symbol table, we must first define the data structure behind it and specify the algorithms required to create and operate this data structure to implement insertion and search operations.
Searching is crucial in most applications. Many programming environments implement symbol tables as advanced abstract data structures, including JAVA -- we will discuss Java symbol table implementation in section 3.5. The example given by subscript is the key and value that may be generated in some typical application scenarios. We will immediately see some reference cases. The purpose of section 3.5 is to show you how to effectively use the symbol table in the program. In this book, we will also use symbol tables in other algorithms.
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.
3.1.1 API
A symbolic table is a typical abstract data type. It represents a set of clearly defined values and corresponding operations, allowing us to differentiate the implementation and use of types. As before, we need to use application programming interfaces (APIS) to precisely define these operations and provide a "contract" for data type implementation and use cases ".
Before viewing the use case code, to ensure code consistency, conciseness, and practicality, we must first describe several design decisions in specific implementation.
3.1.1.1 generic
Like sorting, the generic type is used instead of specifying the type of the processing object in the design method. For symbol tables, we clearly define the types of keys and values for search to distinguish between their unused roles, and ignore the priority queue to confuse keys and elements themselves. After considering this basic API (for example, the ordering of the key is not described here), we will use the copparable object to extend the typical use case. This also brings many new methods for data types.
3.1.1.2 duplicate key
All our implementations follow the following rules:
◆ Each key value corresponds to a value (duplicate keys are not allowed in the table)
◆ When the key-value pairs stored in the use case code to the table conflict with the existing keys (and associated values) in the table, the new values will replace the old values.
These rules define the abstract form of the associated array. You can think of a symbol table as an array. The new value replaces the old value. In the associated array (symbol table), keys can be of any type, but we can use it to quickly access the array content. Some programming languages (non-Java) Support programmers to use St [Key] instead of St. get (key), St [Key] = Val to replace ST. put (Key, Val), where key (key) and Val (value) can be any type of object.
3.1.1.3 null key
The key cannot be blank. Like many other mechanisms in Java, using a null key produces a runtime exception.
3.1.1.4 Null Value
We also stipulate that null values are not allowed. The reason for this rule is that in our API definition, when the key does not exist, the get () method returns NULL, which means that any value associated with the key that is not in the table is empty. This rule produces two results (we expected): First, we can use the get () method to test whether the given key exists in the symbol table. Second, we can save the null value as the second parameter of the put () method to the table for deletion, that is, the main content of the delete operation.
3.1.1.5 Delete
In the symbol table, there are two ways to delete: delayed deletion, that is, to set the corresponding value of the key to null, and then delete all keys with null values at a time; or delete the specified key immediately from the table. As we have already said, put (Key, null) is a simple (delayed) Delete () method of Delete (key) to replace this default scheme. In our symbol table implementation, the default scheme is not used, but on the website of this book, put () implementation starts with such a defensive code:
If (val = NULL) {Delete (key); return ;}
This ensures that the values of any key in the symbol table are not empty.
3.1.1.6 convenient methods
To clarify the use case code, we add the contains () and isempty () Methods to the API. Their implementation is shown in the following table.
Method |
Default implementation |
Boolean contains (Key key) |
Return get (key )! = NULL; |
Void Delete (Key key) |
Put (Key, null ); |
Boolean isempty () |
Return size () = 0; |
3.1.1.7 Iteration
To facilitate the use case processing of all the key values in the table, we sometimes add implements iterable <key> to the first line of the API, forcing all implementations to contain iterator () method to return an iterator that implements the hasnext () and next () methods. However, we use a simpler method for symbol tables. We have defined the keys () method to return an iterable <key> formation to Facilitate the use case to traverse all the keys. This is done to keep up with all subsequent methods of the ordered symbol table, so that the use case can traverse a specified part of the table's key set.
3.1.1.8 key equivalence
To determine whether a given key exists in the symbol table, we must first establish the concept of object equivalence. In Java, all objects according to the Conventions inherit an equals () method. Java also provides standard data types such as integer, double, and string, and some more complex types, for example, file and URL implement the equals () method-you can directly use the built-in implementation when using these data types. For example, if both X and Y are of the string type, if and only if the length of X and Y is the same and the letters at each position are the same, X. Equals (y) returns true. The custom key needs to override the equals () method.