cp's OEIS Frontend

This is a front-end for the Online Encyclopedia of Integer Sequences, made by Christian Perfect. The idea is to provide OEIS entries in non-ancient HTML, and then to think about how they're presented visually. The source code is on GitHub.

Showing 1-8 of 8 results.

A195581 Number T(n,k) of permutations of {1,2,...,n} that result in a binary search tree of height k; triangle T(n,k), n>=0, 0<=k<=n, read by rows.

Original entry on oeis.org

1, 0, 1, 0, 0, 2, 0, 0, 2, 4, 0, 0, 0, 16, 8, 0, 0, 0, 40, 64, 16, 0, 0, 0, 80, 400, 208, 32, 0, 0, 0, 80, 2240, 2048, 608, 64, 0, 0, 0, 0, 11360, 18816, 8352, 1664, 128, 0, 0, 0, 0, 55040, 168768, 104448, 30016, 4352, 256, 0, 0, 0, 0, 253440, 1508032, 1277568, 479040, 99200, 11008, 512
Offset: 0

Views

Author

Alois P. Heinz, Sep 20 2011

Keywords

Comments

Empty external nodes are counted in determining the height of a search tree.

Examples

			T(3,3) = 4, because 4 permutations of {1,2,3} result in a binary search tree of height 3:
  (1,2,3):   1       (1,3,2):   1     (3,1,2):   3   (3,2,1):   3
            / \                / \              / \            / \
           o   2              o   3            1   o          2   o
              / \                / \          / \            / \
             o   3              2   o        o   2          1   o
                / \            / \              / \        / \
               o   o          o   o            o   o      o   o
Triangle T(n,k) begins:
  1;
  0, 1;
  0, 0, 2;
  0, 0, 2,  4;
  0, 0, 0, 16,      8;
  0, 0, 0, 40,     64,      16;
  0, 0, 0, 80,    400,     208,      32;
  0, 0, 0, 80,   2240,    2048,     608,     64;
  0, 0, 0,  0,  11360,   18816,    8352,   1664,   128;
  0, 0, 0,  0,  55040,  168768,  104448,  30016,  4352,   256;
  0, 0, 0,  0, 253440, 1508032, 1277568, 479040, 99200, 11008, 512;
  ...
		

Crossrefs

Row sums give A000142. Column sums give A227822.
Main diagonal gives A011782, lower diagonal gives A076616.
T(n,A000523(n)+1) = A076615(n).
T(2^n-1,n) = A056972(n).
T(2n,n) = A265846(n).
Cf. A195582, A195583, A244108 (the same read by columns), A316944, A317012.

