Rename year directories to allow natural ordering

This commit is contained in:
2023-12-20 03:57:27 +00:00
parent 0ab1f5ad3a
commit 1f7d812b98
1895 changed files with 0 additions and 7188 deletions

View File

@ -0,0 +1,123 @@
/**
* A class that represents nodes in a binary tree.
*
* @author Frank M. Carrano
* @version 2.0
*/
class BinaryNode<T> implements BinaryNodeInterface<T>, java.io.Serializable
{
private static final long serialVersionUID = 6828929352995534482L; // needed for serializable objects
private T data;
private BinaryNode<T> left;
private BinaryNode<T> right;
public BinaryNode()
{
this(null); // call next constructor
} // end default constructor
public BinaryNode(T dataPortion)
{
this(dataPortion, null, null); // call next constructor
} // end constructor
public BinaryNode(T dataPortion, BinaryNode<T> leftChild,
BinaryNode<T> rightChild)
{
data = dataPortion;
left = leftChild;
right = rightChild;
} // end constructor
public T getData()
{
return data;
} // end getData
public void setData(T newData)
{
data = newData;
} // end setData
public BinaryNodeInterface<T> getLeftChild()
{
return left;
} // end getLeftChild
public BinaryNodeInterface<T> getRightChild()
{
return right;
} // end getRightChild
public void setLeftChild(BinaryNodeInterface<T> leftChild)
{
left = (BinaryNode<T>)leftChild;
} // end setLeftChild
public void setRightChild(BinaryNodeInterface<T> rightChild)
{
right = (BinaryNode<T>)rightChild;
} // end setRightChild
public boolean hasLeftChild()
{
return left != null;
} // end hasLeftChild
public boolean hasRightChild()
{
return right != null;
} // end hasRightChild
public boolean isLeaf()
{
return (left == null) && (right == null);
} // end isLeaf
// 26.06
public BinaryNodeInterface<T> copy()
{
BinaryNode<T> newRoot = new BinaryNode<T>(data);
if (left != null)
newRoot.left = (BinaryNode<T>)left.copy();
if (right != null)
newRoot.right = (BinaryNode<T>)right.copy();
return newRoot;
} // end copy
// 26.11
public int getHeight()
{
return getHeight(this); // call private getHeight
} // end getHeight
// 26.11
private int getHeight(BinaryNode<T> node)
{
int height = 0;
if (node != null)
height = 1 + Math.max(getHeight(node.left), getHeight(node.right));
return height;
} // end getHeight
// 26.11
public int getNumberOfNodes()
{
int leftNumber = 0;
int rightNumber = 0;
if (left != null)
leftNumber = left.getNumberOfNodes();
if (right != null)
rightNumber = right.getNumberOfNodes();
return 1 + leftNumber + rightNumber;
} // end getNumberOfNodes
} // end BinaryNode

View File

@ -0,0 +1,56 @@
/**
* An interface for a node in a binary tree.
*
* @author Frank M. Carrano
* @version 2.0
*/
interface BinaryNodeInterface<T>
{
/** Task: Retrieves the data portion of the node.
* @return the object in the data portion of the node */
public T getData();
/** Task: Sets the data portion of the node.
* @param newData the data object */
public void setData(T newData);
/** Task: Retrieves the left child of the node.
* @return the node that is this nodeÕs left child */
public BinaryNodeInterface<T> getLeftChild();
/** Task: Retrieves the right child of the node.
* @return the node that is this nodeÕs right child */
public BinaryNodeInterface<T> getRightChild();
/** Task: Sets the nodeÕs left child to a given node.
* @param leftChild a node that will be the left child */
public void setLeftChild(BinaryNodeInterface<T> leftChild);
/** Task: Sets the nodeÕs right child to a given node.
* @param rightChild a node that will be the right child */
public void setRightChild(BinaryNodeInterface<T> rightChild);
/** Task: Detects whether the node has a left child.
* @return true if the node has a left child */
public boolean hasLeftChild();
/** Task: Detects whether the node has a right child.
* @return true if the node has a right child */
public boolean hasRightChild();
/** Task: Detects whether the node is a leaf.
* @return true if the node is a leaf */
public boolean isLeaf();
/** Task: Counts the nodes in the subtree rooted at this node.
* @return the number of nodes in the subtree rooted at this node */
public int getNumberOfNodes();
/** Task: Computes the height of the subtree rooted at this node.
* @return the height of the subtree rooted at this node */
public int getHeight();
/** Task: Copies the subtree rooted at this node.
* @return the root of a copy of the subtree rooted at this node */
public BinaryNodeInterface<T> copy();
} // end BinaryNodeInterface

View File

