Java Notes
Complete guide to trees — Binary Tree, BST, AVL Tree, traversals, operations, balancing, and classic tree problems with Java implementations.
What is a Tree?
A tree is a hierarchical data structure with nodes connected by edges. Unlike linear structures (arrays, linked lists), trees represent hierarchical relationships.
| - Root | node 1 (top, no parent) |
| - Leaf | nodes 5, 6, 7, 8 (no children) |
| - Height | 3 (longest root-to-leaf path) |
| - Depth of node 4 | 2 (distance from root) |
| - Degree of node 2 | 2 (number of children) |
Tree Traversals
// Inorder (Left → Root → Right) — gives sorted order for BST
public void inorder(TreeNode node) {
if (node == null) return;
inorder(node.left);
System.out.print(node.val + " ");
inorder(node.right);
}
// Result for above BST: 1, 3, 4, 6, 7, 8, 10, 13, 14
// Preorder (Root → Left → Right) — useful for copying tree
public void preorder(TreeNode node) {
if (node == null) return;
System.out.print(node.val + " ");
preorder(node.left);
preorder(node.right);
}
// Postorder (Left → Right → Root) — useful for deletion
public void postorder(TreeNode node) {
if (node == null) return;
postorder(node.left);
postorder(node.right);
System.out.print(node.val + " ");
}
// Level-order (BFS)
public void levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode node = queue.poll();
System.out.print(node.val + " ");
if (node.left != null) queue.offer(node.left);
if (node.right != null) queue.offer(node.right);
}
}Classic Tree Problems
BST Complexity
| Operation | Average | Worst (skewed) | Balanced (AVL) |
|---|---|---|---|
| Search | O(log n) | O(n) | O(log n) |
| Insert | O(log n) | O(n) | O(log n) |
| Delete | O(log n) | O(n) | O(log n) |
Interview Questions
Q1: What is the difference between a Binary Tree and BST?
Answer: Binary Tree: each node has at most 2 children (no ordering). BST: additionally, left subtree values < node < right subtree values. This ordering enables O(log n) search.
Q2: How do you find the kth smallest element in a BST?
Answer: Inorder traversal visits nodes in sorted order. Either traverse and count to k (O(k + h)), or augment nodes with subtree sizes for O(h) access using rank.
Q3: How do you serialize and deserialize a binary tree?
Answer: Serialize with preorder traversal using null markers (e.g., "1,2,null,null,3,4,null,null,5,null,null"). Deserialize by reading values sequentially and recursively building left/right subtrees.
Q4: What happens when you insert sorted data into a BST?
Answer: The tree degenerates into a linked list (all nodes go right). All operations become O(n). Solution: use self-balancing BSTs (AVL, Red-Black) which maintain O(log n) height.
Q5: What is the time complexity of building a BST from n elements?
Answer: O(n log n) average (each of n insertions takes O(log n)). Worst case O(n²) if data is sorted. Building a balanced BST from sorted array takes O(n) by recursively choosing the middle element.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Tree Data Structure in Java.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java Master Course topic.
Search Terms
java-master-course, java master course, java, master, course, dsa, tree, tree data structure in java
Related Java Master Course Topics