Teaching Kids Programming – Dynamic Programming Algorithms to Count the Number of Unique Binary Search Trees (Catalan Numbers)


Teaching Kids Programming: Videos on Data Structures and Algorithms

Given an integer n, return the number of structurally unique BST’s (binary search trees) which has exactly n nodes of unique values from 1 to n.

number-of-unique-bst Teaching Kids Programming - Dynamic Programming Algorithms to Count the Number of Unique Binary Search Trees (Catalan Numbers) algorithms dynamic programming math python teaching kids programming youtube video

Unique Binary Search Trees

Example 1:
Input: n = 3
Output: 5

Example 2:
Input: n = 1
Output: 1

Given N nodes, let’s say, the number of unique Binary Search Trees (BSTs) is G(N). Also, let’s use F(i, N) to represent the number of BSTs with N nodes but also use i as the root.

BST-unique-root Teaching Kids Programming - Dynamic Programming Algorithms to Count the Number of Unique Binary Search Trees (Catalan Numbers) algorithms dynamic programming math python teaching kids programming youtube video

cartesian product of the number of BST for its left and right subtrees, as illustrated below

Give i as the root, there is i-1 nodes on the left and n-i nodes on the right, which corresponds to G(i-1) and G(n-i)
tex_579e49943dca16f3fc2eafc5149de24c Teaching Kids Programming - Dynamic Programming Algorithms to Count the Number of Unique Binary Search Trees (Catalan Numbers) algorithms dynamic programming math python teaching kids programming youtube video

And, tex_e2a41d9fe2746ae4ca8d3c6918b4e48a Teaching Kids Programming - Dynamic Programming Algorithms to Count the Number of Unique Binary Search Trees (Catalan Numbers) algorithms dynamic programming math python teaching kids programming youtube video , thus:

tex_a0530f2d5fc4a9047507ea56ec975e91 Teaching Kids Programming - Dynamic Programming Algorithms to Count the Number of Unique Binary Search Trees (Catalan Numbers) algorithms dynamic programming math python teaching kids programming youtube video

And we know G[0] = G[1] = 1.

Also, if the nodes are label from 0 to n-1 instead. We can shift the value a bit:

tex_218c1944d55f70d09e8fbcc54646b752 Teaching Kids Programming - Dynamic Programming Algorithms to Count the Number of Unique Binary Search Trees (Catalan Numbers) algorithms dynamic programming math python teaching kids programming youtube video

Top Down Dynamic Programming Algorithm to Compute the Catalan Number

We can implement the above DP equation using Top Down, with the help of Recursion and Memoziation via @cache.

1
2
3
4
5
6
7
8
class Solution:
    def numTrees(self, n):
        @cache
        def G(n):
            if n == 0:
                return 1
            return sum(G(i)*G(n-i-1) for i in range(n))
        return G(n)
class Solution:
    def numTrees(self, n):
        @cache
        def G(n):
            if n == 0:
                return 1
            return sum(G(i)*G(n-i-1) for i in range(n))
        return G(n)

The time complexity is O(N^2) as for each G value we have to compute a loop. And the space complexity is O(N) as we are caching G(0), G(1) to G(N-1) i.e. N values.

Bottom Up Dynamic Programming Algorithm to Compute the Catalan Number

We can compute the G values from G[0], G[1] bottom up to G[n]. The values will be stored using a list/array.

1
2
3
4
5
6
7
8
class Solution:
    def numTrees(self, n):
        G = [0]*(n+1)
        G[0] = 1
        for i in range(1, n+1):
            for j in range(i):
                G[i] += G[j] * G[i-j-1]
        return G[n]
class Solution:
    def numTrees(self, n):
        G = [0]*(n+1)
        G[0] = 1
        for i in range(1, n+1):
            for j in range(i):
                G[i] += G[j] * G[i-j-1]
        return G[n]

The time complexity is O(N^2) and the space complexity is O(N).

Catalan Numbers

It turns out the answer is the N-th Catalan numbers.

See also: How to Compute the Catalan Number?

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
787 words
Last Post: Teaching Kids Programming - Finding the Network Delay Time via Floyd-Warshall Shortest Path Algorithm
Next Post: Teaching Kids Programming - Equal Tree Partition via Recursive Depth First Search Algorithm

The Permanent URL is: Teaching Kids Programming – Dynamic Programming Algorithms to Count the Number of Unique Binary Search Trees (Catalan Numbers)

Leave a Reply