- Lists
- Tuple
- Dictionary
- Sets
- Stack
- Queue
- Linked Lists
- Graphs
- Trees
This is a data structure in Python that is a mutable, or changeable, ordered sequence of elements.
- Mutable: Can be changed or altered after initialization
- element are accessed by index
- 0 Indexed: can be indexed to get element starting from 0
- Lists are ordered
- Dynamic: Grows and shrinks in effect to data added and deleted
- Nested: list can be inside of lists
list_name = [1, 2, 3, 4, 5]
This is similar to list and is used to store multiple items in a single variable.
- Immutable: Cannot be modified after initialization
- Only supports immutable Datatypes
- Nested: Tuples Can be inside other tuples
tuple_name = (1, 2, 3, 4, 5)
This is an unordered and mutable Python container that stores mappings of unique keys to element
- Mutable: Can Be edited after initialization
- Keys are Unique
- keys are immutable
dictionary_name = {"key1": value, "key2": value2}
A set is a collection which is unordered, unchangeable, and unindexed.
- immutable: Can not Be edited after initialization
- Distinct: ommits repititions and has no duplicates
- Unordered
set_name = {value1, value2}
Stack is a linear data structure which follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out)
End where element are added is called top and opposite end is called base
- Push: adding element to the top of the stack
- pop: removing element from top
- peek or top: return the top element
- isEmpty: Check sif stack is empty
- List
- Modules
- Push operation: list append function
- pop operation: list pop function
-
Collections Module(deque class) deque class in collection module has the pop and append function enabling use in the stack data type
-
Queue Module(Lifo Queue)
- For push operation "put" method is used
- For pop operation "get" method is used
- isempty: isempty function
- isfull: isfull function
- Limit can be set bu putting the limit value in the parenthesis when initializing stack
This is a linear data structure that is open at both ends and the operations are performed in First In First Out (FIFO) last in last out(LILO) order. Elements are added at one end(rear, back or tail) and removed from the opposite(head, front) end.
- LILO or FIFO
- enque: Adding element to queue
- deque: Removing element from queue
- isFull: If queue is full
- IsEmpty: If queue is empty
- Lists
- Modules
-
Inserting from Right side of list and removing from left side
- enqueue: append function
- dequeue: pop function using 0 index
-
Inserting Left side and removing from right side of list
- enqueue: insert ti zero index
- dequeue: pop with no ararguments
-
dequeue (double ended queue): Collection Module
- Inserting from Right and removing from left side
- enqueue: append function
- dequeue: popleft function
- Inserting from left and removing from right side
- enqueue: appendleft function
- dequeue: pop function
- Inserting from Right and removing from left side
-
queue: Queue Module
- enqueue: put function
- dequeue: get function
They is a modified versoin of the queue data structure in which each element is associated with a priority, and elements are removed according to their priority and if elements have the same priority they are removed according to their position in the queue.
- Elements can be assigned to priorities by collecting values in tuples storing the value and its priority
- Value of elements could also signify its priority
- Lists and Tuples
- Priority Queue class(Queue Module)
Linked List is a dynamic linear Data structure made of chain of nodes in which each node contains a data field and a link or reference to the next node.
Sometimes the node contains two links or references, one for the previous node and one for the next. Number of links in each node depends on the type of linked list.
The link or reference on the last node depends on the type of linked list but for most of the types, The last node link value contains the address to the empty value or None.
Nodes of the linked list is stored randomly in memory and not (contegiously) one after the other ike normal lists.
- Node: Each element in the linked list.
- Head or Front: address of First node or Start point of linked list.
- Tail or End: Last Node.
- Data: Data field.
- Dynamic Memory Allocation: No need to specify size.
- Elements are inserted and deleted easily: No need to shift elements, Just change addresses.
- They can be used to implement stacks, queue and graphs.
- Used to represent and manipulate polynomials
- Requires more nemory to store addresses
- No elements can be assed randomly, each have to be passed
- Singly Linked List: Node contains only one Link.
- Doubly Linked List: Node contains two Links.
- Circular Linked List:
Each Node in the singly linked list contains a data field and a link to the next node, while the last node link field contains a None value.
- Nearly Impossible to move backwards
- Adding Or Insertion: Adding nodes to linked list
- Delete: Removing elements from linked list
- Traversal
- Beginning
- End
- Inbetween
- Create Node
- Copy link from head to new node link field
- Copy address of new node to Head
- Create Node
- Set link field of new node to None
- Copy address of new node to last node link address
- Create Node
- Move to Node at one step before desired position for new node
- Change link field of Current node to address of new node
- Set link field of new node of new node no next node after desired position
- Beginning
- End
- Inbetween
- Change Head to second Node
- Change the link field of the second to the last node to None
- Move to node one step before before node to be deleted
- Change link field of current node to the address of node after node to be deleted
This is going through all nodes in a linked list
- Create Node
- Set self.head prev reference to new_node
- Change next reference of new_node to self.head
- Create Node
- move to current end of list
- Change the next value of the last node to adress of new node
- Set the previous reference of new_node to address of current last node
- Create New node
- Set self.head to new node
- Set self.head to node after self.head
- Set the previous reference of new head to None
- Move to Second to Last Node in list
- Change current node next reference to None
The circular linked list is a linked list where all nodes are connected to form a circle.
In circular linked lists the last node of the linked list contains the address of the first node.
There are tyo types of circular linked lists:
- Circular Singly Linked Lists
- Circular Double linked Lists
- You can start to traverse from any point in the linked list
- Since it contains no null value, if proper caution is not taken it may lead to an infinite loop
- Insertion: Add nodes
- Deletion : Remove nodes
- Traversal: Loop through all the nodes present in the linked list
- Create Node
- Set reference of new node points to itself
- set head to new node
- Create New node
- Set reference field of the new node to the head
- set reference of last node to the new node
- Create New node
- Set reference field of the last node to the new node
- set reference of new node to the head
- Create New node
- Set reference field of the last node to the new node
- set reference of new node to the head
- Point head to second Node
- Set last node reference field to second node
- Traverse to second to last node
- Set Reference to the head
- Traverse to node before the required node
- Set reference to reference next node
- Start from next node of last node
- Print data and go to next node and continue until ref equals head
This is used to find a node n position from the end of a linled list
- Stack
- Queue
- Linked-Lists
- Data items are Traversed Serially and all items are traversed in a single run
- Organised Linearly or sequentially
- Easier to implement
- Only one level depth
- Trees
- Graphs
- Not Organised Linerly or sequentially
A tree is a non-linear data structure that represent relationships between nodes
A tree is a collection of nodes connected by edges and may or may not have children
The top most node of the tree is the root, It is the origin of the tree. There is only one root in a tree
is a node that has a branch to another node
This is a node that was branched from a another node, every node except the roots are child nodes.
Nodes that belong to the same parents are sibling nodes
This are nodes that that have no children.
This are nodes with at least one child
The sequence of nodes and edges from one node to another node is known as path
This is the total number of children of the node
The node with the highest degree is the degree of the whole tree
This is the amount of generations in the tre
This is the total number of edges that lies on the longest path from any leaf node to a particular node
- Height of tree is the height of the root node
This is the number of edges from root node to partcular node
- Depth of tree is the total edges in longest path from root node to leaf node
- The root node can be used to traverse the tree and every non empty tree has a root
- If a tree contains N nodes, The number of edges or links is N-1.
- Every child node can have only one parent but every parent can have multiple children
- Trees are recursive, A tree contains smaller trees called sub-trees
- General Tree: There is no limitation for number of children for each node.
- Binary tree: Every node have a maximum of two children.
- Full Binary tree
- Complete Binary Tree
- Perfect Binary Tree
- Balanced Banary Tree
- pathological Binary Tree
This is a type of binary in which a node has exactly zero or two children. They are also called strict Binary Tree.
This is a type of binary tree where all levels except last level are completely filled with nodes and all nodes on bottom level are completely filled or filled from left to right.
This a binary tree where all internal nodes have two children and the leaf nodes are all in same level
This is a binary tree where hieght of the left and right sub-trees of every node may differ by at most 1
This a special type of Binary Tree with particular features Listed Below
- The left sub tree of a node contains only nodes with keys lesser than node's key
- The right sub tree of a node contains only nodes with keys greater than node's key
- The left and right sub tree must also be Binary Search Trees
- Binary Search trees are used for quick search algorihims
- Heap is used for heap sort