Saturday, 10 August 2013

Binary Search Tree for Strings (Java)

Binary Search Tree for Strings (Java)

I just would like to ask if how can I come up with this specific output?
Enter root string: Old
Insert string or period(.) to stop: Programmers
Insert string or period(.) to stop: Never
Insert string or period(.) to stop: Die
Insert string or period(.) to stop: They
Insert string or period(.) to stop: Just
Insert string or period(.) to stop: Lose
Insert string or period(.) to stop: Their
Insert string or period(.) to stop: Memories
Insert string or period(.) to stop: .
Output:
Old = Root
Programmers = Right child of "Old"
Never = Left child of "Old"
Die = Left child of "Never"
They = Right child of "Programmers"
Just = Right child of "Die"
Lose = Right child of "Just"
Their = Left child of "They"
Memories = Right child of "Lose"
I tried to store the user's input in an array list but I always get an
error. So far I have this code:
public class SortTreeDemo {
ArrayList<String> items = new ArrayList<> ();
private static class TreeNode {
String item;
TreeNode left;
TreeNode right;
TreeNode(String str) {
item = str;
}
}
private static TreeNode root, rootie;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String item;
String str = ".";
int s=0;
while (true) {
if(s==0){System.out.print("Enter root string: ");
item = sc.nextLine();
s++;
}
System.out.print("Insert string or period(.) to stop: ");
item = sc.nextLine();
if(item.equals(str)){
return;
}
if (item.length() == 0) {
break;
}
if (treeContains(root, item)) {
System.out.println("\nThat item is already in the tree.");
} else {
treeInsert(item); // Add user's string to the tree.
}
}
System.out.println("\n\nExiting program.");
}
private static void treeInsert(String newItem) {
ArrayList<String> inputs = new ArrayList<>();
if (root == null) {
root = new TreeNode(newItem);
rootie = root;
return;
}
TreeNode runner;
runner = root;
String str = ".";
while (!newItem.equals(str)) {
if (newItem.compareTo(runner.item) < 0) {
if (runner.left == null) {
runner.left = new TreeNode(newItem);
System.out.println(newItem+ " Left Child of " +runner.item);
inputs.add(newItem);
return; // New item has been added to the tree.
} else {
runner = runner.left;
}
} else {
if (runner.right == null) {
runner.right = new TreeNode(newItem);
System.out.println(newItem+ " Right Child of " +runner.item);
inputs.add(newItem);
return; // New item has been added to the tree.
} else {
runner = runner.right;
}
}
}
}
static boolean treeContains(TreeNode root, String item) {
if (root == null) {
return false;
} else {
if (item.equals(root.item)) {
return true;
} else {
if (item.compareTo(root.item) < 0) {
return treeContains(root.left, item);
} else {
return treeContains(root.right, item);
}
}
}
}
private static void treeList(TreeNode node) {
if (node != null) {
treeList(node.left); // Print items in left subtree.
System.out.print(" " + node.item); // Print item in the node.
treeList(node.right);
}
}
private static void treeDisplay(String newItem) {
if (root == null) {
root = new TreeNode(newItem);
rootie = root;
return;
}
TreeNode runner;
runner = root;
String str = ".";
while (true) {
if(!newItem.equals(str)){
break;
}
if (newItem.compareTo(runner.item) < 0) {
if (runner.left == null) {
runner.left = new TreeNode(newItem);
System.out.println(newItem+ " Left Child of " +runner.item);
//inputs.add(newItem);
return; // New item has been added to the tree.
} else {
runner = runner.left;
}
} else {
if (runner.right == null) {
runner.right = new TreeNode(newItem);
System.out.println(newItem+ " Right Child of " +runner.item);
// inputs.add(newItem);
return; // New item has been added to the tree.
} else {
runner = runner.right;
}
}
}
}
}
Any help will mean a lot to me :) Thanks

No comments:

Post a Comment