whoashish's cp notes

Home

❯

12. Trees

12. Trees

Oct 08, 20262 min read

  • i. Tree Basics, Diameter and Center: A tree is a connected acyclic graph.
  • ii. Tree Isomorphism and Prüfer Codes: Rooted trees are compared by canonical forms built bottom-up (AHU); unrooted trees are rooted at their centre(s) first.
  • iii. Lowest Common Ancestor: The deepest common ancestor of u and v.
  • iv. Euler Tour, Subtree and Path Queries: Flatten the tree in DFS preorder:
  • v. Heavy-Light and Long-Path Decomposition: struct HLD {
  • vi. Centroid Decomposition: Remove a centroid, recurse on the remaining components.
  • vii. Small to Large (DSU on Tree): Answer a query about every subtree by keeping one global structure:
  • viii. Virtual Tree: For a set S of k marked vertices, the virtual (auxiliary) tree keeps S plus the LCAs needed to preserve their ancestor structure:
  • ix. Kruskal Reconstruction Tree: Run Kruskal, but whenever an edge of weight w joins two components, create a new node with weight w whose children are the two components’ current tree nodes.
  • x. Link-Cut Tree: A forest under link and cut with path aggregates, path updates, rerooting, LCA and subtree sizes, each in O(logn) amortized (Sleator–Tarjan).
  • xi. Euler Tour Tree: Keep the Euler tour of every tree of a forest in a balanced sequence (a treap with parent pointers).
  • xii. Top Trees: A top tree represents a tree as a hierarchy of clusters (connected edge sets with at most two boundary vertices), built from single edges by two merges:

12 items under this folder.

  • Oct 08, 2026

    i. Tree Basics, Diameter and Center

    • trees
    • diameter
    • center
    • centroid
  • Oct 08, 2026

    ii. Tree Isomorphism and Prüfer Codes

    • trees
    • isomorphism
    • hashing
    • prufer
    • cayley
  • Oct 08, 2026

    iii. Lowest Common Ancestor

    • trees
    • lca
    • binary-lifting
    • level-ancestor
  • Oct 08, 2026

    iv. Euler Tour, Subtree and Path Queries

    • trees
    • euler-tour
    • subtree-queries
    • path-queries
    • fenwick
  • Oct 08, 2026

    v. Heavy-Light and Long-Path Decomposition

    • trees
    • hld
    • long-path
    • path-queries
  • Oct 08, 2026

    vi. Centroid Decomposition

    • trees
    • centroid-decomposition
    • divide-and-conquer
    • paths
  • Oct 08, 2026

    vii. Small to Large (DSU on Tree)

    • trees
    • small-to-large
    • dsu-on-tree
    • sack
    • offline
  • Oct 08, 2026

    viii. Virtual Tree

    • trees
    • virtual-tree
    • auxiliary-tree
    • lca
  • Oct 08, 2026

    ix. Kruskal Reconstruction Tree

    • trees
    • kruskal
    • krt
    • bottleneck
    • dsu
  • Oct 08, 2026

    x. Link-Cut Tree

    • trees
    • link-cut-tree
    • dynamic-trees
    • splay
  • Oct 08, 2026

    xi. Euler Tour Tree

    • trees
    • euler-tour-tree
    • dynamic-trees
    • treap
  • Oct 08, 2026

    xii. Top Trees

    • trees
    • top-tree
    • static-top-tree
    • dynamic-trees
    • clusters