Definition of Tree
- A tree is a set of linked nodes with one unique root node.
- There is exactly one path from the root to any other node.
- Path: A chain of pointers/references from node A to node B.
- No cycles (circles of pointers) in a tree.
Example of a Tree
A
/
C B
/
D E
/
F G
- Root: A (no parent).
- Leaves: B, E, F, G (no children).
- Edges: Links between nodes.
Binary Trees
- Each node has up to two children.
- Not a Binary Tree: If any node has more than two children.
Example of a Binary Tree
A
/
B C
/
I K
/
E J
Levels
- Level 0: Root (A).
- Level 1: B, C.
- Level 2: I, K.
- Level 3: E, J.
Full Binary Tree
- All nodes except leaves have two children.
- Example:
B
/
D A
/ \ /
H I E J
Complete Binary Tree
- A full binary tree OR missing only the rightmost nodes on the last level.
- Not Complete: Missing non-rightmost nodes on the last level.
Binary Search Trees (BST)
- Properties:
- Left child key < Parent key.
- Right child key ≥ Parent key.
- Node Structure (Java):
class BSTNode { int element; BSTNode left; // Left child BSTNode right; // Right child }
Insertion
-
Algorithm (
insertElement):-
Start at root.
-
Traverse left if new key < current node, else right.
-
Insert at null position.
-
Algorithm insertElement (p, e)
Input: e is the element to be inserted under node p, without rotation.
Output: if e is inserted as the left child of p if e is less than p; else e is inserted as the left child of p
if p == null
p.element = e
else if e < p.element
p.left = insertElement (p.left, e)
else
p.right = insertElement (p.right, e);
return p
Example Insertion Sequence:
37, 2, 45, 48, 41, 29, 20, 30, 49, 7
37
/ \
2 45
\ / \
29 41 48
/ \ \
20 30 49
/
7
Searching
-
Algorithm (
findElement):-
Traverse left if target < current node, else right.
-
Return
NO_SUCH_KEYif null reached.
-
Example Search for 29:
-
29 < 37 → left to 2.
-
29 > 2 → right to 29.
-
Found.
Time Complexity:
-
Balanced BST: O(log n).
-
Unbalanced BST: O(n).
Balance Factor
-
Definition:
Height(right subtree) - Height(left subtree). -
Balanced BST (AVL): All nodes have balance factor -1, 0, or +1.
-
Unbalanced BST: Any node outside [-1, 1].
Example Balance Factors
37 (0)
/ \
2 (-1) 45 (+3)
\ / \
29 41 48 (+1)
/ \ \
20 30 49 (0)
/
7 (0)
- Node
2is unbalanced (+2).
Rotations (Rebalancing)
Types:
-
Single Rotation:
- Left or Right.
-
Double Rotation:
- Left-Right or Right-Left.
Example Rotation:
-
Right-Left Double Rotation at node
2:-
Rotate left at
29. -
Rotate right at
2.
-
Deletion
Cases:
-
No Children: Remove node.
-
One Child: Splice out node, link parent to child.
-
Two Children:
-
Find greatest node in left subtree.
-
Copy its value to the target node.
-
Delete the copied node.
-
Example Deletion of 37:
-
Greatest in left subtree:
30. -
Copy
30to37. -
Delete original
30.
Time Complexity:
-
Balanced BST: O(log n).
-
Unbalanced BST: O(n).
Traversal
-
Pre-Order: Parent → Left → Right.
- Example:
37, 2, 29, 20, 7, 30, 45, 41, 48, 49.
- Example:
-
In-Order: Left → Parent → Right (sorted).
- Example:
2, 7, 20, 29, 30, 37, 41, 45, 48, 49.
- Example:
-
Post-Order: Left → Right → Parent.
- Example:
7, 20, 30, 29, 2, 41, 49, 48, 45, 37.
- Example:
Algorithms:
// Pre-Order
void preOrder(BSTNode p) {
if (p != null) {
visit(p);
preOrder(p.left);
preOrder(p.right);
}
}
// In-Order
void inOrder(BSTNode p) {
if (p != null) {
inOrder(p.left);
visit(p);
inOrder(p.right);
}
}
// Post-Order
void postOrder(BSTNode p) {
if (p != null) {
postOrder(p.left);
postOrder(p.right);
visit(p);
}
}AVL vs Hash Table
AVL Advantages:
-
Sorted output via in-order traversal.
-
Min/Max in O(log n) time.
Hash Table Advantages:
-
Faster search/insertion (O(1)) with good hash function.
-
No sorting support.
Example:
-
For
n = 500,000:-
AVL: ~19 comparisons (worst case).
-
Hash Table: 1 comparison (ideal).
-