Programs

  • Maple
    b:= proc(n, k) option remember; `if`(n<2, `if`(k b(n, k)-b(n, k-1):
    seq(seq(T(n, k), k=0..n), n=0..10);
  • Mathematica
    b[n_, k_] := b[n, k] = If[n == 0, 1, If[n == 1, If[k > 0, 1, 0], Sum[Binomial[n-1, r-1]*b[r-1, k-1]*b[n-r, k-1], {r, 1, n}] ] ]; t [n_, k_] := b[n, k] - If[k > 0, b[n, k-1], 0]; Table[Table[t[n, k], {k, 0, n}], {n, 0, 10}] // Flatten (* Jean-François Alcover, Dec 17 2013, translated from Maple *)

Formula

Sum_{k=0..n} k * T(n,k) = A316944(n).
Sum_{k=n..2^n-1} k * T(k,n) = A317012(n).

A244108 Number T(n,k) of permutations of {1,2,...,n} that result in a binary search tree of height k; triangle T(n,k), k>=0, k<=n<=2^k-1, read by columns.

Original entry on oeis.org

1, 1, 2, 2, 4, 16, 40, 80, 80, 8, 64, 400, 2240, 11360, 55040, 253440, 1056000, 3801600, 10982400, 21964800, 21964800, 16, 208, 2048, 18816, 168768, 1508032, 13501312, 121362560, 1099169280, 10049994240, 92644597760, 857213660160, 7907423180800, 72155129446400
Offset: 0

Views

Author

Alois P. Heinz, Dec 21 2015

Keywords

Comments

Empty external nodes are counted in determining the height of a search tree.

Examples

			Triangle T(n,k) begins:
: 1;
:    1;
:       2;
:       2,  4;
:          16,      8;
:          40,     64,      16;
:          80,    400,     208,      32;
:          80,   2240,    2048,     608,     64;
:               11360,   18816,    8352,   1664,   128;
:               55040,  168768,  104448,  30016,  4352,   256;
:              253440, 1508032, 1277568, 479040, 99200, 11008, 512;
		

Crossrefs

Row sums give A000142.
Column sums give A227822.
Main diagonal gives A011782, lower diagonal gives A076616.
T(n,A000523(n)+1) = A076615(n).
T(2^n-1,n) = A056972(n).
T(2n,n) = A265846(n).
Cf. A195581 (the same read by rows), A195582, A195583, A316944, A317012.

Programs

  • Maple
    b:= proc(n, k) option remember; `if`(n<2, `if`(k b(n, k)-b(n, k-1):
    seq(seq(T(n, k), n=k..2^k-1), k=0..5);
  • Mathematica
    b[n_, k_] := b[n, k] = If[n<2, If[kJean-François Alcover, Feb 19 2017, translated from Maple *)

Formula

Sum_{k=0..n} k * T(n,k) = A316944(n).
Sum_{k=n..2^n-1} k * T(k,n) = A317012(n).

A076616 Number of permutations of {1,2,...,n} that result in a binary search tree (when elements of the permutation are inserted in that order) of height n-1 (i.e., the second largest possible height).

Original entry on oeis.org

0, 0, 0, 2, 16, 64, 208, 608, 1664, 4352, 11008, 27136, 65536, 155648, 364544, 843776, 1933312, 4390912, 9895936, 22151168, 49283072, 109051904, 240123904, 526385152, 1149239296, 2499805184, 5419040768, 11710496768, 25232932864, 54223962112, 116232552448
Offset: 0

Views

Author

Jeffrey Shallit, Oct 22 2002

Keywords

Examples

			a(3) = 2 because only the permutations (2,1,3) and (2,3,1) result in a search tree of height 2 (notice we count empty external nodes in determining the height). The largest such trees are of height 3.
		

Crossrefs

Lower diagonal of A195581 or of A244108.

Programs

  • Maple
    a:= n-> max(-(<<0|1|0>, <0|0|1>, <8|-12|6>>^n. <<1/2, 1, 1>>)[1$2], 0):
    seq(a(n), n=0..40);  # Alois P. Heinz, Sep 20 2011
  • Mathematica
    Join[{0, 0, 0}, LinearRecurrence[{6, -12, 8}, {2, 16, 64}, 40]] (* Jean-François Alcover, Jan 09 2025 *)
  • PARI
    concat(vector(3), Vec(2*x^3*(1+2*x-4*x^2)/(1-2*x)^3 + O(x^50))) \\ Colin Barker, May 16 2016

Formula

G.f.: 2*(4*x^2-2*x-1)*x^3/(2*x-1)^3. - Alois P. Heinz, Sep 20 2011
From Colin Barker, May 16 2016: (Start)
a(n) = 2^(n-3)*(n^2-n-4) for n>2.
a(n) = 6*a(n-1)-12*a(n-2)+8*a(n-3) for n>5.
(End)
From Alois P. Heinz, May 31 2022: (Start)
a(n) = 2 * A100312(n-3) for n>=3.
a(n) = 16 * A049611(n-3) = 16 * A084851(n-4) for n>=4. (End)

Extensions

More terms from Alois P. Heinz, Sep 20 2011

A335919 Number T(n,k) of binary search trees of height k having n internal nodes; triangle T(n,k), n>=0, max(0,floor(log_2(n))+1)<=k<=n, read by rows.

Original entry on oeis.org

1, 1, 2, 1, 4, 6, 8, 6, 20, 16, 4, 40, 56, 32, 1, 68, 152, 144, 64, 94, 376, 480, 352, 128, 114, 844, 1440, 1376, 832, 256, 116, 1744, 4056, 4736, 3712, 1920, 512, 94, 3340, 10856, 15248, 14272, 9600, 4352, 1024, 60, 5976, 27672, 47104, 50784, 40576, 24064
Offset: 0

Views

Author

Alois P. Heinz, Jun 29 2020

Keywords

Comments

Empty external nodes are counted in determining the height of a search tree.
T(n,k) is defined for n,k >= 0. The triangle contains only the positive terms. Terms not shown are zero.

Examples

			Triangle T(n,k) begins:
  1;
     1;
        2;
        1, 4;
           6,   8;
           6,  20,   16;
           4,  40,   56,   32;
           1,  68,  152,  144,   64;
               94,  376,  480,  352,  128;
              114,  844, 1440, 1376,  832,  256;
              116, 1744, 4056, 4736, 3712, 1920, 512;
  ...
		

Crossrefs

Row sums give A000108.
Column sums give A001699.
Main diagonal gives A011782.
T(n+3,n+2) gives A014480.
T(n,max(0,A000523(n)+1)) = A328349(n).
Cf. A073345, A073429 (another version with 0's), A076615, A195581, A244108, A335920 (the same read by columns), A335921, A335922.

Programs

  • Maple
    g:= n-> `if`(n=0, 0, ilog2(n)+1):
    b:= proc(n, h) option remember; `if`(n=0, 1, `if`(n<2^h,
          add(b(j-1, h-1)*b(n-j, h-1), j=1..n), 0))
        end:
    T:= (n, k)-> b(n, k)-`if`(k>0, b(n, k-1), 0):
    seq(seq(T(n, k), k=g(n)..n), n=0..12);
  • Mathematica
    g[n_] := If[n == 0, 0, Floor@Log[2, n]+1];
    b[n_, h_] := b[n, h] = If[n == 0, 1, If[n < 2^h,
         Sum[b[j - 1, h - 1]*b[n - j, h - 1], {j, 1, n}], 0]];
    T[n_, k_] := b[n, k] - If[k > 0, b[n, k - 1], 0];
    Table[Table[T[n, k], {k, g[n], n}], {n, 0, 12}] // Flatten (* Jean-François Alcover, Feb 08 2021, after Alois P. Heinz *)

Formula

Sum_{k=0..n} k * T(n,k) = A335921(n).
Sum_{n=k..2^k-1} n * T(n,k) = A335922(k).

A335920 Number T(n,k) of binary search trees of height k having n internal nodes; triangle T(n,k), k>=0, k<=n<=2^k-1, read by columns.

Original entry on oeis.org

1, 1, 2, 1, 4, 6, 6, 4, 1, 8, 20, 40, 68, 94, 114, 116, 94, 60, 28, 8, 1, 16, 56, 152, 376, 844, 1744, 3340, 5976, 10040, 15856, 23460, 32398, 41658, 49700, 54746, 55308, 50788, 41944, 30782, 19788, 10948, 5096, 1932, 568, 120, 16, 1, 32, 144, 480, 1440, 4056
Offset: 0

Views

Author

Alois P. Heinz, Jun 29 2020

Keywords

Comments

Empty external nodes are counted in determining the height of a search tree.
T(n,k) is defined for n,k >= 0. The triangle contains only the positive terms. Terms not shown are zero.

Examples

			Triangle T(n,k) begins:
  1;
     1;
        2;
        1, 4;
           6,   8;
           6,  20,   16;
           4,  40,   56,   32;
           1,  68,  152,  144,   64;
               94,  376,  480,  352,  128;
              114,  844, 1440, 1376,  832,  256;
              116, 1744, 4056, 4736, 3712, 1920, 512;
  ...
		

Crossrefs

Row sums give A000108.
Column sums give A001699.
Main diagonal gives A011782.
T(n+3,n+2) gives A014480.
T(n,max(0,A000523(n)+1)) = A328349(n).
Cf. A073345, A076615, A195581, A244108, A335919 (the same read by rows), A335921, A335922.

Programs

  • Maple
    b:= proc(n, h) option remember; `if`(n=0, 1, `if`(n<2^h,
          add(b(j-1, h-1)*b(n-j, h-1), j=1..n), 0))
        end:
    T:= (n, k)-> b(n, k)-`if`(k>0, b(n, k-1), 0):
    seq(seq(T(n, k), n=k..2^k-1), k=0..6);
  • Mathematica
    b[n_, h_] := b[n, h] = If[n == 0, 1, If[n < 2^h,
         Sum[b[j - 1, h - 1]*b[n - j, h - 1], {j, 1, n}], 0]];
    T[n_, k_] := b[n, k] - If[k > 0, b[n, k - 1], 0];
    Table[Table[T[n, k], {n, k, 2^k - 1}], {k, 0, 6}] // Flatten (* Jean-François Alcover, Feb 08 2021, after Alois P. Heinz *)

Formula

Sum_{k=0..n} k * T(n,k) = A335921(n).
Sum_{n=k..2^k-1} n * T(n,k) = A335922(k).

A328349 Number of binary search trees (BST) on n nodes which have the same height as the optimal BST.

Original entry on oeis.org

1, 1, 2, 1, 6, 6, 4, 1, 94, 114, 116, 94, 60, 28, 8, 1, 32398, 41658, 49700, 54746, 55308, 50788, 41944, 30782, 19788, 10948, 5096, 1932, 568, 120, 16, 1, 5193067630, 6939692682, 8948546308, 11120136162, 13299362332, 15286065700, 16859410792, 17813777994, 17999433372
Offset: 0

Views

Author

Viktar Karatchenia, Oct 13 2019

Keywords

Comments

For any BST node p, p.left.value < p.value and p.right.value > p.value provided that the left (right) node exists.

Examples

			a(1) = 1 - the trivial tree having 1 node.
a(2) = 2 - two trees: one {1,2} rooted at node 1, and {2,1} - at 2.
a(3) = 1 - one BST tree of height 1 with edges {1,2}, {2,3} rooted at the node 2.
      2
    1   3
a(4) = 6. Optimal BST height is 2. When the root is 1, the remaining nodes {2,3,4} can form 1 subtree having height 1. Taking 2 as the root, 1 must go to the left, and {3,4} - right; there can be 2 trees on {3,4}. The cases for root 4,3 are symmetric. Thus, a(4) = 2*(1+1*2) = 6. The 6 BSTs of the optimal height 2 are: ({1,3},{3,2},{3,4}), ({2,1},{2,3},{3,4}), ({2,1},{2,4},{4,3}), ({3,4},{3,2},{2,1}), ({3,4},{3,1},{1,2}), ({4,2},{2,1},{2,3}).
a(5) = 1+1+2*2 = 6. Height=2. Nodes 1,5 cannot be the root, because the remaining 4 nodes cannot be compressed into a BST of height 1. Node 2 as the root implies {3,4,5} must follow to the right (left) producing 1 tree. Node 4 similarly adds 1 more BST. Finally, 3 as the root allows the formation of 2 trees consisting of {1,2} to the left of the root, and 2 trees of {4,5} to the right giving 2*2 = 4 trees.
a(6) = 4. Here height = 2. The nodes 1,2,5,6 can't be the root. When rooting at the third node, {1,2} will form 2 trees, and {4,5,6} to the right will make 1 possible tree of height 1. The node 4 is similar to 3. In total, a(6) = 2*1*2 = 4.
		

Crossrefs

Programs

  • Julia
    # after C++ program
    const _cache = Dict{Tuple{Int, Int}, Int}()
    function f(k::Int, level::Int)
        (k >= 1 << level) && return 0
        level = min(k, level)
        haskey(_cache, (k, level)) && return _cache[(k, level)]
        r = 0
        for root in 1:k
            left  = root == 1 ? 1 : f(root - 1, level - 1)
            right = root == k ? 1 : f(k - root, level - 1)
            r += left * right
        end
        return _cache[k, level] = r
    end
    BinaryIntegerLength(n) = n == 0 ? 1 : floor(Int, log2(n)) + 1
    [f(n, BinaryIntegerLength(n)) for n in 1:40] |> println # Peter Luschny, Oct 14 2019
  • Maple
    b:= proc(n, h) option remember; `if`(n=0, 1, `if`(n<2^h,
          add(b(j-1, h-1)*b(n-j, h-1), j=1..n), 0))
        end:
    a:= n-> b(n, ilog2(n)+1):
    seq(a(n), n=0..42);  # Alois P. Heinz, Jun 28 2020
  • Mathematica
    b[n_, h_] := b[n, h] = If[n == 0, 1, If[n<2^h, Sum[b[j-1, h-1] b[n-j, h-1], {j, 1, n}], 0]];
    a[n_] := b[n, Floor@Log[2, n]+1];
    a /@ Range[0, 42] (* Jean-François Alcover, Nov 15 2020, after Alois P. Heinz *)

Formula

a(n) = 1 when n = 2^m - 1, m > 0 because the optimal BST represents a full binary tree thus precisely 1 tree is possible.
a(n) = 2^(m-1) when n = 2^m - 2, m > 1. Here the BST is full BST minus 1 node, which must be at the last level. The last level of a full BST has 2^(m-1) nodes. Once the "missing" node is chosen at the last level, there is only 1 BST.
a(n) = f(n, 1+floor(log_2(n))) where f(k, level) is 0 when there are too many nodes (k >= 2^ level), otherwise f(k, level) = Sum_{root=1..n}(root=1 ? 1 : f(root-1, level-1)) * (root=k ? 1 : f(k-root, level-1))).
a(n) = A335919(n,max(0,A000523(n)+1)) = A335920(n,max(0,A000523(n)+1)). - Alois P. Heinz, Jun 29 2020

Extensions

a(0)=1 prepended by Alois P. Heinz, Jun 28 2020

A242461 Decimal expansion of the first positive solution to exp(1-1/x)/x = 1/2, a binary search tree constant.

Original entry on oeis.org

3, 7, 3, 3, 6, 4, 6, 1, 7, 7, 0, 1, 6, 7, 4, 0, 8, 4, 2, 4, 8, 4, 4, 8, 4, 3, 6, 6, 7, 9, 2, 7, 0, 5, 9, 5, 0, 0, 2, 5, 7, 6, 4, 6, 7, 0, 0, 4, 2, 7, 7, 3, 8, 4, 4, 4, 4, 9, 3, 8, 5, 7, 0, 3, 1, 5, 1, 3, 0, 5, 6, 5, 5, 1, 3, 3, 5, 3, 3, 3, 5, 5, 8, 8, 8, 1, 6, 9, 8, 8, 9, 0, 6, 5, 0, 3, 8, 8, 6, 8
Offset: 0

Views

Author

Jean-François Alcover, May 15 2014

Keywords

Comments

The saturation level S_n of a binary search tree defined by a random n-permutation is such that S_n/log(n) converges to 0.3733646... in probability.

Examples

			0.373364617701674084248448436679270595...
		

References

  • Steven R. Finch, Mathematical Constants, Cambridge University Press, 2003, pp. 349-352.

Crossrefs

Programs

  • Mathematica
    RealDigits[-1/ProductLog[-1, -1/(2*E)], 10, 100] // First

Formula

-1/W(-1, -1/(2*e)) where W is the Lambert W function (ProductLog).

A363197 a(n) is the number of ways the labels 1 to 2^n-1 can be assigned to a perfect binary tree with n levels such that there is an ordering between children and parents and also an ordering between the left and the right child.

Original entry on oeis.org

1, 1, 10, 343200, 73082837755699200000, 79548797573848497198355214730517854838277265162240000000000
Offset: 1

Views

Author

Thomas Scheuerle, May 21 2023

Keywords

Comments

We choose one order relation like left > right, parent < child and keep this relation the same while counting all variants which will fit this relation.
Number of permutations {1,2,...,2^n-1} which generate a binary search tree with minimum possible height such that each parent receives the left child first.

Examples

			The 10 variants for a(3) are:
    1          1          1
   / \        / \        / \
  5   2      4   2      4   2
 / \ / \    / \ / \    / \ / \
7  6 4  3  7  5 6  3  7  6 5  3
.
    1          1          1
   / \        / \        / \
  4   2      3   2      3   2
 / \ / \    / \ / \    / \ / \
6  5 7  3  5  4 7  6  7  4 6  5
.
    1          1          1
   / \        / \        / \
  3   2      3   2      3   2
 / \ / \    / \ / \    / \ / \
7  5 6  4  7  6 5  4  6  4 7  5
.
    1
   / \
  3   2
 / \ / \
6  5 7  4
.
		

Crossrefs

Programs

  • Mathematica
    RecurrenceTable[{a[n + 1] == Binomial[2^(n + 1) - 2, 2^n - 1]*2^((n^2 - 3*n)/2)*a[n]^2, a[1] == 1}, a, {n, 1, 6}] (* Amiram Eldar, May 21 2023 *)
  • PARI
    a(n) = if(n==0,1,binomial(2^n-2, 2^(n-1)-1)*2^((4 - 5*n + n^2)/2)*a(n-1)^2)

Formula

a(n) = binomial(2^n - 2, 2^(n-1) - 1)*2^((4 - 5*n + n^2)/2)*a(n-1)^2.
a(n) = A076615(2^n - 1) / 2^(n*(n - 1)/2).
Showing 1-8 of 8 results.