You are on page 1of 20

Semester: 1 Programme: M.Sc.

DS Unit: 1 - 5 Course Code: 23PDS1ES01

Basics of Data Structures


and Algorithms
Elective - 1 : Data Structures &
Algorithms
Dr. M. Kriushanth
Assistant Professor of Data Science

Department of Data Science


St. Joseph’s College (Autonomous)
Tiruchirappalli - 2
Trees
Binary Tree
A binary tree is a tree data
structure in which each parent
node can have at most two
children. Each node of a binary tree
consists of three items:

• data item
• address of left child
• address of right child
Types of Binary Tree
1. Full Binary Tree
• A full Binary tree is a special type of binary tree in which every parent
node/internal node has either two or no children.
2. Perfect Binary Tree
• A perfect binary tree is a type of binary tree in which every internal
node has exactly two child nodes and all the leaf nodes are at the
same level.
3. Complete Binary Tree
A complete binary tree is just like a full binary tree, but with two major
differences
• Every level must be completely filled
• All the leaf elements must lean towards the left.
• The last leaf element might not have a right sibling i.e. a complete
binary tree doesn't have to be a full binary tree.
4. Degenerate or Pathological Tree
• A degenerate or pathological tree is the tree having a single child
either left or right.
5. Skewed Binary Tree
• A skewed binary tree is a pathological/degenerate tree in which the
tree is either dominated by the left nodes or the right nodes. Thus,
there are two types of skewed binary tree: left-skewed binary tree
and right-skewed binary tree.
6. Balanced Binary Tree
• It is a type of binary tree in which the difference between the height
of the left and the right subtree for each node is either 0 or 1.
Binary Search Tree(BST)
Binary search tree is a data structure that quickly allows us to maintain
a sorted list of numbers.

• It is called a binary tree because each tree node has a maximum of


two children.
• It is called a search tree because it can be used to search for the
presence of a number in O(log(n)) time.
• The properties that separate
a binary search tree from a
regular binary tree is
• All nodes of left subtree are
less than the root node
• All nodes of right subtree are
more than the root node
• Both subtrees of each node
are also BSTs i.e. they have
the above two properties
Rehashing
• The reason for rehashing is that anytime a new key-value pair is
placed into the map, the load factor rises, and as a result, the
complexity rises as well. And as the complexity of our HashMap
grows, it will no longer have a constant O(1) time complexity. As a
result, rehashing is used to disperse the items across the hashmap,
lowering both the load factor and the complexity, resulting in search()
and insert() having a constant time complexity of O(1). One should
note that the existing objects may fall into the same or a different
category after rehashing.
How Rehashing is done?
Rehashing can be done in the following way:
• Check the load factor after adding a new entry to the map.
• Rehash if it is more than the pre-defined value (or the default value of
0.75 if none is specified).
• Make a new bucket array that is double the size of the previous one
for Rehash.
• Then go through each element in the old bucket array, calling insert()
on each to add it to the new larger bucket array.
What is Load Factor?
• Load factor is a measure that helps to decide when to increase the
HashTable or HashMap capacity to maintain the operations(search
and insert) complexity of O(1). The default value of the load factor is
0.75, i.e. 75% of the HashMap size.

• Load factor can be decided using the formula as follows:

• The initial capacity of the HashMap * Load factor of the HashMap


• Let's understand Load Factor with the help of an example.

• For example if the initial capacity of Hashtable is 20 and the load


factor of hastable is 0.75(default value), then according to the formula
20*0.75 = 15.

• It means the 15th key-value pair of the hashtable will keep its size as
20. Now when we insert the 16th key-value pair into the hashtable, it
will increases its size from 20 to 20*2 = 40 buckets.
Extensible Hashing
• Extendible Hashing is a dynamic hashing method wherein directories,
and buckets are used to hash data. It is an aggressively flexible
method in which the hash function also experiences dynamic
changes.

• Directories: The directories store addresses of the buckets in pointers.


An id is assigned to each directory which may change each time when
Directory Expansion takes place.
• Buckets: The buckets are used to hash the actual data.
Priority Queue
• A priority queue is a type of queue that arranges elements based on
their priority values. Elements with higher priority values are typically
retrieved before elements with lower priority values.

• In a priority queue, each element has a priority value associated with


it. When you add an element to the queue, it is inserted in a position
based on its priority value. For example, if you add an element with a
high priority value to a priority queue, it may be inserted near the
front of the queue, while an element with a low priority value may be
inserted near the back.
• There are several ways to implement a priority queue, including using
an array, linked list, heap, or binary search tree. Each method has its
own advantages and disadvantages, and the best choice will depend
on the specific needs of your application.

• Priority queues are often used in real-time systems, where the order
in which elements are processed can have significant consequences.
They are also used in algorithms to improve their efficiencies, such as
Dijkstra’s algorithm for finding the shortest path in a graph and the
A* search algorithm for pathfinding.

You might also like