Data structure cheat sheet: Difference between revisions

From Celeste@Hoppinglife
Jump to navigation Jump to search
Line 25: Line 25:
Heapify is a fundamental operation to keep the heap property when there is a new node inserted into the root.
Heapify is a fundamental operation to keep the heap property when there is a new node inserted into the root.


'''Complexity''': <math>O(lg n)</math>. You can prove that using recusion master theroem.
'''Complexity''': <math>\Theta(lg n)</math>. You can prove that using recusion master theroem.


==== Build a heap ====
==== Build a heap ====
Line 31: Line 31:
To build a heap, call <code>heapify()</code> n/2 times for all non-leaf nodes.
To build a heap, call <code>heapify()</code> n/2 times for all non-leaf nodes.


'''Complexity''': <math>O(n)</math>
'''Complexity''': <math>\Theta(n)</math>


==== Heapsort ====
==== Heapsort ====
Line 37: Line 37:
To sort an array in increasing order, build a max heap, put the first element to the final position of the array, and maintain the heap property by calling heapify with the remaining element.
To sort an array in increasing order, build a max heap, put the first element to the final position of the array, and maintain the heap property by calling heapify with the remaining element.


'''Complexity''':<math>O(n lgn)</math>
'''Complexity''':<math>\Theta(n lgn)</math>
 
==== Priority Queue ====
 
A (max) priority queue support four operations: insert, max, extract-max, increase-key. With a heap, these four operations can be done using <math>\Theta(lg n)</math>, <math>\Theta(1)</math>, <math>\Theta(lg n)</math>, <math>\Theta(lg n)</math> time.


==== Codes ====
==== Codes ====

Revision as of 16:59, 25 October 2020

A quick cheat sheet on common algorithms and data structures:

Linear Structures

Linked Lists

Basic Implementation

String

Related Algorithms

Arrays

Sorting

Tree-based Structures

Binary Tree

Property

Traversal

Heap

A (binary) heap is a complete binary tree that keeps a sepcific condition of its nodes: For a max-heap, A[parent[i]]>=A[i], for a min-heap, A[parent[i]]<=A[i]. A heap can be used to maintain a priority queue. A heap is often stored as a continous vector.

Heapify

Heapify is a fundamental operation to keep the heap property when there is a new node inserted into the root.

Complexity: Θ(lgn). You can prove that using recusion master theroem.

Build a heap

To build a heap, call heapify() n/2 times for all non-leaf nodes.

Complexity: Θ(n)

Heapsort

To sort an array in increasing order, build a max heap, put the first element to the final position of the array, and maintain the heap property by calling heapify with the remaining element.

Complexity:Θ(nlgn)

Priority Queue

A (max) priority queue support four operations: insert, max, extract-max, increase-key. With a heap, these four operations can be done using Θ(lgn), Θ(1), Θ(lgn), Θ(lgn) time.

Codes

void max_heapify(Node* A, int size, int start) {
    auto largest = start;
    if(start * 2 < size && A[largest] < A[start * 2])
        largest = start * 2;
    if(start * 2 + 1 < size && A[largest] < A[start * 2 + 1])
        largest = start * 2 + 1;
    if(largest != start) {
        swap(A[start], A[largest]);
        maxheapify(A, size, largest);
    }
}


void build_max_heap(Node* A, int size) {
  for(int i = size / 2; i >= 0; --i) {
    max_heapify(A, size, i);
  }
}

n-ary Tree

Union Find

Hashing Table

Graphs

Storage

Traverse

Properties

Algorithms

Dynamic Programming

Recursion

Searching