Data Structure Using Python
Data Structure Using Python
Instructions:
• Read this study material carefully and make your own handwritten short notes. (Short
notes must not be more than 5-6 pages)
• Revise this material at least 5 times and once you have prepared your short notes, then
revise your short notes twice a week
• If you are not able to understand any topic or required detailed explanation,
please mention it in our discussion forum on webiste
• Let me know, if there are any typos or mistake in study materials. Mail
me at piyushwairale100@gmail.com
• Mutable data structures are those which we can modify – for example, by adding,
removing, or changing their elements. Python has three mutable data structures: lists,
dictionaries, and sets.
• Immutable data structures, on the other hand, are those that we cannot modify after
their creation. The only basic built-in immutable data structure in Python is a tuple.
1.1.1 List
• Lists are dynamic, ordered collections of elements that can hold items of various data
types, including numbers, strings, and even other lists. Their defining feature is their
ability to be modified, expanded, or reduced during program execution. This versatility
makes lists immensely useful for scenarios where data needs to be managed flexibly.
• Lists are created using square brackets, and elements within them are separated by
commas.
• Lists are mutable, ordered sequences of elements in Python. Declared using square
brackets [ ].
• List items are indexed, the first item has index [0], the second item has index [1] etc.
• When we say that lists are ordered, it means that the items have a defined order, and
that order will not change. If you add new items to a list, the new items will be placed
at the end of the list.
• The list is changeable, meaning that we can change, add, and remove items in a list
after it has been created.
1.1.2 Operations:
1. Creation:
2. Accessing Elements:
3. Adding Elements:
4. Removing Elements:
5. Other Operations:
1.2 Tuples
• Tuples resemble lists in that they are ordered collections of elements, but they have a
significant difference: they are immutable, meaning their content cannot be changed
after creation.
• Tuples are defined using parentheses, and elements are separated by commas.
• Tuple items are indexed, the first item has index [0], the second item has index [1] etc.
• When we say that tuples are ordered, it means that the items have a defined order,
and that order will not change.
• Tuples are unchangeable, meaning that we cannot change, add or remove items after
the tuple has been created.
1.2.1 Operations:
1. Creation:
1.3 Sets
• Set items are unordered, unchangeable, and do not allow duplicate values.
• Sets are unordered collections of unique elements. Declared using curly braces { } or
set().
• Set items are unordered, unchangeable, and do not allow duplicate values.
• Set items are unchangeable, meaning that we cannot change the items after the set
has been created.
• Once a set is created, you cannot change its items, but you can remove items and add
new items.
1.3.1 Operations:
1. Creation:
3. Set Operations:
1.4 Dictionaries
• Dictionaries are used to store data values in key:value pairs.
• Dictionaries are key-value pairs. Declared using curly braces { } with key-value pairs.
• Dictionary items are ordered, changeable, and does not allow duplicates.
• Dictionary items are presented in key:value pairs, and can be referred to by using the
key name
• When we say that dictionaries are ordered, it means that the items have a defined
order, and that order will not change.
Unordered means that the items does not have a defined order, you cannot refer to an
item by using an index.
As of Python version 3.7, dictionaries are ordered. In Python 3.6 and
earlier, dictionaries are unordered.
• Dictionaries are changeable, meaning that we can change, add or remove items after
the dictionary has been created.
1.4.1 Operations:
1. Creation:
3. Dictionary Operations:
2. Representation:
• List: Represented by [ ].
• Tuple: Represented by ( ).
• Set: Represented by { }.
• Dictionary: Represented by { }.
3. Duplicate Elements:
5. Example:
7. Mutation:
• List: Mutable.
• Tuple: Immutable.
• Set: Mutable.
• Dictionary: Mutable, but keys are not duplicated.
8. Order:
9. Empty Elements:
• List: l = [].
• Tuple: t = ().
• Set: a = set() or b = set(a).
• Dictionary: d = {}.
• The linked list elements are accessed with special pointer(s) called head and tail.
• The principal benefit of a linked list over a conventional array is that the list elements
can easily be added or removed without reallocation or reorganization of the entire
structure because the data items need not be stored contiguously in memory or on
disk.
• Linked lists allow insertion and removal of nodes at any point in the list.
• Finding a node that contains a given data, or locating the place where a new node
should be inserted may require scanning most or all of the list elements.
• Adding, insertion or deletion of list elements can be accomplished with minimal dis-
ruption of neighbouring nodes
• A linked list is also used to implement other user-defined data structures like stack and
queue.
• Using the collections module in Python, we can use the deque object to implement
operations like insert and delete on linked lists.
• The first node in a linked list is the head, and we must start all the operations on the
linked list from it.
• The last node of the linked list refers to None showing that the linked list is complete.
• Each node will hold two references along with the data, one to its previous node and
the next to the succeeding node.
• Each node points to both the next and the previous nodes.
10
• The operations which can be performed in SLL can also be preformed on DLL.
• The major difference is that we have to adjust double reference as compared to SLL.
• We can traverse or display the list elements in forward as well as in reverse direction.
11
2.3 Stack
• A stack is a last in first out (LIFO) abstract data type and data structure. A stack
can have any abstract data type as an element, but is characterized by only two
fundamental operations, PUSH and POP
• The PUSH operation adds an item to the top of the stack, hiding any items already
on the stack or initializing the stack if it is empty.
• The POP operation removes an item from the top of the stack, and returns the poped
value to the caller.
• Elements are removed from the stack in the reverse order to the order of their insertion.
Therefore, the lower elements are those that have been on the stack for the longest
period.
• top() / peek() – Returns a reference to the topmost element of the stack – Time
Complexity: O(1)
• push(a) – Inserts the element ‘a’ at the top of the stack – Time Complexity: O(1)
• pop() – Deletes the topmost element of the stack – Time Complexity: O(1)
12
• Using List
Python list can be used as the stack. It uses the append() method to insert elements to
the list where stack uses the push() method. The list also provides the pop() method
to remove the last element, but there are shortcomings in the list. The list becomes
slow as it grows.
The list stores the new element in the next to other. If the list grows and out of a
block of memory then Python allocates some memory.
• Using collection.deque
The collection module provides the deque class, which is used to creating Python
stacks. The deque is pronounced as the ”deck” which means ”double-ended queue”.
The deque can be preferred over the list because it performs append and pop operation
faster than the list. The time complexity is O(1), where the list takes O(n).
2.4 Queue
A queue is an ordered collection of items from which items may be deleted at one end (called
the front of queue) and into which items may be inserted at the other end (called rear of
queue). Queue is a linear data structure that maintains the data in first infirst out (FIFO)
order
13
• Dequeue: Removes an item from the queue. The items are popped in the same order
in which they are pushed. If the queue is empty, then it is said to be an Underflow
condition – Time Complexity : O(1)
• Front: Get the front item from queue – Time Complexity : O(1)
• Rear: Get the last item from queue – Time Complexity : O(1)
2. Using collections.deque
Queue in Python can be implemented using deque class from the collections module.
Deque is preferred over list in the cases where we need quicker append and pop opera-
tions from both the ends of container, as deque provides an O(1) time complexity for
append and pop operations as compared to list which provides O(n) time complexity.
Instead of enqueue and deque, append() and popleft() functions are used.
3. Using queue.Queue
Queue is built-in module of Python which is used to implement a queue. queue.Queue(maxsize)
initializes a variable to a maximum size of maxsize. A maxsize of zero ‘0’ means a in-
finite queue. This Queue follows FIFO rule.
14
The advantage of using a circular queue over linear queue is efficient usage of memory.
2.5 Trees
• Tree is non-linear data structure designated at a special node called root and elements
are arranged in levels without containing cycles.
(or)
The tree is
1. Rooted at one vertex
2. Contains no cycles
3. There is a sequence of edges from any vertex to any other
4. Any number of elements may connect to any node (including root)
5. A unique path traverses from root to any node of tree
6. Tree stores data in hierarchical manner
7. The elements are arranged in layers
• A tree data structure is a hierarchical structure that is used to represent and organize
data in a way that is easy to navigate and search. It is a collection of nodes that are
connected by edges and has a hierarchical relationship between the nodes.
15
• The topmost node of the tree is called the root, and the nodes below it are called the
child nodes. Each node can have multiple child nodes, and these child nodes can also
have their own child nodes, forming a recursive structure.
• This data structure is a specialized method to organize and store data in the computer
to be used more effectively. It consists of a central node, structural nodes, and sub-
nodes, which are connected via edges. We can also say that tree data structure has
roots, branches, and leaves connected with one another.
16
• Level of a node: The count of edges on the path from the root node to that node.
The root node has level 0.
• Internal node: A node with at least one child is called Internal Node.
• Neighbour of a Node: Parent or child nodes of that node are called neighbors of
that node. Subtree: Any node of the tree along with its descendant.
Full binary tree: It is a binary tree, for which all leaf nodes are at same level and all
intermediate nodes contains exactly 2 children.
(or) A tree with depth ‘K’ contains exactly 2K – 1 nodes.
Strictly binary tree: A binary tree in which every node contains exactly 0 or 2 children.
Skewed binary tree: A binary tree in which elements are added only in one direction
• Insertions and deletions may require the tree to be rebalanced by one or more tree
rotations.
17
• The balance factor of a node is the height of its left subtree minus the height of its right
subtree (sometimes opposite) and a node with balance factor—1, 0 or -1 is considered
balanced. A node with any other balance factor is considered unbalanced and requires
rebalancing the tree.
• The balance factor is either stored directly at each node or computed from the heights
of the subtrees.
• The shape property: The tree is a complete binary tree; that is, all levels of the tree,
except possibly the last one (deepest) level of the tree is not complete, the nodes of
that level are filled, from left to right.
• Max-Heap
A heap in which each node is greater than or equal to its children is called max-heap.
Max-Heap generally used for heap sort.
• Min-Heap
A heap in which, each node is smaller than or equal to its children is called Min-Heap.
Min-heap generally used to implement priority queue.
Note: By default heap represent Max-Heap
• It operates on the hashing concept, where each key is translated by a hash function
into a distinct index in an array. The index functions as a storage location for the
matching value. In simple words, it maps the keys with the value.
• Hash tables are a type of data structure in which the address or the index value of the
data element is generated from a hash function. That makes accessing the data faster
as the index value behaves as a key for the data value. In other words Hash table
stores key-value pairs but the key is generated through a hashing function.
• So the search and insertion function of a data element becomes much faster as the key
values themselves become the index of the array which stores the data.
• In Python, the Dictionary data types represent the implementation of hash tables. The
Keys in the dictionary satisfy the following requirements.
18
• The keys of the dictionary are hashable i.e. the are generated by hashing function
which generates unique result for each unique value supplied to the hash function.
19
References
• geeksforgeeks.org/
• javatpoint.com/
• w3schools.com/
• tutorialspoint.com/
• byjus.com
20