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 byN! C(N)
, whereC(N)
is theN
th Catalan number. AsymptoticallyC(N) ∼ 4N / [N3/2 sqrt π]
.
❔
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 theN
th Catalan numberC(N)
. AsymptoticallyC(N) ∼ 4N / [N3/2 sqrt π]
. The average height of a randomly built binary search tree wuthN
nodes isO(log N)
.
🎥
- Binary search trees – MIT OCW 6.006: Introduction to algorithms (2011)
📖
- 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)
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 takesO(log N)
worst-case time, and ordered enumeration of all nodes takesO(N)
time. - Self-balancing binary search trees can be used to implement sorted associative containers. For example,
std::set
andstd::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.
🔗
- Self-balancing binary search tree – Wikipedia
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 betweenlog2(N + 1)
and1.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.
🔗
- AVL tree – Wikipedia
- C++ AVL tree template
- AVL trees: Tutorial and C++ implementation
- Simple AVL tree in C++
- AVL tree implementation in C++
📖
- Sec. 6.2.3: Balanced trees – D.E.Knuth. The art of computer programming. Vol. 3: Sorting and searching (1998)
- Sec. 4.4: AVL trees – M.A.Weiss. Data structures and algorithm analysis in C++ (2014)
- Sec. 4.3.4: AVL trees – U.Manber. Introduction to algorithms: A creative approach (1989)
📄
- G.M.Adelson-Velskiĭ, E.M.Landis. An algorithm for organization of information. In english, In russian – Doklady Akademii Nauk SSSR 146, 263 (1962)
🎥
- AVL trees, AVL sort – MIT OCW 6.006: Introduction to algorithms (2011)
💫 Visualizations
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. Both0
-based and1
-based indexing can be used in equally elegant ways. Applications: arithmetic coding, Monte–Carlo simulations.
🔗
- Fenwick tree – Wikipedia
- Binary indexed trees – Topcoder
- Fenwick tree – Brilliant
- A JavaScript implementation of binary indexed tree – Microsoft @ GitHub
❔
🎥
- Fenwick trees – Algorithms Live!
- W.Fiset. Fenwick tree/Binary indexed tree
📖
- Sec. 2.4.4: Binary indexed (Fenwick) tree – S.Halim, F.Halim. Competitive programming (2013)
📄
- P.M.Fenwick. A new data structure for cumulative frequency tables – Software: Practice and Experience 24, 327 (1994)
- Sec. 7.2.1.6: Generating all trees – D.E.Knuth. The art of computer programming. Vol. 4A: Combinatorial algorithms, Part 1 (2011)
🔗
- E.Mäkinen Generating random binary trees – a survey (1998)
- D.W.Schwartz. An efficient method for optimizing binary trees – C/C++ Users Journal 11 (1993)
❔
- Advantage of B+ trees over BSTs? – Stack Overflow
🎥
- 2-3 Trees and B-Trees – MIT OCW 6.046J: Design and analysis of algorithms (2015)
📖
- Ch. 18: B-trees – T.H.Cormen, C.E.Leiserson, R.L.Rivest, C.Stein. Introduction to algorithms (2009)
🎥
- E.Demaine. Augmentation: Range trees – MIT OCW 6.046J: Design and analysis of algorithms (2015)
🔗
- Y.Dandass. Interval trees – C/C++ Users Journal 18 (2000)
🔗
- J.W.M.Stevens. Lexical analysis using search tries – C/C++ Users Journal 10 (1992)
See also: Traversal – Graphs
🔗
- Tree traversal – Wikipedia
🔗
- OpenVDB
- K.Museth. OpenVDB: An open source data structure and toolkit for high-resolution volumes – BIDS (2015)