Data Structures Flashcards
Arrays
Collection of elements stored in memory locations through which each element can be accessed by its index
Accessing an element in array: time complexity
O(1)
Deleting elements in the middle of an array: time complexity
O(n), because it may require shifting elements
Linked Lists
Linked lists are a sequence of nodes where each node contains data and a reference to the next node
There are two types of linked lists:
- Single linked lists (one-way links)
- Double linked lists (two-way links)
For Linked Lists: insertion and deletion can be (enter time complexity) at head or end of tail
O(1)
For linked lists: searching takes (enter time complexity) as you need to traverse the list
O(n)
Stacks
A stack is a collection of elements that follows the Last-In, First-Out principle (LIFO). You can only insert (push) or remove (pop) elements from the top of the stack
Stacks: push and pop operations both run in (insert time complexity)
O(1)
Queues
A queue is a collection of elements that follows the First-In, First-Out (FIFO) principal. Elements are inserted at the back (enqueue) and removed from the front (dequeue). Therefore, the first element is pulled out by dequeue.
Queues: both enqueue and dequeue run in (insert time complexity) in implementations like linked lists
O(1)
Trees
A tree is a hierarchical data structure with nodes connected by edges. The top node is called the root, and each node may have children. It is non-linear compared to arrays and linked lists.
Binary Trees
Binary Trees are trees in which each node has at most two children, referred to as the left child and the right child.
Binary Search Trees
Binary Search Trees (BST) is a type of binary tree in which the left child of a node contains values less than the node, and the right child contains values greater than the node. This allows for efficient searching, insertion, and deletion.
Binary trees have traversal operations with time complexity ____ (inorder, preorder, postorder)
O(n)