Binary Search Trees
Binary Search Trees
B C
Left subtree
Right subtree
D E F G
Edges H I Leaves
J K L M N
Binary Trees
• Binary tree: Each node has at most 2 children (branching factor 2)
• Binary tree is
• A root (with data)
• A left subtree (may be empty)
• A right subtree (may be empty)
• Special Cases:
(Last week’s practice) What does the following method do?
int height(Node root){
if (root == null),
return -1;
return 1 + max(height(root.left),
height(root.right);
}
B C
• In-order: left subtree, root, right subtree
D E F
• Post-order: left subtree, right subtree, root
G
Binary Search Tree (BST) Data Structure
• Structure property (binary tree) 8
• Each node has 2 children
• Result: keeps operations simple
5 11
• Order property
2 6 10 12
4 7 9 14
• Result: straight-forward to find any given value
13