Wintus
6/4/2015 - 8:38 PM

CSC143Lab

CSC143Lab

public class Test {

    public static void main(String[] args) {
/*
        Integer zero = 0;
        Integer one = 1;
        System.out.println(zero.compareTo(one));

*/
        BSTNode<Integer> tree = new BSTNode<>(0);
        tree.insert(-1).insert(1).insert(2);
//        tree.insert(1);
//        tree.insert(2);
        for (int i : new int[]{0, 7, 2, 6}) tree.insert(i);
        System.out.println(tree.inOrder());
        System.out.println(tree.preOrder());
        System.out.println(tree.postOrder());
        System.out.println(tree.find(1).orElse(null));
        System.out.println(tree.find(3).orElse(null));
        System.out.println(tree.find(5).orElse(null));
        System.out.println(tree.find(7).orElse(null));
    }
}
import java.util.LinkedList;
import java.util.Optional;

public class BSTNode<T extends Comparable<T>> {
    T data;
    BSTNode<T> left;
    BSTNode<T> right;

    /**
     * @return the data
     */
    public T getData() {
        return data;
    }

    /**
     * @param data the data to set
     */
    public void setData(T data) {
        this.data = data;
    }

    /**
     * @return the left
     */
    public BSTNode<T> getLeft() {
        return left;
    }

    /**
     * @param left the left to set
     */
    public void setLeft(BSTNode<T> left) {
        this.left = left;
    }

    /**
     * @return the right
     */
    public BSTNode<T> getRight() {
        return right;
    }

    /**
     * @param right the right to set
     */
    public void setRight(BSTNode<T> right) {
        this.right = right;
    }

    public BSTNode(T data) {
        setData(data);
    }

    public BSTNode() {
        // TODO Auto-generated constructor stub
    }

    public boolean isLeaf() {
        return left == null && right == null;
    }

    public BSTNode<T> insert(T data) {
        if (data.compareTo(getData()) < 1) {
            Optional<BSTNode<T>> left = Optional.ofNullable(getLeft());
            left.map(node -> node.insert(data));
            left.orElseGet(() -> {
                setLeft(new BSTNode<>(data));
                return null;
            });
        } else {
            Optional<BSTNode<T>> right = Optional.ofNullable(getRight());
            right.map(node -> node.insert(data));
            right.orElseGet(() -> {
                setRight(new BSTNode<>(data));
                return null;
            });
        }
        return this;
    }

    public Optional<BSTNode<T>> find(T data) {
        int result = data.compareTo(getData());
        if (result == 0)
            return Optional.of(this);
        else if (result < 0) {
            Optional<BSTNode<T>> left = Optional.ofNullable(getLeft());
            return left.map(node -> node.find(data).orElse(null));
        } else {
            Optional<BSTNode<T>> right = Optional.ofNullable(getRight());
            return right.map(node -> node.find(data).orElse(null));
        }
    }

    private LinkedList<T> preOrder(LinkedList<T> list) {
        list.add(getData());
        Optional.ofNullable(getLeft()).ifPresent(node -> node.postOrder(list));
        Optional.ofNullable(getRight()).ifPresent(node -> node.postOrder(list));
        return list;
    }

    LinkedList<T> preOrder() {
        return preOrder(new LinkedList<>());
    }

    private LinkedList<T> postOrder(LinkedList<T> list) {
        Optional.ofNullable(getLeft()).ifPresent(node -> node.postOrder(list));
        Optional.ofNullable(getRight()).ifPresent(node -> node.postOrder(list));
        list.add(getData());
        return list;
    }

    LinkedList<T> postOrder() {
        return postOrder(new LinkedList<>());
    }

    private LinkedList<T> inOrder(LinkedList<T> list) {
        Optional.ofNullable(getLeft()).ifPresent(node -> node.inOrder(list));
        list.add(getData());
        Optional.ofNullable(getRight()).ifPresent(node -> node.inOrder(list));
        return list;
    }

