TREES
Types of Trees:
1. Full Binary tree: each node has 0 or 2 children
2. Complete Binary tree: All levels are completely filled except possibly the last
level and the last level has all nodes as left as possible
3. Perfect Binary tree: All leaf nodes are at the same node
4. Balanced Binary tree: Height of tree is max log(N)
5. Degenerate Binary tree: Skewed trees
6. Binary Tree: At most 2 nodes
7. Binary Search Tree: A binary tree where nodes of left sub tree are less than
root, and nodes of right subtree are greater than root, and lst and rst must
also be BSTs.
BFS/Level Order Traversal: Explores all nodes on the present depth level
before moving on to next depth level. (RPA-remove,print,add)
[Link]
page=1&category=Tree&sortBy=submissions
BFS of n-ary tree(lc-429)
DFS - Preorder
Maximum Depth (lc 104) (no of nodes from root to farthest leaf node)
Count Leaf Nodes
import [Link].*;
class Solution {
class Node {
char ch; // Store character
int data;
Node left, right;
Node(char ch, int data) {
[Link] = ch;
[Link] = data;
[Link] = [Link] = null;
}
}
public ArrayList<String> huffmanCodes(String S, int f[], int n) {
PriorityQueue<Node> q = new PriorityQueue<>(new
Comparator<Node>() {
public int compare(Node n1, Node n2) {
if ([Link] == [Link]) return 1; // Keeps order if
frequencies are equal
return [Link]([Link], [Link]);
}
});
// Insert all characters with their frequencies
for (int i = 0; i < n; i++) {
[Link](new Node([Link](i), f[i]));
}
// Build Huffman Tree
while ([Link]() > 1) {
Node first = [Link]();
Node second = [Link]();
Node merged = new Node('-', [Link] + [Link]); //
Merged node
[Link] = first;
[Link] = second;
[Link](merged);
}
// Store the result in an array list
ArrayList<String> result = new ArrayList<>();
Node root = [Link]();
generateHuffmanCodes(root, "", result);
return result;
}
// Preorder traversal to generate Huffman codes
public void generateHuffmanCodes(Node root, String s,
ArrayList<String> result) {
if (root == null) return;
if ([Link] == null && [Link] == null) {
[Link](s);
}
generateHuffmanCodes([Link], s + "0", result);
generateHuffmanCodes([Link], s + "1", result);
}
// Function to calculate frequency of each character
public Map<Character, Integer> calculateFrequency(String S) {
Map<Character, Integer> freqMap = new HashMap<>();
for (char ch : [Link]()) {
[Link](ch, [Link](ch, 0) + 1);
}
return freqMap;
}
// Helper function to call huffmanCodes with calculated frequency
public ArrayList<String> getHuffmanCodes(String S) {
Map<Character, Integer> freqMap = calculateFrequency(S);
int n = [Link]();
char[] chars = new char[n];
int[] freq = new int[n];
int index = 0;
for ([Link]<Character, Integer> entry :
[Link]()) {
chars[index] = [Link]();
freq[index] = [Link]();
index++;
}
return huffmanCodes(new String(chars), freq, n);
// **Main method to test the implementation**
public static void main(String[] args) {
Solution sol = new Solution();
String S =
"aaaaabbbbbbbbbccccccccccccdddddddddddddeeeeeeeeeeeeeeeeefffffffffffffffffff
ffffffffffffffffffffff";
ArrayList<String> huffmanCodes = [Link](S);
[Link]("Huffman Codes:");
for (String code : huffmanCodes) {
[Link](code);
Anonymous class implements Comparator interface, and this anonymous class
overrides int compare();