Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Hashing

• In all search techniques like linear search, binary search and search trees,
the time required to search an element depends on the total number of
elements present in that data structure. In all these search techniques, as
the number of elements increases the time required to search an element
also increases linearly.
Hashing is another approach in which time required to search an element
doesn't depend on the total number of elements. Using hashing data
structure, a given element is searched with constant time complexity.
Hashing is an effective way to reduce the number of comparisons to search
an element in a data structure.
Definition: Hashing is the process of indexing and retrieving element
(data) in a data structure to provide a faster way of finding the element
using a hash key.
Here, the hash key is a value which provides the index value where the
actual data is likely to be stored in the data structure.
Here, we use a concept called Hash table to store data. All the data
values are inserted into the hash table based on the hash key value.
The hash key value is used to map the data with an index in the hash
table. And the hash key is generated for every data using a hash
function. That means every entry in the hash table is based on the hash
key value generated using the hash function.

Definition: Hash table is an array which maps a key (data) into the
data structure with the help of hash function such that insertion,
deletion and search operations are performed with constant time
complexity (i.e. O(1)).
Hash tables are used to perform insertion, deletion and search
operations very quickly in a data structure. Using hash table concept,
insertion, deletion, and search operations are accomplished in constant
time complexity. Generally, every hash table makes use of a function
called hash function to map the data into the hash table.

• Hash function is a function which takes a piece of data (i.e. key) as


input and produces an integer (i.e. hash value) as output which maps
the data to a particular index in the hash table.
• Basic concept of hashing and hash table is shown in the following
figure.
Red - Black Tree
• Red - Black Tree is another variant of Binary Search Tree in which every node is colored either RED
or BLACK. We can define a Red Black Tree as follows...

• Red Black Tree is a Binary Search Tree in which every node is colored either RED or BLACK.
• In Red Black Tree, the color of a node is decided based on the properties of Red-Black Tree. Every
Red Black Tree has the following properties.

• Properties of Red Black Tree

• Property #1: Red - Black Tree must be a Binary Search Tree.


• Property #2: The ROOT node must be colored BLACK.
• Property #3: The children of Red colored node must be colored BLACK. (There should not be two
consecutive RED nodes).
• Property #4: In all the paths of the tree, there should be same number of BLACK colored nodes.
• Property #5: Every new node must be inserted with RED color.
• Property #6: Every leaf (e.i. NULL node) must be colored BLACK.
A basic red-black tree illustration
B - Tree Data structure
• In search trees like binary search tree, AVL Tree, Red-Black tree, etc.,
every node contains only one value (key) and a maximum of two
children. But there is a special type of search tree called B-Tree in
which a node contains more than one value (key) and more than two
children. B-Tree was developed in the year 1972 by Bayer and
McCreight with the name Height Balanced m-way Search Tree. Later
it was named as B-Tree.

B-Tree can be defined as follows...


B-Tree is a self-balanced search tree in which every node contains
multiple keys and has more than two children.
• Here, the number of keys in a node and number of children for a node depends
on the order of B-Tree. Every B-Tree has an order.
B-Tree of Order m has the following properties:

• Property #1 - All leaf nodes must be at same level.


• Property #2 - All nodes except root must have at least [m/2]-1 keys and
maximum of m-1 keys.
• Property #3 - All non leaf nodes except root (i.e. all internal nodes) must have at
least m/2 children.
• Property #4 - If the root node is a non leaf node, then it must have atleast
2 children.
• Property #5 - A non leaf node with n-1 keys must have n number of children.
• Property #6 - All the key values in a node must be in Ascending Order.
• For example, B-Tree of Order 4 contains a maximum of 3 key values in a node and
maximum of 4 children for a node.
Example
Circular Queue Data structure
• In a normal Queue Data Structure, we can insert elements until
queue becomes full. But once the queue becomes full, we can not
insert the next element until all the elements are deleted from the
queue. For example, consider the queue below...

The queue after inserting all the elements into it is as follows...


• Now consider the following situation after deleting three elements
from the queue...

This situation also says that Queue is Full and we cannot insert the new element because
'rear' is still at last position. In the above situation, even though we have empty positions in
the queue we can not make use of them to insert the new element. This is the major problem
in a normal queue data structure. To overcome this problem we use a circular queue data
structure.
What is Circular Queue?
• A Circular Queue can be defined as follows...
• A circular queue is a linear data structure in which the operations
are performed based on FIFO (First In First Out) principle and the
last position is connected back to the first position to make a circle.

• Graphical representation of a circular queue is as follows...


You might also like