Commit c942b7c5 authored by ElaFHNW's avatar ElaFHNW
Browse files

final versions of BST and recursive graphics

parent 521ecb75
......@@ -3,12 +3,14 @@ package recursion;
// Binary search tree used to sort the input
public class BSTsimple {
Node root;
int size;
public void insert(int value) {
root = add(root, value); // base case
}
private Node add(Node current, int value) {
if (current == null) {
this.size++;
return new Node(value); // terminating condition
}
if (value < current.value) {
......@@ -43,7 +45,9 @@ public class BSTsimple {
listSorted(node.right);
}
}
public int getSize() {
return size;
}
public static void main(String [] args) {
BSTsimple bt = new BSTsimple();
System.out.println("tree " + bt);
......@@ -61,8 +65,9 @@ public class BSTsimple {
System.out.println(bt.contains(6));
System.out.println(bt.contains(4));
System.out.println(bt.contains(1)); // expect false
System.out.println("sorted numbers: ");
bt.listSorted(bt.root);
System.out.println();
System.out.println("\ntree size "+ bt.getSize());
// show the tree
System.out.println(" " + bt.root.value);
System.out.println(" / \\");
......
......@@ -12,7 +12,6 @@ public class RecursiveGraphics extends Application{
public static void main(String[] args) {
launch(args);
}
@Override
......@@ -22,16 +21,16 @@ public class RecursiveGraphics extends Application{
}
private Parent drawPane() {
root.setPrefSize(600, 600);
root.setPrefSize(500, 600);
addRectangle(0,0,170,60); // base case
return root;
}
private void addRectangle(int x, int y, int w, int h) {
if (x+w>1000) return; // terminating condition
if ( y+h>=620 ) return; // terminating condition
Rectangle r = new Rectangle(x,y,w,h);
root.getChildren().add(r);
addRectangle(x+10,y+20,w,h+30);
addRectangle(x+30,y+20,w,h+30);
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment