Data Structures Flashcards
Arrays, Linked List, Hash Table, Stack, Queue, Heap, Binary Tree
array
a collection of data elements, linear order is given by their physical placement in memory
array: index (get)
O(1)
array: insert/remove at the end
amortized O(1) (for dynamically resizing array)
array: insert/remove at any position
O(n)
array: insert/remove at the beginning
O(n)
linked list
a collection of data elements not stored in adjacent physical memory. a group of nodes representing a sequence. each node is composed of data element itself and a pointer to the next node in the sequence.
linked list: insert/remove at any position
O(1)
linked list: index (get)
O(n)
binary tree
a tree with each node having at most two children
use cases of binary tree
- a way to access nodes based on value or label associated with each node. e.g. BST, binary heap (searching and sorting)
- as a representation of data with a relevant bifurcating structure where the particular arrangement of nodes under and/or to the left or right of other nodes is part of the information (that is, changing it would change the meaning).
balanced tree
|height(left) - height(right)| <= 1 for any node
height = O(log n)
binary tree: insert a leaf
O(1)
binary tree: remove a zero/single child node
O(1)
traversal of binary trees
pre-order, in-order, post-order
breath-first order
depth-first order
implementation of binary trees
nodes and pointers
binary search tree
sorted binary tree, node.val is greater or equal than all the nodes in the left subtree, node.val is less or equal than all the nodes in the right subtree
binary search tree: height
balanced bst: O(log n); any bst: O(n)
binary search tree: search
average O(log n), worst case O(n)
binary search tree: insert
average O(log n), worst case O(n)
binary search tree: remove
average O(log n), worst case O(n)
self-balancing binary search tree
AVL and black-red tree
AVL
Re-balance the tree by performing appropriate rotations on the subtree