From 2aa05e6d32484f740fb36d374511c00a524b5796 Mon Sep 17 00:00:00 2001 From: jhonDoe15 <62393042+jhonDoe15@users.noreply.github.com> Date: Mon, 11 Oct 2021 20:35:27 +0300 Subject: [PATCH 1/8] added impl for algorithm and base objects for algorithm --- DataStructures/Trees/nearestRightPoint.java | 126 ++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 DataStructures/Trees/nearestRightPoint.java diff --git a/DataStructures/Trees/nearestRightPoint.java b/DataStructures/Trees/nearestRightPoint.java new file mode 100644 index 000000000000..8742e513b571 --- /dev/null +++ b/DataStructures/Trees/nearestRightPoint.java @@ -0,0 +1,126 @@ + +import java.util.Scanner; +import java.util.concurrent.ThreadLocalRandom; + +public class Main { + + public static void main(String[] args) { + Tree root = BuildTree(); + Scanner sc = new Scanner(System.in); + System.out.print("Enter first number: "); + int inputX0 = sc.nextInt(); + Point toPrint = nearestRightPoint(root, inputX0); + System.out.println("X: " + toPrint.x + " Y: " + toPrint.y); + } + + public static Tree BuildTree() { + int randomX = ThreadLocalRandom.current().nextInt(0, 100 + 1); + int randomY = ThreadLocalRandom.current().nextInt(0, 100 + 1); + Tree root = new Tree(null, null, new Point(randomX, randomY)); + + for (int i = 0; i < 1000; i++) { + randomX = ThreadLocalRandom.current().nextInt(0, 100 + 1); + randomY = ThreadLocalRandom.current().nextInt(0, 100 + 1); + root = root.insertPoint(root, new Point(randomX, randomY)); + } + + Tree.print2D(root); + + return root; + } + + public static Point nearestRightPoint(Tree root, int x0) { + //Check whether tree is empty + if(root == null){ + return new Point(0,0); + } + else { + if(root.data.x - x0 > 0){ + // Go left + Point temp = nearestRightPoint(root.left, x0); + if(temp.x == 0 && temp.y == 0){ + return root.data; + } + return temp; + } else { + // Go right + return nearestRightPoint(root.right, x0); + } + + } + } + +} + + +public class Tree { + + public Tree left; + public Tree right; + public Point data; + + public Tree(Point p) { + this.left = null; + this.right = null; + this.data = p; + } + + public Tree(Tree right, Tree left, Point point) { + this.left = left; + this.right = right; + this.data = point; + } + + public Tree insertPoint(Tree current, Point value) { + if (current == null) { + return new Tree(value); + } + + if (value.x < current.data.x) { + current.left = insertPoint(current.left, value); + } else if (value.x > current.data.x) { + current.right = insertPoint(current.right, value); + } + + return current; + + } + + static final int COUNT = 10; + + static void print2DUtil(Tree root, int space) + { + if (root == null) + return; + + space += COUNT; + + print2DUtil(root.right, space); + + System.out.print("\n"); + for (int i = COUNT; i < space; i++) + System.out.print(" "); + System.out.print(root.data.x + "\n"); + + print2DUtil(root.left, space); + } + + public static void print2D(Tree root) + { + print2DUtil(root, 0); + } +} + +public class Point { + public int x; + public int y; + + public Point(){ + this.x = 0; + this.y = 0; + } + public Point(int x, int y){ + this.x = x; + this.y = y; + } +} From 21551332416da1242a46cfdd8e0576212b7284b0 Mon Sep 17 00:00:00 2001 From: jhonDoe15 <62393042+jhonDoe15@users.noreply.github.com> Date: Sat, 16 Oct 2021 20:10:29 +0300 Subject: [PATCH 2/8] localize class access to file --- DataStructures/Trees/nearestRightPoint.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/DataStructures/Trees/nearestRightPoint.java b/DataStructures/Trees/nearestRightPoint.java index 8742e513b571..6cc947e2f97a 100644 --- a/DataStructures/Trees/nearestRightPoint.java +++ b/DataStructures/Trees/nearestRightPoint.java @@ -2,7 +2,7 @@ import java.util.Scanner; import java.util.concurrent.ThreadLocalRandom; -public class Main { +class Main { public static void main(String[] args) { Tree root = BuildTree(); @@ -53,7 +53,7 @@ public static Point nearestRightPoint(Tree root, int x0) { } -public class Tree { +class Tree { public Tree left; public Tree right; @@ -111,7 +111,7 @@ public static void print2D(Tree root) } } -public class Point { +class Point { public int x; public int y; From 44259d3e8fe16a4ef2d44b993a55cce7eb73bb29 Mon Sep 17 00:00:00 2001 From: jhonDoe15 <62393042+jhonDoe15@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:23:43 +0300 Subject: [PATCH 3/8] point to int transform usage from x, y point coordinates to plane integer values --- DataStructures/Trees/nearestRightPoint.java | 56 ++++++++------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/DataStructures/Trees/nearestRightPoint.java b/DataStructures/Trees/nearestRightPoint.java index 6cc947e2f97a..0fbeb0e136af 100644 --- a/DataStructures/Trees/nearestRightPoint.java +++ b/DataStructures/Trees/nearestRightPoint.java @@ -9,19 +9,17 @@ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); int inputX0 = sc.nextInt(); - Point toPrint = nearestRightPoint(root, inputX0); - System.out.println("X: " + toPrint.x + " Y: " + toPrint.y); + int toPrint = nearestRightKey(root, inputX0); + System.out.println("Key: " + toPrint); } public static Tree BuildTree() { int randomX = ThreadLocalRandom.current().nextInt(0, 100 + 1); - int randomY = ThreadLocalRandom.current().nextInt(0, 100 + 1); - Tree root = new Tree(null, null, new Point(randomX, randomY)); + Tree root = new Tree(null, null, randomX); for (int i = 0; i < 1000; i++) { randomX = ThreadLocalRandom.current().nextInt(0, 100 + 1); - randomY = ThreadLocalRandom.current().nextInt(0, 100 + 1); - root = root.insertPoint(root, new Point(randomX, randomY)); + root = root.insertKey(root, randomX); } Tree.print2D(root); @@ -29,22 +27,22 @@ public static Tree BuildTree() { return root; } - public static Point nearestRightPoint(Tree root, int x0) { + public static int nearestRightKey(Tree root, int x0) { //Check whether tree is empty if(root == null){ - return new Point(0,0); + return 0; } else { - if(root.data.x - x0 > 0){ + if(root.data - x0 > 0){ // Go left - Point temp = nearestRightPoint(root.left, x0); - if(temp.x == 0 && temp.y == 0){ + int temp = nearestRightKey(root.left, x0); + if(temp == 0){ return root.data; } return temp; } else { // Go right - return nearestRightPoint(root.right, x0); + return nearestRightKey(root.right, x0); } } @@ -57,29 +55,29 @@ class Tree { public Tree left; public Tree right; - public Point data; + public int data; - public Tree(Point p) { + public Tree(int x) { this.left = null; this.right = null; - this.data = p; + this.data = x; } - public Tree(Tree right, Tree left, Point point) { + public Tree(Tree right, Tree left, int x) { this.left = left; this.right = right; - this.data = point; + this.data = x; } - public Tree insertPoint(Tree current, Point value) { + public Tree insertKey(Tree current, int x) { if (current == null) { return new Tree(value); } - if (value.x < current.data.x) { - current.left = insertPoint(current.left, value); + if (value < current.data) { + current.left = insertKey(current.left, value); } else if (value.x > current.data.x) { - current.right = insertPoint(current.right, value); + current.right = insertKey(current.right, value); } return current; @@ -100,7 +98,7 @@ static void print2DUtil(Tree root, int space) System.out.print("\n"); for (int i = COUNT; i < space; i++) System.out.print(" "); - System.out.print(root.data.x + "\n"); + System.out.print(root.data + "\n"); print2DUtil(root.left, space); } @@ -110,17 +108,3 @@ public static void print2D(Tree root) print2DUtil(root, 0); } } - -class Point { - public int x; - public int y; - - public Point(){ - this.x = 0; - this.y = 0; - } - public Point(int x, int y){ - this.x = x; - this.y = y; - } -} From ea261a3ca91d65e4beebf7a0f633cbc0ace07145 Mon Sep 17 00:00:00 2001 From: jhonDoe15 <62393042+jhonDoe15@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:25:26 +0300 Subject: [PATCH 4/8] change filename --- .../Trees/{nearestRightPoint.java => nearestRightKey.java} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename DataStructures/Trees/{nearestRightPoint.java => nearestRightKey.java} (100%) diff --git a/DataStructures/Trees/nearestRightPoint.java b/DataStructures/Trees/nearestRightKey.java similarity index 100% rename from DataStructures/Trees/nearestRightPoint.java rename to DataStructures/Trees/nearestRightKey.java From 9402b42f368ddfa7a6547d3a18e352e62c922ddf Mon Sep 17 00:00:00 2001 From: jhonDoe15 <62393042+jhonDoe15@users.noreply.github.com> Date: Sun, 24 Oct 2021 11:27:00 +0300 Subject: [PATCH 5/8] fix missed changes --- DataStructures/Trees/nearestRightKey.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/DataStructures/Trees/nearestRightKey.java b/DataStructures/Trees/nearestRightKey.java index 0fbeb0e136af..ac53cf9ce300 100644 --- a/DataStructures/Trees/nearestRightKey.java +++ b/DataStructures/Trees/nearestRightKey.java @@ -69,14 +69,14 @@ public Tree(Tree right, Tree left, int x) { this.data = x; } - public Tree insertKey(Tree current, int x) { + public Tree insertKey(Tree current, int value) { if (current == null) { return new Tree(value); } if (value < current.data) { current.left = insertKey(current.left, value); - } else if (value.x > current.data.x) { + } else if (value > current.data) { current.right = insertKey(current.right, value); } From 32fed832475d9bf265935e9f55f418ad39c0eda5 Mon Sep 17 00:00:00 2001 From: jhonDoe15 <62393042+jhonDoe15@users.noreply.github.com> Date: Mon, 25 Oct 2021 09:42:44 +0300 Subject: [PATCH 6/8] add package --- DataStructures/Trees/nearestRightKey.java | 1 + 1 file changed, 1 insertion(+) diff --git a/DataStructures/Trees/nearestRightKey.java b/DataStructures/Trees/nearestRightKey.java index ac53cf9ce300..060c37628d86 100644 --- a/DataStructures/Trees/nearestRightKey.java +++ b/DataStructures/Trees/nearestRightKey.java @@ -1,3 +1,4 @@ +package DataStructures.Trees; import java.util.Scanner; import java.util.concurrent.ThreadLocalRandom; From 1636d413e2ebef08f35c03f9b10f48c3bc01e2c7 Mon Sep 17 00:00:00 2001 From: jhonDoe15 <62393042+jhonDoe15@users.noreply.github.com> Date: Mon, 25 Oct 2021 09:46:47 +0300 Subject: [PATCH 7/8] remove tree printing method and usage --- DataStructures/Trees/nearestRightKey.java | 27 ----------------------- 1 file changed, 27 deletions(-) diff --git a/DataStructures/Trees/nearestRightKey.java b/DataStructures/Trees/nearestRightKey.java index 060c37628d86..dc2eea7cb784 100644 --- a/DataStructures/Trees/nearestRightKey.java +++ b/DataStructures/Trees/nearestRightKey.java @@ -23,8 +23,6 @@ public static Tree BuildTree() { root = root.insertKey(root, randomX); } - Tree.print2D(root); - return root; } @@ -82,30 +80,5 @@ public Tree insertKey(Tree current, int value) { } return current; - - } - - static final int COUNT = 10; - - static void print2DUtil(Tree root, int space) - { - if (root == null) - return; - - space += COUNT; - - print2DUtil(root.right, space); - - System.out.print("\n"); - for (int i = COUNT; i < space; i++) - System.out.print(" "); - System.out.print(root.data + "\n"); - - print2DUtil(root.left, space); - } - - public static void print2D(Tree root) - { - print2DUtil(root, 0); } } From 16e76bf398b231f7e7bf55ca28612ee479f51c07 Mon Sep 17 00:00:00 2001 From: jhonDoe15 <62393042+jhonDoe15@users.noreply.github.com> Date: Mon, 25 Oct 2021 09:51:05 +0300 Subject: [PATCH 8/8] transform from regular binary Tree to NRKTree NRKTree - NearestRightKeyTree - the regular tree lacks functionality required for this algorithm to function, specifically in the inserting method --- DataStructures/Trees/nearestRightKey.java | 24 +++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/DataStructures/Trees/nearestRightKey.java b/DataStructures/Trees/nearestRightKey.java index dc2eea7cb784..228bf470d809 100644 --- a/DataStructures/Trees/nearestRightKey.java +++ b/DataStructures/Trees/nearestRightKey.java @@ -1,4 +1,4 @@ -package DataStructures.Trees; +package DataStructures.NRKTrees; import java.util.Scanner; import java.util.concurrent.ThreadLocalRandom; @@ -6,7 +6,7 @@ class Main { public static void main(String[] args) { - Tree root = BuildTree(); + NRKTree root = BuildTree(); Scanner sc = new Scanner(System.in); System.out.print("Enter first number: "); int inputX0 = sc.nextInt(); @@ -14,9 +14,9 @@ public static void main(String[] args) { System.out.println("Key: " + toPrint); } - public static Tree BuildTree() { + public static NRKTree BuildTree() { int randomX = ThreadLocalRandom.current().nextInt(0, 100 + 1); - Tree root = new Tree(null, null, randomX); + NRKTree root = new NRKTree(null, null, randomX); for (int i = 0; i < 1000; i++) { randomX = ThreadLocalRandom.current().nextInt(0, 100 + 1); @@ -26,7 +26,7 @@ public static Tree BuildTree() { return root; } - public static int nearestRightKey(Tree root, int x0) { + public static int nearestRightKey(NRKTree root, int x0) { //Check whether tree is empty if(root == null){ return 0; @@ -50,27 +50,27 @@ public static int nearestRightKey(Tree root, int x0) { } -class Tree { +class NRKTree { - public Tree left; - public Tree right; + public NRKTree left; + public NRKTree right; public int data; - public Tree(int x) { + public NRKTree(int x) { this.left = null; this.right = null; this.data = x; } - public Tree(Tree right, Tree left, int x) { + public NRKTree(NRKTree right, NRKTree left, int x) { this.left = left; this.right = right; this.data = x; } - public Tree insertKey(Tree current, int value) { + public NRKTree insertKey(NRKTree current, int value) { if (current == null) { - return new Tree(value); + return new NRKTree(value); } if (value < current.data) {