```
Multi-Dimensional Data
Applying Iterative Refinement in the Algorithm Design Process to handle multi-dimensional data.
Kevin Lin, with thanks to many others.
1
2-d Tree
The root node partitions entire space into left and right (x-coordinate).
All depth 1 nodes partition subspace into up and down (y-coordinate).
All depth 2 nodes partition subspace into left and right (x-coordinate).
All depth 3 nodes partition subspace into up and down (y-coordinate).
…
2
A
(2, 3)
B
(4, 2)
(4, 5)
C
D
(3, 3)
E
(1, 5)
F
(4, 4)
?: Does insertion order affect the balance of a k-d tree?
Which of these trees are valid 2-d trees?
3
L
R
D
U
(9, 4)
(7, 2)
(8, 0)
D
U
Q
L
R
L
R
(6, 8)
(5, 2)
(3, 7)
D
U
L
R
D
U
(5, 7)
(4, 6)
(9, 3)
L
R
Q1: Which of these trees are valid 2-d trees?
Which of these trees are valid 2-d trees?
4
L
R
D
U
(9, 4)
(7, 2)
(8, 0)
D
U
A
L
R
L
R
(6, 8)
(5, 2)
(3, 7)
D
U
L
R
D
U
(5, 7)
(4, 6)
(9, 3)
L
R
2-d Tree Insertion
Where would G go in the 2-d tree?
5
A
(2, 3)
B
(4, 2)
(4, 5)
C
D
(3, 3)
E
(1, 5)
F
(4, 4)
Q
G
(5, 3)
L
R
D
U
L
R
B (4, 2)
A (2, 3)
C (4, 5)
D (3, 3)
D
U
E (1, 5)
D
U
F (4, 4)
D
U
Q1: Where would G go in the 2-d tree?
2-d Tree Insertion
Where would G go in the 2-d tree?
6
A
(2, 3)
B
(4, 2)
(4, 5)
C
D
(3, 3)
E
(1, 5)
F
(4, 4)
A
G
(5, 3)
L
R
D
U
L
R
B (4, 2)
A (2, 3)
C (4, 5)
D (3, 3)
D
U
E (1, 5)
D
U
F (4, 4)
D
U
Without exploring A’s children, what is the closest possible point to (0, 7) that could exist in the subspace of A.rightChild?
Where would this node go in the 2-d tree?
7
Pruning the “Bad Side”
L
R
D
U
L
R
B (4, 2)
A (2, 3)
C (4, 5)
D (3, 3)
D
U
E (1, 5)
D
U
F (4, 4)
D
U
Q
Q1: Without exploring A’s children, what is the closest possible point to (0, 7) that could exist in the subspace of A.rightChild?
Q2: Where would this node go in the 2-d tree?
Without exploring A’s children, what is the closest possible point to (0, 7) that could exist in the subspace of A.rightChild?
Where would this node go in the 2-d tree?
8
Pruning the “Bad Side”
L
R
D
U
L
R
B (4, 2)
A (2, 3)
C (4, 5)
D (3, 3)
D
U
E (1, 5)
D
U
F (4, 4)
D
U
A
Give the order in which nodes are visited in the nearest neighbor search for (0, 7).
9
Nearest Neighbor Search
Q
A
(2, 3)
B
(4, 2)
(4, 5)
C
D
(3, 3)
E
(1, 5)
F
(4, 4)
G
(5, 3)
L
R
D
U
L
R
B (4, 2)
A (2, 3)
C (4, 5)
D (3, 3)
D
U
E (1, 5)
D
U
F (4, 4)
D
U
Q1: Give the order in which nodes are visited in the nearest neighbor search for (0, 7).
Give the order in which nodes are visited in the nearest neighbor search for (0, 7).
10
Nearest Neighbor Search
A
A
(2, 3)
B
(4, 2)
(4, 5)
C
D
(3, 3)
E
(1, 5)
F
(4, 4)
G
(5, 3)
L
R
D
U
L
R
B (4, 2)
A (2, 3)
C (4, 5)
D (3, 3)
D
U
E (1, 5)
D
U
F (4, 4)
D
U
```