Skip to content

Latest commit

 

History

History
205 lines (120 loc) · 9.98 KB

File metadata and controls

205 lines (120 loc) · 9.98 KB

Trees

Table of contents


Binary trees

A binary tree is a tree in which no node can have more than two children. The total number of binary trees with N different keys is given by N! C(N), where C(N) is the Nth Catalan number. Asymptotically C(N) ∼ 4N / [N3/2 sqrt π].

Binary search trees

A binary search tree is a binary tree that satisfies the binary search property: the key in each node must be greater than or equal to any key stored in the left subtree, and less than or equal to any key stored in the right subtree. The total number of binary search trees with N different keys is given by the Nth Catalan number C(N). Asymptotically C(N) ∼ 4N / [N3/2 sqrt π]. The average height of a randomly built binary search tree wuth N nodes is O(log N).

🎥

📖

  • Sec. 2.3.1: Binary search trees, Sec: Selection sort – E.Horowitz, S.Sahni, S.Rajasekaran. Computer algorithms – W.H.Freeman (1997)
  • Sec. 13.3: Binary search trees – J.Bentley. Programming pearls (1999)

Self-balancing binary search trees

A self-balancing binary search tree is a binary search tree that automatically keeps its height small during arbitrary insertions and deletions.

📝

  • For a self-balancing binary search tree containing N nodes, lookup, insertion, and removal of an item takes O(log N) worst-case time, and ordered enumeration of all nodes takes O(N) time.
  • Self-balancing binary search trees can be used to implement sorted associative containers. For example, std::set and std::map are typically implemented using red-black trees.
  • Self-balancing binary search can be used to count the number of inversions in an array and the number of smaller/larger elements on the right/left side of each element in an array.

🔗

AVL (Adelson-Velsky–Landis) trees

An AVL tree is a self-balancing binary search tree in which for every node the height of the left and right subtrees can differ by at most one.

📝

  • The height of an AVL tree with N nodes lies between log2(N + 1) and 1.4404 log2(N + 2) - 0.3277.
  • For insertions, one rotation always suffices. For deletions, O(log N) rotations may be required. Non-recursive insertions and deletions are generally faster, but harded to code and read.
  • Double rotations can be made more efficient if performed as a single step and not as two single rotations.
  • Balance factors, i.e. differences in heights, can be stored instead of heights. This results in faster but more complicated code. If a balance factor is stored as a separate data member, there is no profit in the amount of space used due to data alignment.
  • AVL trees require at most 45% more comparisons than optimal binary search trees.

🔗

📖

📄

  • G.M.Adelson-Velskiĭ, E.M.Landis. An algorithm for organization of information. In english, In russian – Doklady Akademii Nauk SSSR 146, 263 (1962)

🎥

💫 Visualizations

Other binary trees

Binary indexed trees (Fenwick trees)

A binary indexed tree (Fenwick tree) is a data structure that can efficiently update elements and calculate prefix sums. A Fenwick tree can be built for any cancellative semigroup (e.g. for the set of integers under addition or multiplication); if the cancellation property doesn't hold (e.g. for min(•, •)), a segment tree can be used. Both 0-based and 1-based indexing can be used in equally elegant ways. Applications: arithmetic coding, Monte–Carlo simulations.

🔗

🎥

📖

📄

Generation of binary trees

Random binary trees

🔗

Optimization of binary trees


B/B+-trees

🎥

📖


Range trees

🎥


Interval trees

🔗


Tries

🔗


Traversal

See also: Traversal – Graphs

🔗


Volumetric trees

OpenVDB

🔗