소스 검색

Finished Lab 10

Lara Luu 10 년 전
부모
커밋
ceca009128
2개의 변경된 파일52개의 추가작업 그리고 1개의 파일을 삭제
  1. 51 0
      Lab10/src/BSTWork.java
  2. 1 1
      Lab10/src/BasicBST.java

+ 51 - 0
Lab10/src/BSTWork.java

@@ -1,3 +1,5 @@
+import java.util.Scanner;
+
 
 public class BSTWork {
 	public static void main(String... args)
@@ -10,5 +12,54 @@ public class BSTWork {
 		System.out.println("\"t\" -\tCount Leaves");
 		System.out.println("\"c\" -\tOne Child Nodes");
 		System.out.println("\"q\" -\tQuit");
+		Scanner in = new Scanner(System.in);
+		boolean running = true;
+		while (running)
+		{
+			String line = in.nextLine();
+			if (line.length() != 1)
+			{
+				System.out.println("Invalid Option.");
+				//printHelp();
+				continue;
+			}
+			char command = line.charAt(0);
+			switch (command)
+			{
+				case 'a':
+					System.out.println("Enter an integer to insert.");
+					if (in.hasNextInt())
+					{
+						int newVal = in.nextInt();
+						tree.insert(newVal);
+						System.out.println(newVal+" added");
+					}
+					else
+					{
+						System.out.println("Not an integer.  No action taken.");
+					}
+					in.nextLine();
+					break;
+				case 'o':
+					System.out.println("Tree has "+tree.countOdds()+" odd integers.");
+					break;
+				case 'h':
+					System.out.println("Tree has "+tree.height()+" levels.");
+					break;
+				case 'l':
+					System.out.println("Tree has "+tree.countLeaves()+" leaves.");
+					break;
+				case 'c':
+					System.out.println("Tree has "+tree.countOneChildParents()+" parents with one child.");
+					break;
+				case 'q':
+					running = false;
+					System.out.println("Quitting");
+					break;
+				default:
+					System.out.println("Invalid Option.");
+					//printHelp();
+			}
+		}
 	}
 }

+ 1 - 1
Lab10/src/BasicBST.java

@@ -126,7 +126,7 @@ public class BasicBST
 		}
 		else if (chkNode.left == null ^ chkNode.right == null)
 		{
-			return 1;
+			return 1 + oneChildHelper(chkNode.left) + oneChildHelper(chkNode.right);
 		}
 		else
 		{