@ -0,0 +1,139 @@
/**
* A class that implements the ADT binary tree.
*
* @author Frank M. Carrano
* @version 2.0
*/
public class BinaryTree<T> implements BinaryTreeInterface<T>, java.io.Serializable
{
private static final long serialVersionUID = -1932834476575953631L;
private BinaryNodeInterface<T> root;
public BinaryTree()
{
root = null;
} // end default constructor
public BinaryTree(T rootData)
{
root = new BinaryNode<T>(rootData);
} // end constructor
public BinaryTree(T rootData, BinaryTree<T> leftTree,
BinaryTree<T> rightTree)
{
privateSetTree(rootData, leftTree, rightTree);
} // end constructor
public void setTree(T rootData)
{
root = new BinaryNode<T>(rootData);
} // end setTree
public void setTree(T rootData, BinaryTreeInterface<T> leftTree,
BinaryTreeInterface<T> rightTree)
{
privateSetTree(rootData, (BinaryTree<T>)leftTree,
(BinaryTree<T>)rightTree);
} // end setTree
// 26.08
private void privateSetTree(T rootData, BinaryTree<T> leftTree,
BinaryTree<T> rightTree)
{
root = new BinaryNode<T>(rootData);
if ((leftTree != null) && !leftTree.isEmpty())
root.setLeftChild(leftTree.root);
if ((rightTree != null) && !rightTree.isEmpty())
{
if (rightTree != leftTree)
root.setRightChild(rightTree.root);
else
root.setRightChild(rightTree.root.copy());
} // end if
if ((leftTree != null) && (leftTree != this))
leftTree.clear();
if ((rightTree != null) && (rightTree != this))
rightTree.clear();
} // end privateSetTree
private BinaryNode<T> copyNodes() // not essential
{
return (BinaryNode<T>)root.copy();
} // end copyNodes
// 26.09
public T getRootData()
{
T rootData = null;
if (root != null)
rootData = root.getData();
return rootData;
} // end getRootData
// 26.09
public boolean isEmpty()
{
return root == null;
} // end isEmpty
// 26.09
public void clear()
{
root = null;
} // end clear
// 26.09
protected void setRootData(T rootData)
{
root.setData(rootData);
} // end setRootData
// 26.09
protected void setRootNode(BinaryNodeInterface<T> rootNode)
{
root = rootNode;
} // end setRootNode
// 26.09
protected BinaryNodeInterface<T> getRootNode()
{
return root;
} // end getRootNode
// 26.10
public int getHeight()
{
return root.getHeight();
} // end getHeight
// 26.10
public int getNumberOfNodes()
{
return root.getNumberOfNodes();
} // end getNumberOfNodes
// 26.12
public void inorderTraverse()
{
inorderTraverse(root);
} // end inorderTraverse
private void inorderTraverse(BinaryNodeInterface<T> node)
{
if (node != null)
{
inorderTraverse(node.getLeftChild());
System.out.println(node.getData());
inorderTraverse(node.getRightChild());
} // end if
} // end inorderTraverse
} // end BinaryTree

View File

@ -0,0 +1,20 @@
/**
* An interface for the ADT binary tree.
*
* @author Frank M. Carrano
* @version 2.0
*/
public interface BinaryTreeInterface<T> extends TreeInterface<T>
{
/** Task: Sets an existing binary tree to a new one-node binary tree.
* @param rootData an object that is the data in the new treeÕs root
*/
public void setTree(T rootData);
/** Task: Sets an existing binary tree to a new binary tree.
* @param rootData an object that is the data in the new treeÕs root
* @param leftTree the left subtree of the new tree
* @param rightTree the right subtree of the new tree */
public void setTree(T rootData, BinaryTreeInterface<T> leftTree,
BinaryTreeInterface<T> rightTree);
} // end BinaryTreeInterface

View File

