Data structure cheat sheet: Difference between revisions

From Celeste@Hoppinglife
Jump to navigation Jump to search
Line 23: Line 23:
==== Heapify ====
==== Heapify ====


<syntaxhighlight>
<syntaxhighlight lang='cpp'>
heapify(A)
void maxheapify(Node* array, 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);
    }
}
</syntaxhighlight>
</syntaxhighlight>



Revision as of 16:35, 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.

Heapify

void maxheapify(Node* array, 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);
    }
}

n-ary Tree

Union Find

Hashing Table

Graphs

Storage

Traverse

Properties

Algorithms

Dynamic Programming

Recursion

Searching