Lara Luu 10 anni fa
parent
commit
67e1877a8e

+ 6 - 0
Lab10/.classpath

@@ -0,0 +1,6 @@
+<?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>

+ 17 - 0
Lab10/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Lab10</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>

+ 12 - 0
Lab10/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+#Mon Oct 26 11:08:21 PDT 2015
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6

+ 14 - 0
Lab10/src/BSTWork.java

@@ -0,0 +1,14 @@
+
+public class BSTWork {
+	public static void main(String... args)
+	{
+		BasicBST tree = new BasicBST();
+		System.out.println("Options:");
+		System.out.println("\"a\" -\tAdd to Tree");
+		System.out.println("\"o\" -\tCount number of odds");
+		System.out.println("\"e\" -\tHeight of tree");
+		System.out.println("\"t\" -\tCount Leaves");
+		System.out.println("\"c\" -\tOne Child Nodes");
+		System.out.println("\"q\" -\tQuit");
+	}
+}

+ 136 - 0
Lab10/src/BasicBST.java

@@ -0,0 +1,136 @@
+
+public class BasicBST 
+{
+	
+	private class BSTNode
+	{
+		public int element;
+		public BSTNode left;
+		public BSTNode right;
+		
+		public BSTNode(int element)
+		{
+			this.element = element;
+			this.left = null;
+			this.right = null;
+		}
+	}
+	
+	private BSTNode root;
+	
+	public BasicBST()
+	{
+		root = null;
+	}
+	
+	
+	
+	public void insert(int insert)
+	{
+		if(root == null)
+		{
+			root = new BSTNode(insert);
+		}
+		else
+		{
+			insertHelper(insert, root);
+		}
+	}
+	
+	private BSTNode insertHelper(int insert, BSTNode chkNode)
+	{
+		if (chkNode == null)
+		{
+			return new BSTNode(insert);
+		}
+		else if(insert < chkNode.element)
+		{
+			chkNode.left = insertHelper(insert, chkNode.left);
+		}
+		else
+		{
+			chkNode.right = insertHelper(insert, chkNode.right);
+		}
+		return chkNode;
+	}
+	
+	public int countOdds()
+	{
+		return countHelper(root);
+	}
+	
+	private int countHelper(BSTNode chkNode)
+	{
+		if (chkNode == null)
+		{
+			return 0;
+		}
+		else if(chkNode.element %2 == 1)
+		{
+			return 1 + countHelper(chkNode.left)+ countHelper(chkNode.right); 
+		}
+		else
+		{
+			return countHelper(chkNode.left)+ countHelper(chkNode.right);
+		}
+	}
+	
+	public int height()
+	{
+		return heightHelper(root);
+	}
+	
+	private int heightHelper(BSTNode chkNode)
+	{
+		if(chkNode == null)
+		{
+			return -1;
+		}
+		else
+		{
+			return 1 + Math.max(heightHelper(chkNode.left), heightHelper(chkNode.right));
+		}
+	}
+	
+	public int countLeaves()
+	{
+		return leavesHelper(root);
+	}
+	
+	private int leavesHelper(BSTNode chkNode)
+	{
+		if(chkNode == null)
+		{
+			return 0;
+		}
+		else if (chkNode.left == null && chkNode.right == null)
+		{
+			return 1;
+		}
+		else
+		{
+			return leavesHelper(chkNode.left) + leavesHelper(chkNode.right);
+		}
+	}
+
+	public int countOneChildParents()
+	{
+		return oneChildHelper(root);
+	}
+	
+	private int oneChildHelper(BSTNode chkNode)
+	{
+		if(chkNode == null)
+		{
+			return 0;
+		}
+		else if (chkNode.left == null ^ chkNode.right == null)
+		{
+			return 1;
+		}
+		else
+		{
+			return oneChildHelper(chkNode.left) + oneChildHelper(chkNode.right);
+		}
+	}
+}