@ -0,0 +1,217 @@
import java.util.*;
import java.io.*;
@SuppressWarnings("unchecked") // ignoring warnings lol
public class GuessingGame implements Serializable {
private static BinaryTree<String> tree = new BinaryTree<String>();
private static String input; // string to which input will be read
private static Scanner sc = new Scanner(System.in); // scanner to read in input
public static void main(String[] args) {
while (1 == 1) {
// pick the tree that will be used for the game, either from a file or built-in
loadTree();
// play the game
gameplay();
playAgainLoop: while (1 == 1) {
System.out.printf("Enter '1' to play again, '2' to quit, or '3' to save the tree to a file\n> ");
input = sc.nextLine();
switch (input) {
case "1": // going back to start of loop
break playAgainLoop;
case "2":
System.exit(0);
break playAgainLoop;
case "3": // storing the tree, this should not break the loop
storeTree();
break;
default:
// loop
}
}
}
}
// method that serializes a tree object to a file
public static void storeTree() {
// scanning in the name of the file from the user
System.out.printf("Enter the name of the file that it the tree should be stored as\n> ");
input = sc.nextLine();
try {
// creating output streams
FileOutputStream fos = new FileOutputStream(input);
ObjectOutputStream oos = new ObjectOutputStream(fos);
// writing the tree object ot the file
oos.writeObject(tree);
// closing output streams
oos.close();
fos.close();
}
// catching IOExceptions
catch (IOException E) {
System.out.println(E);
System.exit(1);
}
}
// method to load a tree, either from a file, from memory, or hardcoded in
public static void loadTree() {
// looping until an appropriate choice is made
loadTreeLoop: while (1 == 1) {
System.out.printf("Load a tree from a file? If no, then the built-in tree will be used. y/n\n> ");
input = sc.nextLine();
switch (input) {
case "y":
// looping until valid filename is entered
while (1 == 1) {
System.out.printf("Enter the file from which the tree should be loaded\n> ");
input = sc.nextLine();
File treefile = new File(input);
// breaking if the file exists
if (treefile.exists()) {
break;
}
}
try {
// creating input streams
FileInputStream fis = new FileInputStream(input);
ObjectInputStream ois = new ObjectInputStream(fis);
// deserializing tree object
tree = (BinaryTree<String>) ois.readObject();
// closing input streams
ois.close();
fis.close();
}
// printing errors and crashing
catch(IOException E) {
System.out.println(E);
System.exit(1);
}
catch (ClassNotFoundException E) {
System.out.println(E);
System.exit(1);
}
break loadTreeLoop;
case "n":
// if no tree is defined building the default tree
if (tree.getRootNode() == null) {
// first the leaves
BinaryTree<String> cowTree = new BinaryTree<String>("Is it a cow?");
BinaryTree<String> dogTree = new BinaryTree<String>("Is it a dog?");
BinaryTree<String> fishTree = new BinaryTree<String>("Is it a goldfish?");
BinaryTree<String> geckoTree = new BinaryTree<String>("Is it a gecko?");
// Now the subtrees joining leaves:
BinaryTree<String> mammalTree = new BinaryTree<String>("Is it a farm animal?", cowTree, dogTree);
BinaryTree<String> notMammalTree = new BinaryTree<String>("Is it a type of fish?", fishTree, geckoTree);
// Now the root
tree.setTree("Is it a mammal?", mammalTree, notMammalTree);
}
break loadTreeLoop;
default:
// loop
}
}
}
public static void gameplay() {
System.out.println("Enter 'y' for 'yes', 'n' for 'no'");
BinaryNodeInterface<String> curr = tree.getRootNode(); // current node
// looping until a leaf node is reached
while (!curr.isLeaf()) {
// looping until an appropriate reply is given by the user
answerloop: while (1 == 1) {
// printing the question & scanning in the answer
System.out.printf(curr.getData() + "\n> ");
input = sc.nextLine();
switch (input) {
case "y": // continuing via left node if answer to question is yes
curr = curr.getLeftChild();
break answerloop;
case "n": // continuing via right node if answer to question is no
curr = curr.getRightChild();
break answerloop;
default:
// loop
}
}
}
// making a guess
// looping until an appropriate reply is given by the user
guessloop: while (1 == 1) {
// printing the question & scanning in the answer
System.out.printf(curr.getData() + "\n> ");
input = sc.nextLine();
switch (input) {
case "y": // printing a success message if the answer is yes
System.out.println("Success! The guess was correct");
break guessloop;
case "n": // inserting a new question and putting the two guesses beneath it if wrong
System.out.printf("Enter the animal that you were thinking of\n> ");
String thinkingOf = sc.nextLine();
String wrong = curr.getData();
// ask the user for a question to distinguish the wrong guess from the correct answer
System.out.printf("Enter a question to distinguish %s from '%s'\n> ", thinkingOf, wrong);
String question = sc.nextLine();
// replacing the current node with the question to distinguish it
curr.setData(question);
// asking the user for the correct answer to the question for the animal they were thinking of
addNodeLoop: while (1 == 1) { // looping until an appropriate answer is given
System.out.printf("Enter the correct answer to the question that you entered for %s (y or n)\n> ", thinkingOf);
input = sc.nextLine();
switch (input) {
case "y": // adding thinkingOf to the left of the question node if the answer is yes
curr.setLeftChild(new BinaryNode<String>("Is it a " + thinkingOf + "?"));
curr.setRightChild(new BinaryNode<String>(wrong));
break addNodeLoop;
case "n": // adding thinkingOf to the left of the question node if the answer is no
curr.setLeftChild(new BinaryNode<String>(wrong));
curr.setRightChild(new BinaryNode<String>("Is it a " + thinkingOf + "?"));
break addNodeLoop;
default:
// loop
}
}
break guessloop;
default:
// loop
}
}
}
}

View File

@ -0,0 +1,14 @@
/**
* An interface for basic operations of a tree.
*
* @author Frank M. Carrano
* @version 2.0
*/
public interface TreeInterface<T>
{
public T getRootData();
public int getHeight();
public int getNumberOfNodes();
public boolean isEmpty();
public void clear();
} // end TreeInterface