forked from aswinkumarrk/data-structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUniqueBinarySearchTree_CatalanNumber.java
More file actions
93 lines (81 loc) · 3.3 KB
/
UniqueBinarySearchTree_CatalanNumber.java
File metadata and controls
93 lines (81 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package com.geeksforgeeks.dynamicProgramming;
import com.util.LogUtil;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Count Total Unique Binary Search Trees - The nth Catalan Number (Dynamic Programming)
* <p>
* <p>
* Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n?
* <p>
* Example:
* <p>
* Input: 3
* Output: 5
* Explanation:
* Given n = 3, there are a total of 5 unique BST's:
* <p>
* * 1 3 3 2 1
* * \ / / / \ \
* * 3 2 1 1 3 2
* * / / \ \
* * 2 1 2 3
*
* @author neeraj on 2019-05-11
* Copyright (c) 2019, data-structures.
* All rights reserved.
*/
public class UniqueBinarySearchTree_CatalanNumber {
public static void main(String[] args) {
countUniqueBinarySearchTreeWith(3);
printPermutation("abc");
printPermutation("boat");
}
public static void countUniqueBinarySearchTreeWith(int number) {
int[] numberOfUniqueBST = new int[number + 1];
// Base Case 1
// When we have no nodes, i.e number = 0;
numberOfUniqueBST[0] = 1; // We only have 1 unique BST i.e EMPTY_TREE
// Base Case 2
// When we have just 1 node.
numberOfUniqueBST[1] = 1; // With 1 node we can just put it in root and have only 1 unique BST
// Now starting from Number of Nodes = 2 to the input number, we will calculate unique BSt
for (int i = 2; i <= number; i++) {
// Now we can put 1....to...the...number (i) of nodes
// alternatively on the root and can check unique BST
// Summation of all the unique ways
for (int j = 1; j <= i; j++) {
// G(i) = F(i-1)(Left Tree) * F(number - i)(Right Tree);
numberOfUniqueBST[i] += numberOfUniqueBST[j - 1] * numberOfUniqueBST[i - j];
}
}
LogUtil.logIt("Number of Unique BST with n = " + number + " is " + numberOfUniqueBST[number]);
}
public static void printPermutation(String str) {
Map<Character, Long> frequencyMap = calculateFrequency(str);
permuteInternally(frequencyMap, str, 0, new char[str.length()]);
}
private static void permuteInternally(Map<Character, Long> frequencyMap, String str, int currentLevel, char[] result) {
if (currentLevel == str.length()) {
System.out.println(String.valueOf(result));
return;
}
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (frequencyMap.get(c) == 0L) {
continue;
}
frequencyMap.put(c, frequencyMap.get(c) - 1l);
result[currentLevel] = c;
permuteInternally(frequencyMap, str, currentLevel + 1, result);
frequencyMap.put(c, frequencyMap.get(c) + 1l);
}
}
private static Map<Character, Long> calculateFrequency(String str) {
return str
.chars() // Convert to IntStream
.mapToObj(character -> (char) character) // Getting all the characters
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting())); // Applying grouping and counting the frequency
}
}