Commit 521ecb75 authored by ElaFHNW's avatar ElaFHNW
Browse files

recursion files

parent 269320d5
package recursion;
// Binary search tree used to sort the input
public class BSTsimple {
Node root;
public void insert(int value) {
root = add(root, value); // base case
}
private Node add(Node current, int value) {
if (current == null) {
return new Node(value); // terminating condition
}
if (value < current.value) {
current.left = add(current.left, value);
} else if (value > current.value) {
current.right = add(current.right, value);
} else {
// value already exists
return current;
}
return current;
}
public boolean contains(int value) {
return containsNode(root, value);
}
private boolean containsNode(Node current, int value) {
if (current == null) {
return false;
}
if (value == current.value) {
return true;
}
return value < current.value
? containsNode(current.left, value)
: containsNode(current.right, value);
}
public void listSorted(Node node) {
if (node != null) {
listSorted(node.left);
System.out.print(" " + node.value);
listSorted(node.right);
}
}
public static void main(String [] args) {
BSTsimple bt = new BSTsimple();
System.out.println("tree " + bt);
System.out.println("root " + bt.root);
bt.insert(6);
System.out.println("root " + bt.root + " value " + bt.root.value);
System.out.println("children: " + bt.root.left + ", " + bt.root.right);
bt.insert(4);
bt.insert(8);
bt.insert(3);
bt.insert(5);
bt.insert(7);
bt.insert(9);
// containment tests
System.out.println(bt.contains(6));
System.out.println(bt.contains(4));
System.out.println(bt.contains(1)); // expect false
bt.listSorted(bt.root);
System.out.println();
// show the tree
System.out.println(" " + bt.root.value);
System.out.println(" / \\");
System.out.print(" " + bt.root.left.value);
System.out.println(" " + bt.root.right.value);
System.out.println(" / \\ / \\");
System.out.print(" " + bt.root.left.left.value);
System.out.print(" " + bt.root.left.right.value);
System.out.print(" " + bt.root.right.left.value);
System.out.println(" " + bt.root.right.right.value);
}
private class Node {
int value;
Node left;
Node right;
Node(int value) {
this.value = value;
right = null;
left = null;
}
}
}
\ No newline at end of file
package recursion;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Line;
//************************************************************************
// KochPane.java Author: Lewis/Loftus
//
// Represents the pane in which the Koch Snowflake fractal is presented.
//************************************************************************
public class KochPane extends Pane
{
public final static double SQ = Math.sqrt(3) / 6;
//--------------------------------------------------------------------
// Makes an initial fractal of order 1 (a triangle) when the pane
// is first created.
//--------------------------------------------------------------------
public KochPane()
{
makeFractal(1);
}
//--------------------------------------------------------------------
// Draws the fractal by clearing the pane and then adding three
// lines of the specified order between three predetermined points.
//--------------------------------------------------------------------
public void makeFractal(int order)
{
getChildren().clear();
addLine(order, 200, 20, 60, 300);
addLine(order, 60, 300, 340, 300);
addLine(order, 340, 300, 200, 20);
}
//--------------------------------------------------------------------
// Recursively adds a line of the specified order to the fractal.
// The base case is a straight line between the given points.
// Otherwise, three intermediate points are computed and four line
// segments are added as a fractal of decremented order.
//--------------------------------------------------------------------
public void addLine(int order, double x1, double y1, double x5,
double y5)
{
double deltaX, deltaY, x2, y2, x3, y3, x4, y4;
if (order == 1)
{
getChildren().add(new Line(x1, y1, x5, y5));
}
else
{
deltaX = x5 - x1; // distance between the end points
deltaY = y5 - y1;
x2 = x1 + deltaX / 3; // one third
y2 = y1 + deltaY / 3;
x3 = (x1 + x5) / 2 + SQ * (y1 - y5); // projection
y3 = (y1 + y5) / 2 + SQ * (x5 - x1);
x4 = x1 + deltaX * 2 / 3; // two thirds
y4 = y1 + deltaY * 2 / 3;
addLine(order - 1, x1, y1, x2, y2);
addLine(order - 1, x2, y2, x3, y3);
addLine(order - 1, x3, y3, x4, y4);
addLine(order - 1, x4, y4, x5, y5);
}
}
}
package recursion;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//************************************************************************
// KochSnowflake.java Author: Lewis/Loftus
//
// Demonstrates the use of recursion to draw a fractal.
//************************************************************************
public class KochSnowflake extends Application
{
private final static int MIN_ORDER = 1;
private final static int MAX_ORDER = 6;
private int order;
private Button up, down;
private Text orderText;
private KochPane fractalPane;
//--------------------------------------------------------------------
// Displays two buttons that control the order of the fractal
// shown in the pane below the buttons.
//--------------------------------------------------------------------
public void start(Stage primaryStage)
{
Image upImage = new Image("recursion/up.png");
up = new Button();
up.setGraphic(new ImageView(upImage));
up.setOnAction(this::processUpButtonPress);
Image downImage = new Image("recursion/down.png");
down = new Button();
down.setGraphic(new ImageView(downImage));
down.setOnAction(this::processDownButtonPress);
down.setDisable(true);
order = 1;
orderText = new Text("Order: 1");
HBox toolbar = new HBox();
toolbar.setStyle("-fx-background-color: darksalmon");
toolbar.setAlignment(Pos.CENTER);
toolbar.setPrefHeight(50);
toolbar.setSpacing(40);
toolbar.getChildren().addAll(up, orderText, down);
fractalPane = new KochPane();
VBox root = new VBox();
root.setStyle("-fx-background-color: white");
root.getChildren().addAll(toolbar, fractalPane);
Scene scene = new Scene(root, 400, 450);
primaryStage.setTitle("Koch Snowflake");
primaryStage.setScene(scene);
primaryStage.show();
}
//--------------------------------------------------------------------
// Increments the fractal order when the up button is pressed.
// Disables the up button if the maximum order is reached.
//--------------------------------------------------------------------
public void processUpButtonPress(ActionEvent event)
{
order++;
orderText.setText("Order: " + order);
fractalPane.makeFractal(order);
down.setDisable(false);
if (order == MAX_ORDER)
up.setDisable(true);
}
//--------------------------------------------------------------------
// Decrements the fractal order when the down button is pressed.
// Disables the down button if the minimum order is reached.
//--------------------------------------------------------------------
public void processDownButtonPress(ActionEvent event)
{
order--;
orderText.setText("Order: " + order);
fractalPane.makeFractal(order);
up.setDisable(false);
if (order == MIN_ORDER)
down.setDisable(true);
}
public static void main(String[] args)
{
launch(args);
}
}
package recursion;
//********************************************************************
// Maze.java Author: Lewis/Loftus
//
// Represents a maze of characters. The goal is to get from the
// top left corner to the bottom right, following a path of 1s.
//********************************************************************
public class Maze
{
private final int TRIED = 3;
private final int PATH = 7;
private int[][] grid = { {1,1,1,0,1,1,0,0,0,1,1,1,1},
{1,0,1,1,1,0,1,1,1,1,0,0,1},
{0,0,0,0,1,0,1,0,1,0,1,0,0},
{1,1,1,0,1,1,1,0,1,0,1,1,1},
{1,0,1,0,0,0,0,1,1,1,0,0,1},
{1,0,1,1,1,1,1,1,0,1,1,1,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0},
{1,1,1,1,1,1,1,1,1,1,1,1,1} };
//-----------------------------------------------------------------
// Attempts to recursively traverse the maze. Inserts special
// characters indicating locations that have been tried and that
// eventually become part of the solution.
//-----------------------------------------------------------------
public boolean traverse(int row, int column)
{
boolean done = false;
if (valid(row, column))
{
grid[row][column] = TRIED; // this cell has been tried
if (row == grid.length-1 && column == grid[0].length-1)
done = true; // the maze is solved
else
{
done = traverse(row+1, column); // down
if (!done)
done = traverse(row, column+1); // right
if (!done)
done = traverse(row-1, column); // up
if (!done)
done = traverse(row, column-1); // left
}
if (done) // this location is part of the final path
grid[row][column] = PATH;
}
return done;
}
//-----------------------------------------------------------------
// Determines if a specific location is valid.
//-----------------------------------------------------------------
private boolean valid(int row, int column)
{
boolean result = false;
// check if cell is in the bounds of the matrix
if (row >= 0 && row < grid.length &&
column >= 0 && column < grid[row].length)
// check if cell is not blocked and not previously tried
if (grid[row][column] == 1)
result = true;
return result;
}
//-----------------------------------------------------------------
// Returns the maze as a string.
//-----------------------------------------------------------------
public String toString()
{
String result = "\n";
for (int row=0; row < grid.length; row++)
{
for (int column=0; column < grid[row].length; column++)
result += grid[row][column] + "";
result += "\n";
}
return result;
}
}
package recursion;
//********************************************************************
// MazeSearch.java Author: Lewis/Loftus
//
// Demonstrates recursion.
//********************************************************************
public class MazeSearch
{
//-----------------------------------------------------------------
// Creates a new maze, prints its original form, attempts to
// solve it, and prints out its final form.
//-----------------------------------------------------------------
public static void main(String[] args)
{
Maze labyrinth = new Maze();
System.out.println(labyrinth);
if (labyrinth.traverse(0, 0))
System.out.println("The maze was successfully traversed!");
else
System.out.println("There is no possible path.");
System.out.println(labyrinth);
}
}
package recursion;
import javafx.application.Application;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.Pane;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
public class RecursiveGraphics extends Application{
Pane root = new Pane();
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setScene(new Scene(drawPane()));
primaryStage.show();
}
private Parent drawPane() {
root.setPrefSize(600, 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
Rectangle r = new Rectangle(x,y,w,h);
root.getChildren().add(r);
addRectangle(x+10,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