Browse Source

Added heap project

Tom Flucke 10 years ago
parent
commit
a61a3f053c
5 changed files with 127 additions and 0 deletions
  1. 6 0
      Lab7/.classpath
  2. 17 0
      Lab7/.project
  3. 12 0
      Lab7/.settings/org.eclipse.jdt.core.prefs
  4. 28 0
      Lab7/src/TreeTest.java
  5. 64 0
      Lab7/src/TreeWork.java

+ 6 - 0
Lab7/.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.6"/>
+	<classpathentry kind="output" path="bin"/>
+</classpath>

+ 17 - 0
Lab7/.project

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+	<name>Lab7</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
Lab7/.settings/org.eclipse.jdt.core.prefs

@@ -0,0 +1,12 @@
+#Mon Oct 12 11:08:10 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

+ 28 - 0
Lab7/src/TreeTest.java

@@ -0,0 +1,28 @@
+import java.util.Scanner;
+
+
+public class TreeTest
+{
+	public static void main(String... args)
+	{
+		Integer[] arr = new Integer[20];
+		int size = 0;
+		Scanner in = new Scanner(System.in);
+		System.out.println("Waddup boosh. I herd you like giving me a tree to input. (:");
+		while (size != arr.length && in.hasNextInt())
+		{
+			arr[size++] = in.nextInt();
+		}
+		in.close();
+		if (size == 0)
+		{
+			System.out.println("The tree is empty.");
+		}
+		else
+		{
+			System.out.println(TreeWork.isHeap(arr, size)? "Is Heap":"Is Not Heap");
+			System.out.println();
+			TreeWork.printTree(arr, size);
+		}
+	}
+}

+ 64 - 0
Lab7/src/TreeWork.java

@@ -0,0 +1,64 @@
+
+public class TreeWork
+{
+	private static int leftChild(int index)
+	{
+		return 2*index+1;
+	}
+	private static int rightChild(int index)
+	{
+		return 2*index+2;
+	}
+	
+	public static<T extends Comparable<? super T>> boolean isHeap(T[] tree, int size)
+	{
+		return isHeap(tree, 0, size);
+	}
+	
+	private static<T extends Comparable<? super T>> boolean isHeap(T[] tree, int start, int size)
+	{
+		if (leftChild(start) >= size)
+		{
+			return true;
+		}
+		else if (tree[leftChild(start)].compareTo(tree[start]) < 0)
+		{
+			return false;
+		}
+		else if (!isHeap(tree, leftChild(start), size))
+		{
+			return false;
+		}
+		
+		else if (rightChild(start) >= size)
+		{
+			return true;
+		}
+		else if (tree[rightChild(start)].compareTo(tree[start]) < 0)
+		{
+			return false;
+		}
+		else if (!isHeap(tree, rightChild(start), size))
+		{
+			return false;
+		}
+		
+		else
+		{
+			return true;
+		}
+	}
+	
+	public static<T> void printTree(T[] tree, int size)
+	{
+		for (int i = 0; i < size; i++)
+		{
+			if (i != 0 && (Math.log(i + 1)/Math.log(2)) % 1 == 0)
+			{
+				System.out.println();
+			}
+			System.out.print(tree[i] + " ");
+		}
+		System.out.println();
+	}
+}