Tree

Abstract Data Type (ADT)

A mathematical model for data types:

  • How to store data

  • What query to support? (lookup, findMin, findSum, …)

  • What update to support? (insertion, deletion, filter, multi_insert, delete_min, …)

  • Algorithms for the operations

Examples of ADT

  • FIFO Queue

  • Deque (double-ended queue)

  • Stack

  • Priority queue

  • Ordered set/map

  • Unordered set/map

Winning Tree

  • An implementation of priority queue

  • Easy to implement, same bound as binary heap

  • Can also be used as a static version of search tree

Similar concepts

  • Also Winner Tree, Tournament Tree

  • Segment Tree (w/o insert, delete operations).

  • Fenwick tree or binary indexed tree (only used to efficiently calculate prefix sums)

Use case (as priority queue)

  • Huffman coding: need extract_min and insertion

  • Dijkstra's algorithm and Prim's algorithm: extract_min and add/update

Implementation

  • store all elements at the leaves (N nodes)

  • each internal node is a competition (N-1 nodes)

  • insertion, deletion, update operations at O(log n) time; the bound is tight

  • can also use an array to store it

    • two children of A[i]: A[2i] and A[2i+1] (start from 1 can make it easier, but can also start from 0)

    • a complete binary tree

  • a good alternative to binary heap

    • same asymptotical bound for insertion/deletion/update/extract_min

    • easy to implement

    • takes more space, and is slightly slower in practice

    • when combining with augmentation, can be used to implement almost all data structures

References

Augmented trees

  • Range-related queries: 1D range max/min/sum

    • Extract-Min is a special range query (on the entire range)

  • We can augment the winning tree according to designated tasks

    • store different field for different query

    • often augment multiple fields at the same time

  • If we pre-sort all elements or the key range is fixed (e.g., entire range)

    • winning tree can be used as a static search tree

  • For floating-point range query, we can discretize it and map it to integers

Example Code: Greedy & Winner Tree (code to be improved)

Last updated

Was this helpful?