15

I'm a bit confused on what exactly the meaning of a 'key' is in computer science. I understand key-value pairs, primary keys, etc... But I can't find a definition of what the term 'key' means by itself.

As far as I can tell it just means a piece of data. In CLRS, data associated with tree nodes are referred to as 'keys'. Data to search a hash table is called a 'key'. Is this what a 'key' is?

TheMax
  • 177
  • 1
  • 1
  • 5

2 Answers2

31

In the most general sense, a key is a piece of information required to retrieve some data. However, this meaning plays out differently depending on exactly what situation you're dealing with.

In the contexts you mention, a key is a unique identifier for the complete data used to retrieve it from some location in the structure. Each key is associated with only one item, so it can be used to find a particular set of data. The data structure will usually be organized in such a way that finding the key is much more efficient than a linear search through all of the data. Sometimes the key is actually part of the data and stored along with it (like primary keys in the database); other times, it is segregated from the data itself (like in a hash map). The data structure will also often perform extra processing on the key (and only the key) to support its efficient searching algorithm (such as in a hash map, the key is converted into a hash code, or a database will index the primary keys using a B-tree).

In cryptography, a key is used in a sense more akin to physical keys used on locks. They're pieces of data required to obtain the original from the encrypted data (to "unlock" the data, so to speak).

jpmc26
  • 428
  • 3
  • 6
13

A key in the context of data structures (such as in the book CLRS) is a value (often an integer) that is used to identify a certain component of a data-structure. Often, keys determine how the underlying data is stored or manipulated. For example, in binary search trees we have that for every node, the key of that node is larger than the keys in the left sub-tree and smaller than those in the right subtree. This property makes it easier to search for a given key (or determine there is no node with such a key).

In practice, our 'actual' data is often not a key, but something larger and more relevant that a single number. This data is called satellite data and can be mostly ignored when dealing with manipulations on data structures, as long as the satellite data moves whenever the key gets moved (otherwise, you lose track of your data).


The concept of a key is similar in the context of databases, but there it is often required that a key is unique. A primary key has to be unique, for example. This requirement is often nessecary in the context of data-structures, but is sometimes made for simplicity.

In cryptography, a key usually refers to an (often secret, but not always!) parameter that is needed to encrypt or decrypt with a given en- or decryption algorithm. The keys used to encrypt and decrypt have to be 'related' (in symmetric cryptography, the need to be the same) for the process to encryption or decryption to be successful.

Discrete lizard
  • 8,392
  • 3
  • 25
  • 53