symbols table ( symbol table)
This series of articles mainly introduces the commonly used algorithms and data structure knowledge, recorded is "Algorithms i/ii" the content of the course, using the "algorithm (4th edition)" This red scroll as a learning material, language is java. I needn't say much about the fame of the book. Watercress score 9.4, I also think it is an excellent learning algorithm for books.
through this series of articles, you can deepen your understanding of data structures and basic algorithms (which you personally think are much clearer than school) and deepen your understanding of java. 1 Symbol table introduction and API 1.1 symbol Table Introduction
Symbolic table (symbol table) is a very common data structure, which has a lot of applications in real life. It is a "key"-"value" corresponding to the structure. In the symbol table, a key value pair is stored. The corresponding value is queried by entering the key.
This basic data structure, in the real life of the use of a lot of, such as dictionaries.
1.2 symbol Table API
In fact, the operation of the symbol table is nothing more than the change of check and so on
The
name of the function |
function |
ST () |
Create a Symbol Table object |
void put (key key, Value Val) |
Inserts a key value pair record into the collection, if value is null and does not add |
Value get (Key key) |
Find value based on key if no return null is found |
void Delete (Key key) |
Delete key to key record |
Boolean Contains (Key key) |
To determine if a key is a record in the collection |
Boolean IsEmpty () |
To determine whether a lookup table is empty |
int Size () |
Returns the number of key-value pairs in a collection |
Iterable Keys () |
Returns all keys in the collection |
Ps. There are some conventions to say in advance.
-the value is not NULL
-if key does not exist, get () is returned as null
-Put () overwrites old values
With these conventions, we can simply implement the contain and delete functions.
Public boolean contains (key key)
{return get (key)!= null; }
public void Delete (key key)
{put (key, NULL); }
2 Conventions for keys and values
2.1 values (value)
In Java, if we want to implement a symbolic table, we want it to support all generics. 2.2 Key (key)
For the key, we hope: key is comparable, that is comparable, and the use of the CompareTo () function key is generic can use Equals () to determine equality, can use the hashcode () to get the keys (both functions are Java built-in functions) It is best to use immutable types (integer,double, string, and so on) for key 3 equality tests
If we mention the Equals () function, we have to mention Java's equality test, Java requirements equals () satisfy: reflexivity : x.equals (x) is true symmetry: x.equals (y) IFF Y.equals (x) transitivity: if X.equals (y) and Y.equals (z), then X.equals (z) is not null: x.equals (null) is Fals E
In general, the formula we use for judgment (x = y) does not do type checking. So, when we implement equals (), we should pay special attention to it, it looks very simple, but it is more troublesome to achieve perfection.
such as a date class, you might achieve equals ()
public class Date implements comparable<date>
{
private final int month;
private final int day;
Private final int year;
...
public Boolean equals (Date this)
{
if (this.day != that.day ) return false;
if (this.month!= that.month) return false;
if (this.year != that.year) return false;
return true;
}
But you would be better off doing this: With the final field, you might violate symmetry (inheritance) preferably with object (but this expert is still in the middle of a heated debate) to add reflexivity, not null judgments, and verify type consistency. About comparing different variables in a class:
If the comparison is an original type, with = = If the comparison is an object, with equals () if the comparison is an array, to Arrays.equals (a, b) rather than a.equals (b)
public final class Date implements comparable<date> {private final int month;
private final int day;
Private final int year;
... public boolean equals (Object y) {if (y = = this) return true;
if (y = = null) return false;
if (Y.getclass ()!= This.getclass ()) return false;
Date that = (date) y;
if (this.day!= that.day) return false;
if (this.month!= that.month) return false;
if (this.year!= that.year) return false;
return true; }
}