    public LinkedList<T> inOrder() {
        return inOrder(new LinkedList<>());
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="NewModuleRootManager" inherit-compiler-output="false">
    <output url="file://$MODULE_DIR$/bin" />
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
    </content>
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="inheritedJdk" />
  </component>
</module>
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.source=1.7
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
	<name>CSC143BinarySearchTreeLab</name>
	<comment></comment>
	<projects>
	</projects>
	<buildSpec>
		<buildCommand>
			<name>org.eclipse.jdt.core.javabuilder</name>
			<arguments>
			</arguments>
		</buildCommand>
	</buildSpec>
	<natures>
		<nature>org.eclipse.jdt.core.javanature</nature>
	</natures>
</projectDescription>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="VcsDirectoryMappings">
    <mapping directory="" vcs="" />
  </component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="Palette2">
    <group name="Swing">
      <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
        <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
      </item>
      <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
        <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
      </item>
      <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
      </item>
      <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
        <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
      </item>
      <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
        <initial-values>
          <property name="text" value="Button" />
        </initial-values>
      </item>
      <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
        <initial-values>
          <property name="text" value="RadioButton" />
        </initial-values>
      </item>
      <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
        <initial-values>
          <property name="text" value="CheckBox" />
        </initial-values>
      </item>
      <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
        <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
        <initial-values>
          <property name="text" value="Label" />
        </initial-values>
      </item>
      <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
          <preferred-size width="150" height="-1" />
        </default-constraints>
      </item>
      <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
          <preferred-size width="150" height="-1" />
        </default-constraints>
      </item>
      <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
          <preferred-size width="150" height="-1" />
        </default-constraints>
      </item>
      <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
          <preferred-size width="150" height="50" />
        </default-constraints>
      </item>
      <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
          <preferred-size width="150" height="50" />
        </default-constraints>
      </item>
      <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
          <preferred-size width="150" height="50" />
        </default-constraints>
      </item>
      <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
        <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
      </item>
      <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
          <preferred-size width="150" height="50" />
        </default-constraints>
      </item>
      <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
          <preferred-size width="150" height="50" />
        </default-constraints>
      </item>
      <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
          <preferred-size width="150" height="50" />
        </default-constraints>
      </item>
      <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
          <preferred-size width="200" height="200" />
        </default-constraints>
      </item>
      <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
        <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
          <preferred-size width="200" height="200" />
        </default-constraints>
      </item>
      <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
      </item>
      <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
      </item>
      <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
        <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
      </item>
      <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
      </item>
      <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
        <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
          <preferred-size width="-1" height="20" />
        </default-constraints>
      </item>
      <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
        <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
      </item>
      <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
        <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
      </item>
    </group>
  </component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ProjectModuleManager">
    <modules>
      <module fileurl="file://$PROJECT_DIR$/CSC143BinarySearchTreeLab.iml" filepath="$PROJECT_DIR$/CSC143BinarySearchTreeLab.iml" />
    </modules>
  </component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="ClientPropertiesManager">
    <properties class="javax.swing.AbstractButton">
      <property name="hideActionText" class="java.lang.Boolean" />
    </properties>
    <properties class="javax.swing.JComponent">
      <property name="html.disable" class="java.lang.Boolean" />
    </properties>
    <properties class="javax.swing.JEditorPane">
      <property name="JEditorPane.w3cLengthUnits" class="java.lang.Boolean" />
      <property name="JEditorPane.honorDisplayProperties" class="java.lang.Boolean" />
      <property name="charset" class="java.lang.String" />
    </properties>
    <properties class="javax.swing.JList">
      <property name="List.isFileList" class="java.lang.Boolean" />
    </properties>
    <properties class="javax.swing.JPasswordField">
      <property name="JPasswordField.cutCopyAllowed" class="java.lang.Boolean" />
    </properties>
    <properties class="javax.swing.JSlider">
      <property name="Slider.paintThumbArrowShape" class="java.lang.Boolean" />
      <property name="JSlider.isFilled" class="java.lang.Boolean" />
    </properties>
    <properties class="javax.swing.JTable">
      <property name="Table.isFileList" class="java.lang.Boolean" />
      <property name="JTable.autoStartsEdit" class="java.lang.Boolean" />
      <property name="terminateEditOnFocusLost" class="java.lang.Boolean" />
    </properties>
    <properties class="javax.swing.JToolBar">
      <property name="JToolBar.isRollover" class="java.lang.Boolean" />
    </properties>
    <properties class="javax.swing.JTree">
      <property name="JTree.lineStyle" class="java.lang.String" />
    </properties>
    <properties class="javax.swing.text.JTextComponent">
      <property name="caretAspectRatio" class="java.lang.Double" />
      <property name="caretWidth" class="java.lang.Integer" />
    </properties>
  </component>
  <component name="EntryPointsManager">
    <entry_points version="2.0" />
  </component>
  <component name="MavenImportPreferences">
    <option name="generalSettings">
      <MavenGeneralSettings>
        <option name="mavenHome" value="Bundled (Maven 3)" />
      </MavenGeneralSettings>
    </option>
  </component>
  <component name="ProjectLevelVcsManager" settingsEditedManually="false">
    <OptionsSetting value="true" id="Add" />
    <OptionsSetting value="true" id="Remove" />
    <OptionsSetting value="true" id="Checkout" />
    <OptionsSetting value="true" id="Update" />
    <OptionsSetting value="true" id="Status" />
    <OptionsSetting value="true" id="Edit" />
    <ConfirmationsSetting value="0" id="Add" />
    <ConfirmationsSetting value="0" id="Remove" />
  </component>
  <component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/classes" />
  </component>
</project>
<component name="CopyrightManager">
  <settings default="" />
</component>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="CompilerConfiguration">
    <resourceExtensions />
    <wildcardResourcePatterns>
      <entry name="!?*.java" />
      <entry name="!?*.form" />
      <entry name="!?*.class" />
      <entry name="!?*.groovy" />
      <entry name="!?*.scala" />
      <entry name="!?*.flex" />
      <entry name="!?*.kt" />
      <entry name="!?*.clj" />
      <entry name="!?*.aj" />
    </wildcardResourcePatterns>
    <annotationProcessing>
      <profile default="true" name="Default" enabled="false">
        <processorPath useClasspath="true" />
      </profile>
    </annotationProcessing>
  </component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
	<classpathentry kind="src" path="src"/>
	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
	<classpathentry kind="output" path="bin"/>
</classpath>