Without using ML libraries, implement k-Nearest Neighbors for classification. Requirements: (a) Support Euclidean and cosine distances; (b) Allow tie-breaking via class priors; (c) Analyze training time, query time, and memory complexity; (d) Extend to weighted voting with distance decay; (e) Discuss how you would scale queries to millions of points (e.g., KD-tree limitations in high dimensions, approximate methods).