Basics Flashcards
What are the two types of data structure? Give examples.
linear structures and hierarchical structures. Arrays, linked lists, stacks, and queues are linear structures, while trees, graphs, heaps etc. are hierarchical structures.
What is a heap?
Heap is a binary tree that stores a collection of keys by satisfying heap property. Max heap and min heap are two flavors of heap data structure. The heap property for max heap is: each node should be greater than or equal to each of its children. While, for min heap it is: each node should be smaller than or equal to each of its children. Heap data structure is usually used to implement priority queues.
What is a hash table?
Hash Table is again a data structure that stores data in form of key-element pairs. A key is a non-null value which is mapped to an element. And, the element is accessed on the basis of the key associated with it. Hash table is a useful data structure for implementing dictionary.
What are linked lists and what are they good / bad at?
Stores data with nodes that point to other nodes.
Nodes, at its most basic it has one datum and one reference (another node). Designed to optimize insertion and deletion, slow at indexing and searching.
Big O of Linked Lists
Indexing: Linked Lists: O(n)
Search: Linked Lists: O(n)
Optimized Search: Linked Lists: O(n)
Insertion: Linked Lists: O(1)
Big O of Arrays
Indexing: Linear array: O(1), Dynamic array: O(1)
Search: Linear array: O(n), Dynamic array: O(n)
Optimized Search: Linear array: O(log n), Dynamic array: O(log n)
Insertion: Linear array: n/a Dynamic array: O(n)
What is the hash used for?
Designed to optimize searching, insertion, and deletion.
Indexing: Hash Tables: O(1)
Search: Hash Tables: O(1)
Insertion: Hash Tables: O(1)
What are hash collisions?
Hash collisions are when a hash function returns the same output for two distinct outputs.
What is a degenerate tree?
An unbalanced tree, which if entirely one-sided is a essentially a linked list.
What are binary trees and what are they good at?
Designed to optimize searching and sorting.
Indexing: Binary Search Tree: O(log n)
Search: Binary Search Tree: O(log n)
Insertion: Binary Search Tree: O(log n)
Is a tree like data structure where every node has at most two children. left and right nodes.
What is Breadth First Search?
An algorithm that searches a tree (or graph) by searching levels of the tree first, starting at the root.
It finds every node on the same level, most often moving left to right.
While doing this it tracks the children nodes of the nodes on the current level.
When finished examining a level it moves to the left most node on the next level.
What data structure is used when performing a BFS?
Queue
What is Depth First Search?
An algorithm that searches a tree (or graph) by searching depth of the tree first, starting at the root.
It traverses left down a tree until it cannot go further.
Once it reaches the end of a branch it traverses back up trying the right child of nodes on that branch, and if possible left from the right children.
When finished examining a branch it moves to the node right of the root then tries to go left on all it’s children until it reaches the bottom.
What data structure is used when performing a DFS?
Stack
BFS vs DFS
For wide, shallow trees use Breadth First Search
For deep, narrow trees use Depth First Search