# Various implementations of Symbol Table

Symbol Table is an important data structure that is created and maintained by the compilers in order to track information about the occurrences of various entities such as variable names, objects, function names, interfaces, etc. The information that is collected by the compiler inside the symbol table in the analysis phase is used by the synthesis phase to generate the target code.

Comparison between different implementations of Symbol Table :

**1. Array (Sorted) :**

**Insertion Time –**

When inserting an element traversing must be done in order to shift elements to right. Thus, it takes O(n) time.**Lookup Time –**

While looking up a binary search can be used to find an element. Thus, it takes O(log n) time.

**2. Linked List :**

**Insertion Time –**

Similar to arrays here in order to search for the position to insert the element traversing is required. Thus, it takes O(n) time.**Lookup Time –**

While fetching a data item the linked list must be traversed completely ( linear search ). Thus, this operation takes O(n) time.

**3. Unordered List :**

**Insertion Time –**

In an unordered array, insertion can be done at the beginning whereas in an unordered linked list insertion can be done at the end. Thus, it requires O(n) time.**Lookup Time –**

As it is an unordered list complete list must be searched in order to find an element. Thus, it takes O(n) time.

Disadvantages of using Array, Linked Lists, or Unsorted Lists for symbol table implementations :

- Lookup time is directly proportional to the table size.
- Every insertion operation is preceded with lookup operation.

**4. Self-organizing List :**

**Insertion Time –**

It is generally an unsorted list therefore, the time taken to insert an element is O(1).**Lookup Time –**

Searching for an element here requires to search an element and then shift it to the beginning of the list so that the most frequently used elements are at the beginning of the list. As it is, in general, an unsorted list searching requires O(n) time.

Disadvantages of using Self-organizing Lists for symbol table implementations :

The disadvantage in this implementation is that whenever less frequent elements are searched then the performance becomes poor.

**5. Search Trees (Balanced) :**

**Insertion Time –**

If there are n elements in the tree and the order of the tree is k, then the height of the tree will be logkn. Now, in the worst case, the insertion would happen at the height of the tree. Thus, the time required is O(logkn).**Lookup Time –**

In a balanced search tree, the time required for searching is O(logkn).

Disadvantages of using Search Trees for symbol table implementations :

The disadvantage of this implementation is that the tree always has to be kept balanced.

**6. Hashing :**

**Insertion Time –**

As in a hash table insertion takes constant time so the time required for insertion is O(1).**Lookup Time –**

Searching in a hash table requires constant time so the time required is O(1).

Disadvantages of using Hashing for symbol table implementations :

The disadvantage of this implementation is when there are too many collisions the time complexity increases to O(n).

Thus, we can see that every implementation of the symbol table is unique and has its own disadvantages. The implementer according to its requirements can choose any of the above implementations of Symbol Tables.