Data structure tutorial 29th lesson Static Lookup table (i) Sequential table lookup

Source: Internet
Author: User
Tags comparison strcmp

Teaching Objective: to grasp the basic concept of search, the performance analysis of sequential table lookup

Teaching Focus: The basic concept of search

Teaching Difficulty: The performance analysis of sequential table lookup

Teaching Content:

First, the basic concept of search

Lookup table: is a collection of data elements (or records) of the same type.
Lookup Table Actions: 1, query a "specific" data element is in the lookup table.
2. Retrieves a variety of attributes for a "specific" data element.
3, insert a data element in the lookup table;
4. Delete a data element from the lookup table.
Static lookup table Only the first two actions for a lookup table
Dynamic lookup Table Find Table element Collection dynamic change during lookup
Key words Is the value of a data item in a data element (or record)
Primary key word Can uniquely identify a record
Secondary keywords Used to identify a number of records
Find Determines, based on a given value, that a key is equal to the given record or data element in the lookup table. If there is such a record in the table, the lookup is a success, at which point the result of the lookup is the information that gives the entire record, or indicates the location of the record in the lookup table, or if the table does not have a record with the keyword equal to the given value, then the lookup is unsuccessful.

Some conventions:

Typical keyword type description:

typedef float KEYTYPE;//Real Type
typedef int keytype;//Integral type
typedef char *KEYTYPE;//String type

The data element type is defined as:

typedef struct{
KeyType key; Keyword fields
...
}elemtype;

The comparison convention for two keywords is defined as the following macro:

For numeric keywords
#define EQ (A,b) ((a) = = (b))
#define LT (A,b) ((a) < (b))
#define LQ (A,b) ((a) <= (b))

For string-type keywords
#define EQ (A,b) (!STRCMP ((a), (b)))
#define LT (A,b) (strcmp ((a), (b)) <0)
#define LQ (A,b) (strcmp ((a), (b)) <=0)

Second, static lookup table

Type definition for a static lookup table:

ADT staticsearchtable{

A data Object D:D is a collection of data elements with the same attributes. Each data element contains the same type, which uniquely identifies the data element.

Data relation R: A data element is a collection of the same genus.

Basic Operations P:

Create (&st,n);

Action Result: Constructs a static lookup table St with n data elements.

Destroy (&ST);

Initial condition: Static lookup table St exists.

Operation Result: Destroy Table St.

Search (St,key);

Initial condition: Static lookup table St exists, key is the same given value as the keyword type.

Operation Result: If St in its keyword equals key data element, then function value is that element's value or in the table position, otherwise is "empty".

Traverse (St,visit ());

Initial condition: Static lookup Table St existence, visit is the application function to the element operation.

Operation Result: Call function visit () Once and only once for each element of St in a certain order. Once visit () fails, the operation fails.

}adt staticsearchtable

Third, the search of the sequential table

Sequential storage structure for static lookup tables

typedef struct {

Elemtype *elem;

int length;

}sstable;

Sequential lookup: Start with the last record in the table, record each keyword and a given value comparison, if a record of the keyword and the given value is more equal, then find success, find the record;

int Search_seq (sstable st,keytype key) {

St.elme[0].key=key;

for (i=st.length;!) EQ (St.elem[i].key,key); I.);

return i;

}

Performance analysis for lookup operations:

The basic operation in the lookup algorithm is to compare the key of the record with the given value, usually by "average of the number of records compared with its keyword and given value" as the basis for measurement.

Average lookup Length:

To determine where a record is located in a lookup table, the expected value of the number of keywords to compare with the given values is called the average lookup length when the lookup algorithm finds success.

Wherein: PI is the probability of finding the first record in the table, and;

CI The number of keywords that have been compared with a given value to find the first record of its keywords in the table that are equal to the given value.

Equal probability conditions are:

Suppose the probability of finding success is the same as unsuccessful:

Iv. Summary

What is a lookup table

Lookup procedures for sequential tables

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.