Add second year
@ -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
|
@ -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
|
@ -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
|
@ -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
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
@ -0,0 +1,85 @@
|
||||
<?xml version="1.0" standalone="yes"?>
|
||||
<!-- logreq request file -->
|
||||
<!-- logreq version 1.0 / dtd version 1.0 -->
|
||||
<!-- Do not edit this file! -->
|
||||
<!DOCTYPE requests [
|
||||
<!ELEMENT requests (internal | external)*>
|
||||
<!ELEMENT internal (generic, (provides | requires)*)>
|
||||
<!ELEMENT external (generic, cmdline?, input?, output?, (provides | requires)*)>
|
||||
<!ELEMENT cmdline (binary, (option | infile | outfile)*)>
|
||||
<!ELEMENT input (file)+>
|
||||
<!ELEMENT output (file)+>
|
||||
<!ELEMENT provides (file)+>
|
||||
<!ELEMENT requires (file)+>
|
||||
<!ELEMENT generic (#PCDATA)>
|
||||
<!ELEMENT binary (#PCDATA)>
|
||||
<!ELEMENT option (#PCDATA)>
|
||||
<!ELEMENT infile (#PCDATA)>
|
||||
<!ELEMENT outfile (#PCDATA)>
|
||||
<!ELEMENT file (#PCDATA)>
|
||||
<!ATTLIST requests
|
||||
version CDATA #REQUIRED
|
||||
>
|
||||
<!ATTLIST internal
|
||||
package CDATA #REQUIRED
|
||||
priority (9) #REQUIRED
|
||||
active (0 | 1) #REQUIRED
|
||||
>
|
||||
<!ATTLIST external
|
||||
package CDATA #REQUIRED
|
||||
priority (1 | 2 | 3 | 4 | 5 | 6 | 7 | 8) #REQUIRED
|
||||
active (0 | 1) #REQUIRED
|
||||
>
|
||||
<!ATTLIST provides
|
||||
type (static | dynamic | editable) #REQUIRED
|
||||
>
|
||||
<!ATTLIST requires
|
||||
type (static | dynamic | editable) #REQUIRED
|
||||
>
|
||||
<!ATTLIST file
|
||||
type CDATA #IMPLIED
|
||||
>
|
||||
]>
|
||||
<requests version="1.0">
|
||||
<internal package="biblatex" priority="9" active="0">
|
||||
<generic>latex</generic>
|
||||
<provides type="dynamic">
|
||||
<file>CT2109-Assignment-05.bcf</file>
|
||||
</provides>
|
||||
<requires type="dynamic">
|
||||
<file>CT2109-Assignment-05.bbl</file>
|
||||
</requires>
|
||||
<requires type="static">
|
||||
<file>blx-dm.def</file>
|
||||
<file>blx-compat.def</file>
|
||||
<file>biblatex.def</file>
|
||||
<file>standard.bbx</file>
|
||||
<file>numeric.bbx</file>
|
||||
<file>numeric.cbx</file>
|
||||
<file>biblatex.cfg</file>
|
||||
<file>english.lbx</file>
|
||||
</requires>
|
||||
</internal>
|
||||
<external package="biblatex" priority="5" active="0">
|
||||
<generic>biber</generic>
|
||||
<cmdline>
|
||||
<binary>biber</binary>
|
||||
<infile>CT2109-Assignment-05</infile>
|
||||
</cmdline>
|
||||
<input>
|
||||
<file>CT2109-Assignment-05.bcf</file>
|
||||
</input>
|
||||
<output>
|
||||
<file>CT2109-Assignment-05.bbl</file>
|
||||
</output>
|
||||
<provides type="dynamic">
|
||||
<file>CT2109-Assignment-05.bbl</file>
|
||||
</provides>
|
||||
<requires type="dynamic">
|
||||
<file>CT2109-Assignment-05.bcf</file>
|
||||
</requires>
|
||||
<requires type="editable">
|
||||
<file>ecl.bib</file>
|
||||
</requires>
|
||||
</external>
|
||||
</requests>
|
@ -0,0 +1,187 @@
|
||||
% THIS DOCUMENT HAS BEEN EDITED FOR EXPERIMENTATION PURPOSES AND THUS NO LONGER IS THE SAME AS THE DOCUMENT SUBMITTED FOR GRADING
|
||||
\documentclass[a4paper]{article}
|
||||
|
||||
\usepackage{listings}
|
||||
\usepackage{xcolor}
|
||||
\definecolor{codegreen}{rgb}{0,0.6,0}
|
||||
\definecolor{codegray}{rgb}{0.5,0.5,0.5}
|
||||
\definecolor{codeorange}{rgb}{1,0.49,0}
|
||||
\definecolor{backcolour}{rgb}{0.95,0.95,0.96}
|
||||
\usepackage{sourcecodepro}
|
||||
\usepackage{pgfplots}
|
||||
\pgfplotsset{width=\textwidth,compat=1.9}
|
||||
|
||||
\lstdefinestyle{mystyle}{
|
||||
backgroundcolor=\color{backcolour},
|
||||
commentstyle=\color{codegray},
|
||||
keywordstyle=\color{codeorange},
|
||||
numberstyle=\tiny\color{codegray},
|
||||
stringstyle=\color{codegreen},
|
||||
basicstyle=\ttfamily\footnotesize,
|
||||
breakatwhitespace=false,
|
||||
breaklines=true,
|
||||
captionpos=b,
|
||||
keepspaces=true,
|
||||
numbers=left,
|
||||
numbersep=5pt,
|
||||
showspaces=false,
|
||||
showstringspaces=false,
|
||||
showtabs=false,
|
||||
tabsize=2,
|
||||
xleftmargin=10pt,
|
||||
}
|
||||
|
||||
\lstset{style=mystyle}
|
||||
|
||||
\input{head}
|
||||
\hypersetup{
|
||||
colorlinks,
|
||||
urlcolor = black
|
||||
}
|
||||
|
||||
\raggedbottom
|
||||
\begin{document}
|
||||
% \renewcommand{\verbatim@font}{\ttfamily\small}
|
||||
|
||||
%-------------------------------
|
||||
% TITLE SECTION
|
||||
%-------------------------------
|
||||
|
||||
\fancyhead[C]{}
|
||||
\hrule \medskip % Upper rule
|
||||
\begin{minipage}{0.295\textwidth}
|
||||
\begin{raggedright}
|
||||
\footnotesize
|
||||
Andrew Hayes \hfill\\
|
||||
21321503 \hfill\\
|
||||
\href{mailto:a.hayes18@nuigalway.ie}{a.hayes18@nuigalway.ie}
|
||||
\end{raggedright}
|
||||
\end{minipage}
|
||||
\begin{minipage}{0.4\textwidth}
|
||||
\begin{centering}
|
||||
\large
|
||||
CT2109 Assignment 3\\
|
||||
\normalsize
|
||||
\end{centering}
|
||||
\end{minipage}
|
||||
\begin{minipage}{0.295\textwidth}
|
||||
\begin{raggedleft}
|
||||
\footnotesize
|
||||
\today \hfill\\
|
||||
\end{raggedleft}
|
||||
\end{minipage}
|
||||
|
||||
\medskip\hrule
|
||||
\medskip
|
||||
\begin{centering}
|
||||
Expandable Binary Tree Guessing Game\\
|
||||
\end{centering}
|
||||
\medskip\hrule
|
||||
\bigskip
|
||||
|
||||
\section{Problem Statement}
|
||||
The problem of this assignment is to create an expandable binary tree guessing game, not unlike the popular web game ``Akinator''.
|
||||
There will be a tree which will consist of question nodes.
|
||||
Each node will contain some String data: this will be the question that the node represents.
|
||||
These questions will be ``yes'' or ``no'' questions.
|
||||
Each node will have a maximum of two children.
|
||||
These children will represent the next question after the parent has been answered.
|
||||
\\\\
|
||||
The tree will be traversed node by node, starting at the root node.
|
||||
Each node's data will be printed to the user, and they will be prompted to answer the question by entering either ``y'' or ``n''.
|
||||
If the user answers ``y'', then the next node traversed will be the left child of the current node, but if the answer is ``n'', then the next node will be the right child.
|
||||
Nodes on the left represent an affirmative answer to the parent's question, and nodes on the right will represent a negative answer to the parent's question.
|
||||
\\\\
|
||||
Eventually, a leaf node will be reached (a node with no children).
|
||||
If a node is a leaf node, then this means that this node represents a guess by the game.
|
||||
These guesses will be in the form of a ``yes'' or ``no'' question, e.g., ``Is it a dog?''.
|
||||
If the user enters ``y'', then the program has won, and the game will be over.
|
||||
If the user enters ``n'', then the user won the game, and the program will expand it's knowledge by asking the user for a question that distinguishes the game's guess from the correct answer.
|
||||
This question will then be inserted in the tree to replace the leaf node that was the game's guess.
|
||||
The user will then be asked if the answer to the distinguishing question for the correct answer should be ``y'' or ``n'', and the initial incorrect guess \& the correct answer will be inserted as children
|
||||
of this parent node in the appropriate positions, depending on what the answer to the distinguishing question is.
|
||||
\\\\
|
||||
This program will also implement the ability to save the binary tree to a file and to load a binary tree from a file.
|
||||
The standard way of doing such a thing in Java is to implement the \verb|Serializable| interface.
|
||||
This allows for an object to be written to a file and recovered at a later date, after an indefinite time period has elapsed, regardless of whether the program has been running or not.
|
||||
|
||||
\section{Analysis \& Design Notes}
|
||||
The main method of the program will consist of an infinite loop.
|
||||
The loop will firstly call a \verb|loadTree()| method which does one of three things: load a tree from a file, generate a pre-built tree that's hardcoded in, or use an already defined tree in memory, if one
|
||||
exists.
|
||||
The user will be asked whether or not the tree should be loaded from a file.
|
||||
A case statement will be used to react to the user's inputs, ``y'' for ``yes'', ``n'' for ``no''.
|
||||
This will loop until a valid input is given.
|
||||
If the answer is ``y'', then the user will be prompted to enter the name of the file from which the tree should be loaded.
|
||||
This will loop until a valid filename is given (one that exists).
|
||||
Then, the program will attempt to de-serialize the file into a \verb|BinaryTree<String>| object, throwing an error and exiting if any problems are encountered.
|
||||
Because this is not necessarily a safe operation, we presume that the user knows only to supply the program with valid files, and do not check for safety, instead opting to use \verb|@SuppressWarnings("unchecked")|
|
||||
at the top of the class definition.
|
||||
Although it would of course be better practise to ensure file safety, this is somewhat beyond the scope of the assignment, and not really relevant to the theory at hand.
|
||||
If the user opts to not load the tree from a file, one will be generated from some hardcoded values, provided that the existing tree is \verb|null|.
|
||||
If the existing tree is not \verb|null|, then the existing tree is used instead.
|
||||
This will only occur on rematches.
|
||||
\\\\
|
||||
After the tree has been loaded, the \verb|gameplay()| method will be called, and the game will begin.
|
||||
This method will loop over each node, starting at the root node, while the current node is not a leaf node (i.e., while the current node has children).
|
||||
The data of this node (a question String) will be printed out, and the user will enter ``y'' or ``n'' to answer the question.
|
||||
If the user enters anything else, it will loop on this question until an appropriate answer is given.
|
||||
Then, depending on the input, the current node will be replaced with the left or the right child of the current node, left for ``y'', right for ``n''.
|
||||
An appropriate answer will break out of the loop using a label.
|
||||
\\\\
|
||||
When a leaf node is reached, the loop will end and a guess will be made.
|
||||
The user will be asked to verify the guess, and this will loop until an appropriate answer is given.
|
||||
If the user confirms the guess as correct, the game ends, and the user will be presented with a game menu.
|
||||
Otherwise, the user will be asked what they were thinking of.
|
||||
They will then be asked to provide a question to distinguish what they were thinking of from the program's guess, and the appropriate answer to that distinguishing question for the answer they were thinking of.
|
||||
The current node will then be replaced with the distinguishing question, and the guessed answer \& the real answer will become child nodes of this node, placed appropriately on the right or left according
|
||||
to the user's instructions.
|
||||
\\\\
|
||||
Finally, after the \verb|gameplay()| method has completed execution, the user will be presented with the options to play again, quit, or save the tree to a file.
|
||||
If they choose play again, the infinite loop simply repeats, and they are presented with the \verb|loadTree()| operations again.
|
||||
If they choose to quit, the program will exit with code \verb|0|.
|
||||
If they choose to save the tree to a file, the \verb|storeTree()| method will be called.
|
||||
This method prompts the user to enter a filename to which the serialized tree should be saved.
|
||||
The serialized object is then written to this file, overwriting any data that was already in that file, if it existed.
|
||||
|
||||
\section{Code}
|
||||
\lstinputlisting[language=Java, breaklines=true, title={GuessingGame.java}]{../code/GuessingGame.java}
|
||||
|
||||
\newpage
|
||||
\section{Testing}
|
||||
The first thing to be tested is just basic functionality of the game.
|
||||
The screenshot below shows basic testing with valid \& invalid input, but only with animals known to the game.
|
||||
Invalid input should just be re-prompted to be entered.
|
||||
\begin{figure}[h]
|
||||
\centering \includegraphics[width=0.7\textwidth]{./images/first.png}
|
||||
\caption{Basic Testing of the Game with the In-Built Tree}
|
||||
\end{figure}
|
||||
|
||||
The next bit of testing is testing of an animal that the game doesn't know, adding it to the tree, and saving it to a file, as shown in the screenshot below.
|
||||
\begin{figure}[h]
|
||||
\centering \includegraphics[width=0.7\textwidth]{./images/saving.png}
|
||||
\caption{Testing of the Saving a Tree to a File}
|
||||
\end{figure}
|
||||
|
||||
\newpage
|
||||
The next bit of testing is testing restoring a binary tree from a file on disk, using the file made above.
|
||||
\begin{figure}[h]
|
||||
\centering \includegraphics[width=0.7\textwidth]{./images/restore.png}
|
||||
\caption{Testing of the Restoring a Tree from a File}
|
||||
\end{figure}
|
||||
|
||||
Testing trying to load a tree from a non-existent file on disk. This should loop until a valid input is given.
|
||||
\begin{figure}[h]
|
||||
\centering \includegraphics[width=0.7\textwidth]{./images/invalidtree.png}
|
||||
\caption{Testing Trying to Load a Non-Existent Tree File from Disk}
|
||||
\end{figure}
|
||||
|
||||
Testing trying to load a normal text file as a binary tree file.
|
||||
Should throw an error and exit gracefully.
|
||||
\begin{figure}[h]
|
||||
\centering \includegraphics[width=0.7\textwidth]{./images/faketree.png}
|
||||
\caption{Testing Trying to Load a Non-Binary Tree File from Disk}
|
||||
\end{figure}
|
||||
|
||||
|
||||
\end{document}
|
@ -0,0 +1,49 @@
|
||||
\addtolength{\hoffset}{-2.25cm}
|
||||
\addtolength{\textwidth}{4.5cm}
|
||||
\addtolength{\voffset}{-3.25cm}
|
||||
\addtolength{\textheight}{5cm}
|
||||
\setlength{\parskip}{0pt}
|
||||
\setlength{\parindent}{0in}
|
||||
|
||||
%----------------------------------------------------------------------------------------
|
||||
% PACKAGES AND OTHER DOCUMENT CONFIGURATIONS
|
||||
%----------------------------------------------------------------------------------------
|
||||
|
||||
\usepackage{blindtext} % Package to generate dummy text
|
||||
\usepackage{charter} % Use the Charter font
|
||||
\usepackage[utf8]{inputenc} % Use UTF-8 encoding
|
||||
\usepackage{microtype} % Slightly tweak font spacing for aesthetics
|
||||
\usepackage[english]{babel} % Language hyphenation and typographical rules
|
||||
\usepackage{amsthm, amsmath, amssymb} % Mathematical typesetting
|
||||
\usepackage{float} % Improved interface for floating objects
|
||||
\usepackage[final, colorlinks = true,
|
||||
linkcolor = black,
|
||||
citecolor = black]{hyperref} % For hyperlinks in the PDF
|
||||
\usepackage{graphicx, multicol} % Enhanced support for graphics
|
||||
\usepackage{xcolor} % Driver-independent color extensions
|
||||
\usepackage{marvosym, wasysym} % More symbols
|
||||
\usepackage{rotating} % Rotation tools
|
||||
\usepackage{censor} % Facilities for controlling restricted text
|
||||
\usepackage{listings, style/lstlisting} % Environment for non-formatted code, !uses style file!
|
||||
\usepackage{pseudocode} % Environment for specifying algorithms in a natural way
|
||||
\usepackage{style/avm} % Environment for f-structures, !uses style file!
|
||||
\usepackage{booktabs} % Enhances quality of tables
|
||||
\usepackage{tikz-qtree} % Easy tree drawing tool
|
||||
\tikzset{every tree node/.style={align=center,anchor=north},
|
||||
level distance=2cm} % Configuration for q-trees
|
||||
\usepackage{style/btree} % Configuration for b-trees and b+-trees, !uses style file!
|
||||
\usepackage[backend=biber,style=numeric,
|
||||
sorting=nyt]{biblatex} % Complete reimplementation of bibliographic facilities
|
||||
\addbibresource{ecl.bib}
|
||||
\usepackage{csquotes} % Context sensitive quotation facilities
|
||||
\usepackage[yyyymmdd]{datetime} % Uses YEAR-MONTH-DAY format for dates
|
||||
\renewcommand{\dateseparator}{-} % Sets dateseparator to '-'
|
||||
\usepackage{fancyhdr} % Headers and footers
|
||||
\pagestyle{fancy} % All pages have headers and footers
|
||||
\fancyhead{}\renewcommand{\headrulewidth}{0pt} % Blank out the default header
|
||||
\fancyfoot[L]{} % Custom footer text
|
||||
\fancyfoot[C]{} % Custom footer text
|
||||
\fancyfoot[R]{\thepage} % Custom footer text
|
||||
\newcommand{\note}[1]{\marginpar{\scriptsize \textcolor{red}{#1}}} % Enables comments in red on margin
|
||||
|
||||
%----------------------------------------------------------------------------------------
|
After Width: | Height: | Size: 45 KiB |
After Width: | Height: | Size: 22 KiB |
After Width: | Height: | Size: 36 KiB |
After Width: | Height: | Size: 24 KiB |
After Width: | Height: | Size: 23 KiB |
After Width: | Height: | Size: 39 KiB |
@ -0,0 +1,165 @@
|
||||
% avm.sty -- for attribute-value matrices -- mar 29, 1992; rev. dec 6, 1993
|
||||
% (c) 1992 christopher manning (manning@csli.stanford.edu) -- see avm.doc.tex
|
||||
|
||||
\newif\ifavmactive\newif\ifavmsorted\newif\ifavmlabeled
|
||||
\newif\ifavmcenter\newif\ifavmbottom
|
||||
\newif\ifavmbottomright\newif\ifavmtopleft\newif\ifavmtopright
|
||||
|
||||
\newdimen\avmdimen
|
||||
\newbox\avmboxone\newbox\avmboxthree
|
||||
|
||||
\def\avmoptions#1{\avmactivefalse\avmsortedfalse\avmlabeledfalse
|
||||
\avmcenterfalse\avmbottomfalse
|
||||
\avmbottomrightfalse\avmtopleftfalse\avmtoprightfalse
|
||||
\def\more{#1}\ifx\more\empty\else\avmjoptions#1,\@nil\fi}
|
||||
\def\avmjoptions#1,#2\@nil{\def\more{#2}\csname avm#1true\endcsname
|
||||
\ifx\more\empty\else\avmjoptions#2\@nil\fi}
|
||||
|
||||
|
||||
\def\avmfont#1{\def\avmjfont{#1}}
|
||||
\def\avmjfont{}
|
||||
|
||||
\def\avmvalfont#1{\def\avmjvalfont{#1}}
|
||||
\def\avmjvalfont{}
|
||||
|
||||
\def\avmsortfont#1{\def\avmjsortfont{#1}}
|
||||
\def\avmjsortfont{}
|
||||
|
||||
\def\avmhskip#1{\def\avmjhskip{#1}}
|
||||
\def\avmjhskip{1em}
|
||||
|
||||
\def\avmbskip#1{\def\avmjbskip{#1}}
|
||||
\def\avmjbskip{0em}
|
||||
|
||||
\def\avmvskip#1{\def\avmjvskip{#1}}
|
||||
\def\avmjvskip{0.385ex}%was .3875
|
||||
|
||||
|
||||
\def\avmjprolog#1{$\mskip-\thinmuskip
|
||||
\left#1\hskip\avmjbskip\vcenter\bgroup\vskip\avmjvskip
|
||||
\ialign\bgroup\avmjfont
|
||||
\strut ##\unskip\hfil
|
||||
&&\hskip\avmjhskip\avmjvalfont ##\unskip\hfil\cr}
|
||||
\def\avmjpostlog#1{\crcr\egroup\vskip\avmjvskip\egroup
|
||||
\hskip\avmjbskip\right#1\mskip-\thinmuskip$\ignorespaces}
|
||||
|
||||
|
||||
\def\avmjcatcode{\let\lparen=(\let\rparen=)\catcode`\[=13\catcode`\]=13
|
||||
\catcode`\<=13\catcode`\@=13\catcode`\(=13\catcode`\)=13
|
||||
\catcode`\>=13\catcode`\|=13}
|
||||
|
||||
{\avmjcatcode % new group: redefine above catcodes as active
|
||||
|
||||
\gdef\specialavm{\avmjcatcode
|
||||
\def({\avmjprolog\lparen}%
|
||||
\def){\avmjpostlog\rparen}%
|
||||
\def<{\avmjprolog\langle}%
|
||||
\def>{\avmjpostlog\rangle}%
|
||||
\ifavmsorted
|
||||
\def[##1{\setbox\avmboxthree=\hbox{\avmjsortfont##1\/}\setbox2=\hbox
|
||||
\bgroup\avmjprolog\lbrack}%
|
||||
\def]{\avmjpostlog\rbrack\egroup\avmjsort}%
|
||||
\else\ifavmlabeled
|
||||
\def[##1{\def\more{##1}\setbox2=\hbox\bgroup\avmjprolog[}%
|
||||
\def]{\avmjpostlog]\egroup\node{\more}{\box2}}%
|
||||
\else
|
||||
\def[{\avmjprolog\lbrack}%
|
||||
\def]{\avmjpostlog\rbrack}%
|
||||
\fi\fi
|
||||
%
|
||||
\def\<{$\langle$}\def\>{$\rangle$}%
|
||||
\def\({\lparen}\def\){\rparen}%
|
||||
\def\[{\lbrack}\def\]{\rbrack}%
|
||||
\def|{$\,\vert\,$}%
|
||||
\def@##1{\avmbox{##1}}%
|
||||
} % end defn of \specialavm
|
||||
} % restore active catcodes
|
||||
|
||||
|
||||
\long\def\avm{\begingroup
|
||||
\ifavmactive\specialavm
|
||||
\else
|
||||
\def\({\avmjprolog(}%
|
||||
\def\){\avmjpostlog)}%
|
||||
\def\<{\avmjprolog\langle}%
|
||||
\def\>{\avmjpostlog\rangle}%
|
||||
%
|
||||
\ifavmsorted
|
||||
\def\[##1{\setbox\avmboxthree=\hbox{\avmjsortfont##1\/}\setbox
|
||||
2=\hbox\bgroup\avmjprolog[}%
|
||||
\def\]{\avmjpostlog]\egroup\avmjsort}%
|
||||
\else\ifavmlabeled
|
||||
\def\[##1{\def\more{##1}\setbox2=\hbox\bgroup\avmjprolog[}%
|
||||
\def\]{\avmjpostlog]\egroup\node{\more}{\box2}}%
|
||||
\else
|
||||
\def\[{\avmjprolog[}%
|
||||
\def\]{\avmjpostlog]}%
|
||||
\fi\fi
|
||||
%
|
||||
\def\|{$\,\vert\,$}%
|
||||
\def\@##1{\avmbox{##1}}%
|
||||
\fi % end not active
|
||||
%
|
||||
\ifx\LaTeX\undefined\def\\{\cr}% running under TeX
|
||||
\else \def\\{\@tabularcr}% Leverage off LaTeX's \\*[dimen] options
|
||||
\fi
|
||||
\def\!{\node}%
|
||||
\long\def\avmjsort{\dimen2=\ht2\advance\dimen2 by -.25\baselineskip
|
||||
\global\dimen\avmdimen=\wd\avmboxthree
|
||||
\ifavmtopleft \raise\dimen2\llap{\box\avmboxthree}\box2%
|
||||
\else\ifavmtopright \box2\raise\dimen2\box\avmboxthree%
|
||||
\else\ifavmbottomright \box2\lower\dimen2\box\avmboxthree%
|
||||
\else \lower\dimen2\llap{\box\avmboxthree}\box2%
|
||||
\fi\fi\fi}%
|
||||
\long\def\sort##1##2{\setbox2=\hbox{##2}\setbox
|
||||
\avmboxthree=\hbox{\avmjsortfont##1\/}\dimen2=\ht2
|
||||
\advance\dimen2 by -.25\baselineskip
|
||||
\ifavmtopleft \raise\dimen2\box\avmboxthree\box2%
|
||||
\else\ifavmtopright \box2\raise\dimen2\box\avmboxthree%
|
||||
\else\ifavmbottomright \box2\lower\dimen2\box\avmboxthree%
|
||||
\else \lower\dimen2\box\avmboxthree\box2%
|
||||
\fi\fi\fi}%
|
||||
\long\def\osort##1##2{\setbox2=\hbox{##2}\setbox
|
||||
\avmboxthree=\hbox{\avmjsortfont ##1\/}\avmjsort}%
|
||||
\def\avml{\avmjprolog.}%
|
||||
\def\avmr{\avmjpostlog.}%
|
||||
\def\avmb##1{\node{##1}{\lbrack\;\rbrack}}%
|
||||
\def\avmd##1{\node{##1}{---}}%
|
||||
\def\q##1{\ifx ##1\{$\lbrace$\else
|
||||
\ifx ##1\}$\rbrace$\else
|
||||
\ifx ##1<$\langle$\else
|
||||
\ifx ##1>$\rangle$\fi \fi \fi \fi}%
|
||||
\def\{{\avmjprolog\lbrace}%
|
||||
\def\}{\avmjpostlog\rbrace}%
|
||||
\def\;{\hskip\avmjhskip}%
|
||||
\def\avmspan##1{\multispan2\strut ##1\expandafter\hfil}%
|
||||
\avmjfont
|
||||
\openup\avmjvskip
|
||||
\setbox\avmboxone=\hbox\bgroup\ignorespaces
|
||||
} % end defn of \avm
|
||||
|
||||
|
||||
\def\endavm{\egroup\ifvmode\leavevmode\fi % this if is useful!
|
||||
\ifavmsorted\null\hskip\dimen\avmdimen\fi
|
||||
\ifavmcenter
|
||||
\box\avmboxone
|
||||
\else \ifavmbottom
|
||||
\lower.575\baselineskip\hbox{\vbox{\box\avmboxone\null}}%
|
||||
\else
|
||||
% the next bit is ripped off from Emma's \evnup in lingmacros.sty
|
||||
\dimen2=\ht\avmboxone\advance\dimen2 by -.725\baselineskip
|
||||
\lower\dimen2\box\avmboxone
|
||||
\fi \fi \endgroup}
|
||||
|
||||
|
||||
% based on TeXbook exercise 21.3
|
||||
\def\avmbox#1{\setbox2=\hbox{$\scriptstyle #1$}\lower.2ex\vbox{\hrule
|
||||
\hbox{\vrule\kern1.25pt
|
||||
\vbox{\kern1.25pt\box2\kern1.25pt}\kern1.25pt\vrule}\hrule}}
|
||||
|
||||
% ============ COSTOM CONFIGURATION =============
|
||||
\avmfont{\sc}
|
||||
\avmoptions{sorted,active}
|
||||
\avmvalfont{\rm}
|
||||
\avmsortfont{\scriptsize\it}
|
||||
% ===============================================
|
@ -0,0 +1,131 @@
|
||||
%% Last Modified: Thu Oct 18 18:26:25 2007.
|
||||
|
||||
\NeedsTeXFormat{LaTeX2e}
|
||||
\ProvidesPackage{style/btree}
|
||||
\typeout{Document Style `weiw_BTree - Support drawing B+-Tree (ver 0.999)}
|
||||
|
||||
\RequirePackage{tikz}
|
||||
\RequirePackage{ifthen}
|
||||
|
||||
% use libraries
|
||||
\usetikzlibrary{arrows,shapes,decorations,matrix}
|
||||
|
||||
|
||||
%% global declaration
|
||||
\tikzstyle{btreeptr} = [draw, semithick, minimum height=2em]
|
||||
\tikzstyle{btreeval} = [draw, semithick, minimum size=2em]
|
||||
\tikzstyle{btreevale} = [draw,semithick, minimum size=2em]
|
||||
\tikzstyle{btlink} = [draw, semithick, ->, >=triangle 45]
|
||||
|
||||
%% macro
|
||||
%% helper macros
|
||||
\newcommand{\suppressemptystr}[1]{% leave blank for entries in leaf nodes
|
||||
\ifthenelse{\equal{#1}{}}%
|
||||
{%
|
||||
\relax%
|
||||
}%
|
||||
% Else
|
||||
{%
|
||||
#1\textsuperscript{*}%
|
||||
}%
|
||||
}%
|
||||
|
||||
\newcommand{\xyshift}[3]{% help to place the nodes
|
||||
\begin{scope}[xshift=#1, yshift=#2]
|
||||
#3
|
||||
\end{scope}%
|
||||
}
|
||||
|
||||
%% Common btree macros
|
||||
\newcommand{\btreelink}[2]{% #1: src node; #2: dest node;
|
||||
\draw[btlink] ([yshift=3pt] #1.south) -- (#2-b.north);
|
||||
}
|
||||
|
||||
\newcommand{\btreelinknorth}[2]{% #1: src node; #2: dest node;
|
||||
\draw[btlink] ([yshift=3pt] #1.south) -- (#2.north);
|
||||
}
|
||||
|
||||
\newcommand{\btreetriangle}[2]{% #1: node name; #2 text inside
|
||||
\node[anchor=north, regular polygon, regular polygon sides=3, draw] (#1) {#2};
|
||||
}
|
||||
|
||||
%%======================================================================
|
||||
%% btree with capacity = 4
|
||||
\newcommand{\btreeinodefour}[5]{%
|
||||
\matrix [ampersand replacement=\&] (#1)
|
||||
{
|
||||
\node[btreeptr] (#1-1) {\vphantom{1}}; \& \node[btreeval] (#1-a) {#2}; \&
|
||||
\node[btreeptr] (#1-2) {\vphantom{1}}; \& \node[btreeval] (#1-b) {#3}; \&
|
||||
\node[btreeptr] (#1-3) {\vphantom{1}}; \& \node[btreeval] (#1-c) {#4}; \&
|
||||
\node[btreeptr] (#1-4) {\vphantom{1}}; \& \node[btreeval] (#1-d) {#5}; \&
|
||||
\node[btreeptr] (#1-5) {\vphantom{1}}; \\
|
||||
};
|
||||
}
|
||||
\newcommand{\btreelnodefour}[5]{%
|
||||
\matrix [ampersand replacement=\&, outer sep=0pt, matrix anchor=north] (#1)
|
||||
{
|
||||
\node[btreevale] (#1-a) {\suppressemptystr{#2}}; \&
|
||||
\node[btreevale] (#1-b) {\suppressemptystr{#3}}; \&
|
||||
\node[btreevale] (#1-c) {\suppressemptystr{#4}}; \&
|
||||
\node[btreevale] (#1-d) {\suppressemptystr{#5}}; \\
|
||||
};
|
||||
}
|
||||
|
||||
%%======================================================================
|
||||
%% btree with capacity = 3
|
||||
\newcommand{\btreeinodethree}[4]{%
|
||||
\matrix [ampersand replacement=\&] (#1)
|
||||
{
|
||||
\node[btreeptr] (#1-1) {\vphantom{1}}; \& \node[btreeval] (#1-a) {#2}; \&
|
||||
\node[btreeptr] (#1-2) {\vphantom{1}}; \& \node[btreeval] (#1-b) {#3}; \&
|
||||
\node[btreeptr] (#1-3) {\vphantom{1}}; \& \node[btreeval] (#1-c) {#4}; \&
|
||||
\node[btreeptr] (#1-4) {\vphantom{1}}; \\
|
||||
};
|
||||
}
|
||||
\newcommand{\btreelnodethree}[4]{%
|
||||
\matrix [ampersand replacement=\&, outer sep=0pt, matrix anchor=north] (#1)
|
||||
{
|
||||
\node[btreevale] (#1-a) {\suppressemptystr{#2}}; \&
|
||||
\node[btreevale] (#1-b) {\suppressemptystr{#3}}; \&
|
||||
\node[btreevale] (#1-c) {\suppressemptystr{#4}}; \\
|
||||
};
|
||||
}
|
||||
|
||||
%%======================================================================
|
||||
%% btree with capacity = 2
|
||||
\newcommand{\btreeinodetwo}[4]{%
|
||||
\matrix [ampersand replacement=\&] (#1)
|
||||
{
|
||||
\node[btreeptr] (#1-1) {\vphantom{1}}; \& \node[btreeval] (#1-a) {#2}; \&
|
||||
\node[btreeptr] (#1-2) {\vphantom{1}}; \& \node[btreeval] (#1-b) {#3}; \&
|
||||
\node[btreeptr] (#1-3) {\vphantom{1}}; \\
|
||||
};
|
||||
}
|
||||
\newcommand{\btreelnodetwo}[3]{%
|
||||
\matrix [ampersand replacement=\&, outer sep=0pt, matrix anchor=north] (#1)
|
||||
{
|
||||
\node[btreevale] (#1-a) {\suppressemptystr{#2}}; \&
|
||||
\node[btreevale] (#1-b) {\suppressemptystr{#3}}; \\
|
||||
};
|
||||
}
|
||||
%%======================================================================
|
||||
|
||||
|
||||
|
||||
|
||||
%% simple example
|
||||
% \begin{center}
|
||||
% \scalebox{0.7}{
|
||||
% \begin{tikzpicture}
|
||||
% %
|
||||
% \btreeinodefour{root}{13}{17}{24}{30};
|
||||
% \xyshift{-40mm}{-20mm}{\btreelnodefour{n1}{2}{3}{5}{7}}
|
||||
% \xyshift{-0mm}{-20mm}{\btreelnodefour{n2}{14}{16}{}{}}
|
||||
% \xyshift{40mm}{-20mm}{\btreelnodefour{n3}{19}{20}{22}{}}
|
||||
% \xyshift{80mm}{-20mm}{\btreelnodefour{n4}{24}{27}{29}{}}
|
||||
% \xyshift{120mm}{-20mm}{\btreelnodefour{n5}{33}{34}{38}{39}}
|
||||
% %
|
||||
% \foreach \x in {1,2,...,5} { \btreelink{root-\x}{n\x} }
|
||||
% \end{tikzpicture}
|
||||
% }
|
||||
% \end{center}
|
After Width: | Height: | Size: 66 KiB |
@ -0,0 +1,38 @@
|
||||
% Source: ss17_wissschreib (Eva)
|
||||
|
||||
\lstset{
|
||||
basicstyle=\ttfamily\scriptsize\mdseries,
|
||||
keywordstyle=\bfseries\color[rgb]{0.171875, 0.242188, 0.3125},
|
||||
identifierstyle=,
|
||||
commentstyle=\color[rgb]{0.257813, 0.15625, 0},
|
||||
stringstyle=\itshape\color[rgb]{0.0195313, 0.195313, 0.0117188},
|
||||
numbers=left,
|
||||
numberstyle=\tiny,
|
||||
stepnumber=1,
|
||||
breaklines=true,
|
||||
frame=none,
|
||||
showstringspaces=false,
|
||||
tabsize=4,
|
||||
backgroundcolor=\color[rgb]{0.98,0.98,0.98},
|
||||
captionpos=b,
|
||||
float=htbp,
|
||||
language=Python,
|
||||
xleftmargin=15pt,
|
||||
xrightmargin=15pt
|
||||
}
|
||||
|
||||
%(deutsche) Sonderzeichen
|
||||
\lstset{literate=%
|
||||
{Ä}{{\"A}}1
|
||||
{Ö}{{\"O}}1
|
||||
{Ü}{{\"U}}1
|
||||
{ä}{{\"a}}1
|
||||
{ö}{{\"o}}1
|
||||
{ü}{{\"u}}1
|
||||
{ß}{{\ss}}1
|
||||
}
|
||||
|
||||
%Verwendung im Text:
|
||||
%-> \begin{lstlisting}[language=Python,firstnumber=27] ... \end{lstlisting}
|
||||
%-> \begin{lstlisting}[language=Python,numbers=none] ... \end{lstlisting}
|
||||
%-> \lstinline[language=JAVA]{...}
|