From 811422cbbf6664154ecb8cd2404a0ebd4b6c663a Mon Sep 17 00:00:00 2001 From: Shreya Mishra <143825321+shrexxs@users.noreply.github.com> Date: Fri, 18 Oct 2024 22:23:04 +0530 Subject: [PATCH 1/2] Created K Dimensional tree --- K Dimensional tree | 100 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 K Dimensional tree diff --git a/K Dimensional tree b/K Dimensional tree new file mode 100644 index 000000000..1a73d857a --- /dev/null +++ b/K Dimensional tree @@ -0,0 +1,100 @@ +import java.util.*; + +class Node { + int[] point; + Node left, right; + + public Node(int[] arr) + { + this.point = arr; + this.left = this.right = null; + } +} + +class KDtree { + int k = 2; + + public Node newNode(int[] arr) { return new Node(arr); } + + public Node insertRec(Node root, int[] point, int depth) + { + if (root == null) { + return newNode(point); + } + + int cd = depth % k; + if (point[cd] < root.point[cd]) { + root.left + = insertRec(root.left, point, depth + 1); + } + else { + root.right + = insertRec(root.right, point, depth + 1); + } + + return root; + } + + public Node insert(Node root, int[] point) + { + return insertRec(root, point, 0); + } + + public boolean arePointsSame(int[] point1, int[] point2) + { + for (int i = 0; i < k; ++i) { + if (point1[i] != point2[i]) { + return false; + } + } + return true; + } + + public boolean searchRec(Node root, int[] point, + int depth) + { + if (root == null) { + return false; + } + if (arePointsSame(root.point, point)) { + return true; + } + + int cd = depth % k; + if (point[cd] < root.point[cd]) { + return searchRec(root.left, point, depth + 1); + } + return searchRec(root.right, point, depth + 1); + } + + public boolean search(Node root, int[] point) + { + return searchRec(root, point, 0); + } + + public static void main(String[] args) + { + KDtree kdTree = new KDtree(); + + Node root = null; + int[][] points + = { { 3, 6 }, { 17, 15 }, { 13, 15 }, { 6, 12 }, + { 9, 1 }, { 2, 7 }, { 10, 19 } }; + + int n = points.length; + + for (int i = 0; i < n; i++) { + root = kdTree.insert(root, points[i]); + } + + int[] point1 = { 10, 19 }; + System.out.println(kdTree.search(root, point1) + ? "Found" + : "Not Found"); + + int[] point2 = { 12, 19 }; + System.out.println(kdTree.search(root, point2) + ? "Found" + : "Not Found"); + } +} From edabfd0779da6173d790b34ace5a1c9e256fb7ee Mon Sep 17 00:00:00 2001 From: Shreya Mishra <143825321+shrexxs@users.noreply.github.com> Date: Fri, 18 Oct 2024 22:51:29 +0530 Subject: [PATCH 2/2] Update and rename K Dimensional to java/K-Dimensional_tree.java --- K Dimensional tree => java/K-Dimensional_tree.java | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) rename K Dimensional tree => java/K-Dimensional_tree.java (81%) diff --git a/K Dimensional tree b/java/K-Dimensional_tree.java similarity index 81% rename from K Dimensional tree rename to java/K-Dimensional_tree.java index 1a73d857a..8e8d2764f 100644 --- a/K Dimensional tree +++ b/java/K-Dimensional_tree.java @@ -50,8 +50,7 @@ public boolean arePointsSame(int[] point1, int[] point2) return true; } - public boolean searchRec(Node root, int[] point, - int depth) + public boolean searchRec(Node root, int[] point, int depth) { if (root == null) { return false; @@ -78,8 +77,7 @@ public static void main(String[] args) Node root = null; int[][] points - = { { 3, 6 }, { 17, 15 }, { 13, 15 }, { 6, 12 }, - { 9, 1 }, { 2, 7 }, { 10, 19 } }; + = { { 3, 6 }, { 17, 15 }, { 13, 15 }, { 6, 12 },{ 9, 1 }, { 2, 7 }, { 10, 19 } }; int n = points.length; @@ -88,13 +86,9 @@ public static void main(String[] args) } int[] point1 = { 10, 19 }; - System.out.println(kdTree.search(root, point1) - ? "Found" - : "Not Found"); + System.out.println(kdTree.search(root, point1)? "Found": "Not Found"); int[] point2 = { 12, 19 }; - System.out.println(kdTree.search(root, point2) - ? "Found" - : "Not Found"); + System.out.println(kdTree.search(root, point2)? "Found": "Not Found"); } }