Created
January 30, 2012 23:40
-
-
Save nixpulvis/1707557 to your computer and use it in GitHub Desktop.
infinate set (countability of rational numbers)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class ATree { | |
Rational value; | |
ATree(Rational value){ | |
this.value = value; | |
} | |
abstract Node expandOneLevel(); | |
// returns the integer value of the node/leaf | |
public Double doubleValue(){ | |
return this.value.toDouble(); | |
} | |
// makes n expansions of the tree from the given tree | |
public ATree expandNLevels(int n){ | |
if(n <= 0) { | |
return new Leaf(null); | |
} else if (n == 1) { | |
return this.expandOneLevel(); | |
} else { | |
return this.expandOneLevel().expandNLevels(n-1); | |
} | |
} | |
//Takes an ATree and prints the values of it's node | |
// the order goes left to right on each level before moving to the next | |
public abstract String toString(); | |
} | |
class Node extends ATree{ | |
ATree left; | |
ATree right; | |
Node(Rational value, ATree left, ATree right){ | |
super(value); | |
this.left = left; | |
this.right = right; | |
} | |
public Node expandOneLevel(){ | |
return new Node(this.value, this.left.expandOneLevel(), this.right.expandOneLevel()); | |
} | |
//Takes an ATree and prints the values of it's node | |
// the order goes left to right on each level before moving to the next | |
public String toString(){ | |
//return Double.toString(this.doubleValue()).concat(" ").concat(this.left.toString()).concat(this.right.toString()); | |
return Double.toString(this.doubleValue()).concat(" ").concat(Double.toString(this.left.doubleValue())).concat(" ").concat(Double.toString(this.doubleValue())) | |
.concat(" ").concat(this.left.toString()).concat(this.right.toString()); | |
} | |
} | |
class Leaf extends ATree{ | |
Leaf(Rational value){ | |
super(value); | |
} | |
public Node expandOneLevel(){ | |
return new Node(this.value, new Leaf(new Rational(this.value.numerator,(this.value.numerator+this.value.denominator))), | |
new Leaf(new Rational((this.value.numerator+this.value.denominator),this.value.denominator))); | |
} | |
//Takes an ATree and prints the values of it's node | |
// the order goes left to right on each level before moving to the next | |
public String toString(){ | |
return Double.toString(this.doubleValue()).concat(" "); | |
} | |
} | |
class Rational{ | |
int numerator; | |
int denominator; | |
Rational(int numerator, int denominator) { | |
this.numerator = numerator; | |
this.denominator = denominator; | |
} | |
//return the int value of this rational | |
public Double toDouble() { | |
int n = this.numerator; | |
int d = this.denominator; | |
return ((double)n / (double)d); | |
} | |
} | |
class ExamplesTrees{ | |
Rational oneHalf = new Rational(1,2); | |
double pointfive = oneHalf.toDouble(); | |
ATree t_0 = new Leaf(new Rational(1,1)); | |
ATree t_n = t_0.expandNLevels(2); | |
String ans = t_n.toString(); | |
//ATree t_4 = t_0.expandNLevels(4); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment