Skip to content

Commit 7b7e4aa

Browse files
authored
Merge branch 'master' into anuska
2 parents b549dbc + b031a0b commit 7b7e4aa

File tree

9 files changed

+1253
-3
lines changed

9 files changed

+1253
-3
lines changed

pmd-exclude.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,14 @@ com.thealgorithms.others.LinearCongruentialGenerator=UselessMainMethod
8888
com.thealgorithms.others.Luhn=UnnecessaryFullyQualifiedName,UselessMainMethod
8989
com.thealgorithms.others.Mandelbrot=UselessMainMethod,UselessParentheses
9090
com.thealgorithms.others.MiniMaxAlgorithm=UselessMainMethod,UselessParentheses
91+
com.thealgorithms.others.MosAlgorithm=UselessMainMethod
9192
com.thealgorithms.others.PageRank=UselessMainMethod,UselessParentheses
9293
com.thealgorithms.others.PerlinNoise=UselessMainMethod,UselessParentheses
9394
com.thealgorithms.others.QueueUsingTwoStacks=UselessParentheses
9495
com.thealgorithms.others.Trieac=UselessMainMethod,UselessParentheses
9596
com.thealgorithms.others.Verhoeff=UnnecessaryFullyQualifiedName,UselessMainMethod
9697
com.thealgorithms.puzzlesandgames.Sudoku=UselessMainMethod
98+
com.thealgorithms.recursion.DiceThrower=UselessMainMethod
9799
com.thealgorithms.searches.HowManyTimesRotated=UselessMainMethod
98100
com.thealgorithms.searches.InterpolationSearch=UselessParentheses
99101
com.thealgorithms.searches.KMPSearch=UselessParentheses

src/main/java/com/thealgorithms/datastructures/trees/BSTRecursiveGeneric.java

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* </p>
1414
*
1515
* @author [Madhur Panwar](<a href="https://github.com/mdrpanwar">git-Madhur Panwar</a>)
16+
* @author [Udaya Krishnan M](<a href="https://github.com/UdayaKrishnanM/">git-Udaya Krishnan M</a>) {added prettyDisplay() method}
1617
*/
1718
public class BSTRecursiveGeneric<T extends Comparable<T>> {
1819

@@ -28,6 +29,29 @@ public BSTRecursiveGeneric() {
2829
root = null;
2930
}
3031

32+
/**
33+
* Displays the tree is a structed format
34+
*/
35+
public void prettyDisplay() {
36+
prettyDisplay(root, 0);
37+
}
38+
39+
private void prettyDisplay(Node<T> node, int level) {
40+
if (node == null) {
41+
return;
42+
}
43+
prettyDisplay(node.right, level + 1);
44+
if (level != 0) {
45+
for (int i = 0; i < level - 1; i++) {
46+
System.out.print("|\t");
47+
}
48+
System.out.println("|---->" + node.data);
49+
} else {
50+
System.out.println(node.data);
51+
}
52+
prettyDisplay(node.left, level + 1);
53+
}
54+
3155
/**
3256
* main function for testing
3357
*/
@@ -38,7 +62,12 @@ public static void main(String[] args) {
3862

3963
integerTree.add(5);
4064
integerTree.add(10);
41-
integerTree.add(9);
65+
integerTree.add(-9);
66+
integerTree.add(4);
67+
integerTree.add(3);
68+
integerTree.add(1);
69+
System.out.println("Pretty Display of current tree is:");
70+
integerTree.prettyDisplay();
4271
assert !integerTree.find(4)
4372
: "4 is not yet present in BST";
4473
assert integerTree.find(10)
@@ -54,16 +83,21 @@ public static void main(String[] args) {
5483
assert integerTree.find(70)
5584
: "70 was inserted but not found";
5685
/*
57-
Will print in following order
58-
5 10 20 70
86+
Will print in following order
87+
5 10 20 70
5988
*/
89+
System.out.println("Pretty Display of current tree is:");
90+
integerTree.prettyDisplay();
6091
integerTree.inorder();
92+
System.out.println("Pretty Display of current tree is:");
93+
integerTree.prettyDisplay();
6194
System.out.println();
6295
System.out.println("Testing for string data...");
6396
// String
6497
BSTRecursiveGeneric<String> stringTree = new BSTRecursiveGeneric<String>();
6598

6699
stringTree.add("banana");
100+
stringTree.add("apple");
67101
stringTree.add("pineapple");
68102
stringTree.add("date");
69103
assert !stringTree.find("girl")
@@ -80,11 +114,15 @@ public static void main(String[] args) {
80114
stringTree.add("hills");
81115
assert stringTree.find("hills")
82116
: "hills was inserted but not found";
117+
System.out.println("Pretty Display of current tree is:");
118+
stringTree.prettyDisplay();
83119
/*
84120
Will print in following order
85121
banana hills india pineapple
86122
*/
87123
stringTree.inorder();
124+
System.out.println("Pretty Display of current tree is:");
125+
stringTree.prettyDisplay();
88126
}
89127

90128
/**
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.thealgorithms.maths;
2+
import java.util.ArrayList;
3+
import java.util.List;
4+
import java.util.Random;
5+
6+
/**
7+
* Implementation to calculate an estimate of the number π (Pi).
8+
*
9+
* We take a random point P with coordinates (x, y) such that 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1.
10+
* If x² + y² ≤ 1, then the point is inside the quarter disk of radius 1,
11+
* else the point is outside. We know that the probability of the point being
12+
* inside the quarter disk is equal to π/4.
13+
*
14+
*
15+
* @author [Yash Rajput](https://github.com/the-yash-rajput)
16+
*/
17+
public final class PiApproximation {
18+
19+
private PiApproximation() {
20+
throw new AssertionError("No instances.");
21+
}
22+
23+
/**
24+
* Structure representing a point with coordinates (x, y)
25+
* where 0 ≤ x ≤ 1 and 0 ≤ y ≤ 1.
26+
*/
27+
static class Point {
28+
double x;
29+
double y;
30+
31+
Point(double x, double y) {
32+
this.x = x;
33+
this.y = y;
34+
}
35+
}
36+
37+
/**
38+
* This function uses the points in a given list (drawn at random)
39+
* to return an approximation of the number π.
40+
*
41+
* @param pts List of points where each point contains x and y coordinates
42+
* @return An estimate of the number π
43+
*/
44+
public static double approximatePi(List<Point> pts) {
45+
double count = 0; // Points in circle
46+
47+
for (Point p : pts) {
48+
if ((p.x * p.x) + (p.y * p.y) <= 1) {
49+
count++;
50+
}
51+
}
52+
53+
return 4.0 * count / pts.size();
54+
}
55+
56+
/**
57+
* Generates random points for testing the Pi approximation.
58+
*
59+
* @param numPoints Number of random points to generate
60+
* @return List of random points
61+
*/
62+
public static List<Point> generateRandomPoints(int numPoints) {
63+
List<Point> points = new ArrayList<>();
64+
Random rand = new Random();
65+
66+
for (int i = 0; i < numPoints; i++) {
67+
double x = rand.nextDouble(); // Random value between 0 and 1
68+
double y = rand.nextDouble(); // Random value between 0 and 1
69+
points.add(new Point(x, y));
70+
}
71+
72+
return points;
73+
}
74+
}

0 commit comments

Comments
 (0)