Sirr_Purr_the_Cat's Profile
Reputation: 1
Apprentice
- Group:
- Members
- Active Posts:
- 33 (0.1 per day)
- Joined:
- 14-June 12
- Profile Views:
- 298
- Last Active:
Apr 21 2013 09:15 PM- Currently:
- Offline
Previous Fields
- Dream Kudos:
- 0
Posts I've Made
-
In Topic: BinarySearchTree - Search Method Works but Fails to Print Result
Posted 21 Apr 2013
That does make sense to me, so I've added it. And while I did learn something...it still doesn't solve my searchFor method problem, not returning the value it has found. -
In Topic: BinarySearchTree - Search Method Works but Fails to Print Result
Posted 21 Apr 2013
macosxnerd101, on 21 April 2013 - 08:39 PM, said:Quote
Where should I extend comparable? I do that already for the class data type which the Node is holding.
Your BinarySearchTree class, and you aren't doing it for Node.
I am doing it for the class Word though, where I really want the compareTo method to come into play. Or should I change that? -
In Topic: BinarySearchTree - Search Method Works but Fails to Print Result
Posted 21 Apr 2013
macosxnerd101, on 21 April 2013 - 08:24 PM, said:Also, a note on your design. Your BST should be defined so that T extends Comparable<? super T> in your class definition. You shouldn't be casting T to Word in your search() method. You should be able to invoke compareTo() directly.
I have a bad habit when working with generics to just cast my method for the current program I'm using just to make it easier on myself. Where should I extend comparable? I do that already for the class data type which the Node is holding.
And for a runnable sample here is the code of all the classes that I'm using and a simple main class that tests the search method.
public class Node<T> { private T data; private Node left; private Node right; public Node (T newData) { data = newData; left = null; right = null; } public Node () { data = null; left = null; right = null; } public String toString () { String s = data.toString(); if (left != null ) { s += "\n" + left.toString(); } if (right != null) { s+= "\n"+ right.toString(); } return s; } public void setData (T newData) { data = newData; } public T getData () { return data; } public void setLeft (Node newLeft) { left = newLeft; } public Node getLeft () { return left; } public void setRight (Node newRight) { right = newRight; } public Node getRight () { return right; } public boolean equals (Node theOther) { return this.data.equals (theOther.data); } public int compareTo (Node theOther) { return ((Word) this.getData()).compareTo((Word) theOther.getData()); } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author fsylin */ public class BinaryTree<T> { private Node<T> root ; public BinaryTree () { root = null; } public void inOrder (Node root) { if (root != null) { inOrder (root.getLeft ()); System.out.println (root); inOrder (root.getRight ()); } } public void preOrder (Node root) { if (root != null) { System.out.println (root); preOrder (root.getLeft ()); preOrder (root.getRight ()); } } public void postOrder (Node root) { if (root != null) { postOrder (root.getLeft ()); postOrder (root.getRight ()); System.out.println (root); } } public boolean empty () { return (root == null); } public Node getRoot () { return root; } public void setRoot (Node newRoot) { root = newRoot; } }
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ /** * * @author fsylin */ public class BinarySearchTree<T> extends BinaryTree<T> { public BinarySearchTree () { super (); } public Node add(T element, Node<T> root) { if(root == null) root = new Node(element); else if(((Word) element).compareTo((Word)root.getData()) <= 0) root.setLeft(add(element, root.getLeft())); else root.setRight(add(element, root.getRight())); return root; } public String search(T element, Node<T> root) { if(root == null) return null; else if(((Word) element).compareTo((Word)root.getData()) < 0) return search(element, root.getLeft()); else if(((Word) element).compareTo((Word) root.getData()) > 0) return search(element, root.getRight()); else return root.toString(); } public boolean addOne (T newMember) { boolean success = true; Node<T> newNode = new Node<T> (newMember); if (empty ()) setRoot (newNode); else { Node p = null; Node c = getRoot (); int compare = 0; while (c != null) { compare = newNode.compareTo (c); if (compare == 0) { System.out.println ("duplicate found"); success = false; return success; } else { p = c; if (compare > 0) c = c.getRight (); else c = c.getLeft (); } } if (compare > 0) p.setRight (newNode); else p.setLeft (newNode); } return success; } public T delete (Node key) { T dataInDeleted = null; if (!empty ()) { Node p = null; Node c = getRoot (); int compare = 0; while (c != null) { compare = key.compareTo (c); if (compare == 0) break; else { p = c; if (compare > 0) c = c.getRight (); else c = c.getLeft (); } } if (c == null) { System.out.println ("not found"); return dataInDeleted; } else { dataInDeleted = (T) c.getData (); Node betweenPnC = null; // covers left and one-child cases if (c == getRoot ()) betweenPnC = getRoot (); else if (c == p.getLeft ()) betweenPnC = p.getLeft (); else betweenPnC = p.getRight (); if (betweenPnC.getRight () == null) betweenPnC = betweenPnC.getLeft (); else if (betweenPnC.getLeft () == null) betweenPnC = betweenPnC.getRight (); //end of leaf and one-child cases //2-child case, go right once then keep going left till null Node px = c; Node pc = c.getRight (); while (pc.getLeft () != null) { px = pc; pc = pc.getLeft (); } c.setData ((T) pc.getData ()); if (px == c) px.setRight (pc.getRight ()); else px.setLeft (pc.getRight ()); } } return dataInDeleted; } //use inorder traversal public void traverse () { Node startPoint = getRoot (); inOrder (startPoint); } public Node binarySearch (Node key) { Node found = null; Node temp = getRoot ( ); while (temp != null) { int x = key.compareTo (temp); if (x == 0) { found = temp; break; } else if (x < 0) temp = temp.getLeft ( ); else temp = temp.getRight ( ); } return found; } }
public class Word implements Comparable<Word> { String name = ""; String define = ""; public Word(String newName) { name = newName; define = "No current definition yet..."; } public Word(String newName, String newDef) { name = newName; define = newDef; } public Word() {} public String name() { return name; } public String define() { return define; } public int compareTo(Word otherWord) { int comparison = name.compareToIgnoreCase(otherWord.name()); if(comparison < 0) comparison = -1; else if(comparison > 0) comparison = 1; else if(comparison ==0) comparison = 0; return comparison; } public String toString() { String output = ""; output = name + ": " + define; return output; } }
Ignore the second constructor
import java.util.ArrayList; import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; public class Dictionary { Scanner input = null; Scanner scan = new Scanner(System.in); ArrayList<Word> list = new ArrayList<Word>(); BinarySearchTree<Word> wordTree = new BinarySearchTree<Word>(); public Dictionary(Word[] newList) { for(int i = 0; i < newList.length; i++) { list.add(newList[i]); } for(int x = 0; x < list.size(); x++) wordTree.addOne(list.get(x)); } /* public Dictionary() { boolean fileOk = true; do { fileOk = true; System.out.println("Enter source file name: "); String sourceFileName = scan.next(); try { input = new Scanner (new File (sourceFileName)); } catch (FileNotFoundException ex) { System.out.println ("Failed to find file..."); fileOk = false; } } while (!fileOk); while(input.hasNext()) { String aLine = input.nextLine (); int pos = aLine.indexOf(":"); String name = aLine.substring(0,pos - 1); String definition = aLine.substring(pos + 1); Word newWord = new Word(name, definition); list.add(newWord); } } */ public String searchFor(Word searchedWord) { if(wordTree.search(searchedWord, wordTree.getRoot()) == null) { System.out.println("Word not found. Type in the word you want to add, press enter, and then type it's definition."); String name = scan.next(); String def = scan.next(); Word newWord = new Word(name, def); wordTree.add(newWord, wordTree.getRoot()); } return wordTree.search(searchedWord, wordTree.getRoot()); } public String toString() { return wordTree.getRoot().toString(); } }
import java.util.*; public class Main { /** * @param args */ public static void main(String[] args) { Scanner scan = new Scanner(System.in); Word GUI = new Word("GUI", "Graphical User Interface"); Word Mouse = new Word("Mouse","Accesory that allows simpler interface with a GUI"); Word RAM = new Word("RAM","Random Accesss Memory"); Word CLI = new Word("CLI","Command Line Interface"); Word AI = new Word("AI","Artifical Intelligence"); Word IDE = new Word("IDE","Intergrated Development Enviornment"); Word[] list = new Word[]{GUI, Mouse, RAM, CLI, AI, IDE}; Dictionary computerDefs = new Dictionary(list); System.out.println(computerDefs); System.out.println("\nWhat word would you like to search for?"); String x = scan.next(); Word wordN = new Word(x); computerDefs.searchFor(wordN); } }
That might not be what you were looking for when you said minimally runnable lol....but the BST class itself takes two other classes to run so I figure I might as well post the exact thing giving me the error.
-
In Topic: NullPointerException when enQueueing User Created Queue Class
Posted 14 Apr 2013
pbl, on 14 April 2013 - 09:53 PM, said:this is ridiculous
for(i = 0; i < capacity; i++) ++count;
simply write: count = capacity;
I'm using the do-while loop there to break out of the for loop when the next space is "null". That way, if 5 objects are in the array, but the capacity is still 25, it will stop the count at 5 and return that number; thus giving me the "length" being the actual amount of objects in the array. I hope that makes sense because when I wrote it, it seemed like it would have worked for it's purpose at the time. It was an old method I created which I ended up not using.
Would you happen to know if there is anything wrong with my dequeue method? I'm having more trouble later on...might need a new topic but everytime I try to dequeue an object out of the array it doesn't seem to actually do it for some reason... -
In Topic: NullPointerException when enQueueing User Created Queue Class
Posted 14 Apr 2013
Ok, so I cleared my head and ran through the debugger one more time.
In my constructor (out of bad habits developed in an earlier class) I had decided to make the Queue that was being used set to null. I have deleted that and moved on and I think I am in the clear now.
Thanks for reading!
My Information
- Member Title:
- New D.I.C Head
- Age:
- Age Unknown
- Birthday:
- Birthday Unknown
- Gender:
Contact Information
- E-mail:
- Click here to e-mail me
Friends
|
|


Find Topics
Find Posts
View Reputation Given


|
Comments
Sirr_Purr_the_Cat has no profile comments yet. Why not say hello?