Data Structures in Python | CBSE Class 12
In this section of “Data Structure in Python | Stack and Queue Using Lists“, we will provide a comprehensive information about all concepts of Data Structures in Python like Stack and Queue Using Lists with examples including
- Elementary Data Representation in Python.
- Data Type Vs Data Structure in python.
- Different Types of Data Structure in python.
and much more, nearly all information about Data Structure in Python which is necessary to become a python Developer / Programmer.
Surely, by the end of this tutorial, you will have a solid understanding of all about available Data Structure in Python and will be able to use this knowledge in their own programming projects.
Also this tutorial covers all necessary topics/concepts required to complete your exams preparations in CBSE schools / classes 11th and 12th in 2023 – 2024.
INTRODUCTION TO DATA STRUCTURES
The computer system is used essentially as data manipulation system where “Data’ are very important thing for it. The data can be referred to in many ways viz. data, data items, data structures etc. The data being an active participant in the organizations’ operations and Planning, data are aggregated and summarized in various meaningful ways to form information. The data must be represented, stored, organized, processed and managed so as to support the user environment. All these factors very much depend upon the way data are aggregated. The Data structures are an effective and reliable way to achieve this.
We are going to introduces data structures, basic terminology used for them, and different commonly used data structures and also be discussing about Stacks and Queues data structures.
ELEMENTARY DATA REPRESENTATION IN PYTHON
Elementary representation of data can be in forms of raw data, data item, data structures.
- Raw data are raw facts. These are simply values or set of values.
- Data items are used as per their associated data types
Data items are used as per their associated data types.
While designing data structures, one must determine the logical picture of the data in a particular program, choose the representation of the data, and develop the operations that will be applied on.
Data Type vs. Data Structure in python
In python A Data type defines a set of values along with well-defined operations stating its input-output behavior for example, you cannot put decimal point in an integer or two strings cannot not be multiplied etc.
Data structure in python, On the other hand, is a physical implementation that clearly defines a way of storing, accessing, manipulating data stored in a data structure. The data stored in a data structure has a specific work pattern e.g., in a stack, all insertions and deletions take place at one and only.
For example, Python supports lists as a collection data type which can store heterogenous types of elements, but when we implement List as a data structure, its behavior is clearly a redecided for example, it will store all elements of same type; for searching, list elements will be presorted and so forth.
Different Data Structures in python
Data Structures are very important in a computer system, as these not only allow the user to combine various data types in a group but also allow processing of the group as a single unit thereby making things much simpler and easier. The data structures can be classified into following two types:
-
Simple Data Structures in python.
These data structures are normally built from primitive data types like integers, reals, characters, boolean. Following data structures can be termed as simple data structures:
-
Array or Linear Lists
-
Compound Data Structures in python.
Simple data structures can be combined in various ways to form more complex structures called compound data structures. Compound data structures are classified into following two types:
-
Linear data structures.
These data structures are single level data structures. A data structure is said to be linear if its elements form a sequence. Following are the examples of linear data structures
(a) Stack
(b) Queue
(c) Linked List
-
Non-Linear data structures.
These are multilevel data structures. Example of non-linear data structure is Tree.
Below given figure shows, all the data structures.
Linear Lists Arrays
Linear Lists or Arrays refer to a named list of a finite number n of similar data elements. Each of the data elements can be referenced respectively by a set of consecutive numbers, usually 0, 1, 2, 3….n. If the name of a linear list of 10 elements is LIL, then its elements will be referenced as shown:
LIL[0], LIL[1], LIL[2], LIL[3], … LIL[91
Arrays can be one dimensional, two dimensional or multi-dimensional. In Python, arrays are implemented through List data types as Linear Lists or through NumPy arrays.
Stacks
Stacks data structures refer to the lists stored and accessed in a special way, where LIFO (Last In First Out) technique is followed. In Stacks, insertions and deletions take place only at one end, called the top. Stack is similar to a stack of plates as shown in Fig. 9.2(a). Note that plates are inserted or removed only from the top of the stack.
Queues
Queues data structures are FIFO (First in First Out) lists, where insertions take place at the “rear” end of the queues and deletions take place at the “front” end of the queues. Queue is much the same as a line of people [shown in Figure] waiting for their turn to vote. First person will be the first to vote and a new person can join the queue, at the rear-end of it.
Linked lists
Linked lists are special lists of some data elements linked to one another. The logical ordering is represented by having each element pointing to the next element. Each element is called a node, which has two parts. The INFO part which stores the information and the reference-pointer part, which points to i.e., stores the reference of the next element. Below given figure shows both types of lists (singly linked lists and doubly linked lists).
In singly linked lists, nodes have one reference-pointer(next) pointing to the next node, whereas nodes of doubly linked lists have two reference-pointers (prev and next). Prev points to the previous node and next points to the next node in the list.
Trees
Trees are multilevel data structures having a hierarchical relationship among its elements called nodes. Topmost node is called the root of the tree and bottommost nodes are called leaves of the tree. Each of the nodes has some reference-pointers, pointing to (i.e., storing the reference of) the nodes below it.
Operations on Data structures
The basic operations that re performed on data structures are as follows:
- Insertion. Insertion means addition of a new data element in a data structure.
- Deletion means removal of a data element from a data structure. The data element is searched for before its removal.
- Searching. Searching involves searching for the specified daa element in a data structure.
- Traversal of data structure means processing all the data elements of it, one by one.
- Sorting. Arranging data elements of a data structure in a specified order is called sorting.
- Merging. Combining elements of two similar data structures to form a new data structure of same type, is called merging.