binary tree of strings in java - java

I'm trying to create a binary tree of strings with constructors and methods of searching for an element and printing a binary tree. In main I want to check if the string is in a binary tree. Can someone help me? Thank you in advance

Here is an implementation of binary search tree with strings :
public static void main(String[] args) throws IOException {
boolean toExit = false;
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BinaryTreeNode binaryTreeNode = new BinaryTreeNode("hello", "hello");
binaryTreeNode.put("world","world");
binaryTreeNode.put("ab","ab");
binaryTreeNode.put("cd","cd");
while(!toExit) {
System.out.println("Please enter string to search in the binary search tree or -1 to exit: ");
String input = bufferedReader.readLine();
if("-1".equals(input)){
toExit = true;
} else {
if (binaryTreeNode.get(input) == null) {
System.out.println("The input string does not exist");
} else {
System.out.println(input + " exists");
}
}
}
}
public static class BinaryTreeNode
{
private String key;
private Object value;
private BinaryTreeNode left, right;
public BinaryTreeNode(String key, Object value)
{
this.key = key;
this.value = value;
}
public Object get( String key )
{
if (this.key.equals(key))
{
return value;
}
if (key.compareTo(this.key) < 0 )
{
return left == null ? null : left.get(key);
}
else
{
return right == null ? null : right.get(key);
}
}
public void put(String key, Object value)
{
if (key.compareTo(this.key) < 0)
{
if (left != null)
{
left.put(key, value);
}
else
{
left = new BinaryTreeNode(key, value);
}
}
else if (key.compareTo(this.key) > 0)
{
if (right != null)
{
right.put(key, value);
}
else
{
right = new BinaryTreeNode(key, value);
}
}
else
{
this.value = value;
}
}
}
Hope that it helps...

Related

Chained Hashing Program; the method is undefined for the type error

I have made multiple methods, and they all have the error called
The method add(int, ChainHashEx.Data) is undefined for the type ChainHash<Integer,ChainHashEx.Data>
and there is another problem that is
The constructor ChainHash<Integer,ChainHashEx.Data>(int) is undefined
Why are the methods undefined? Is there something wrong with the class?
Or is there something wrong in someplace I am ignorant about.
adding code and the class with methods underneath.
code ChainHashEx :
package week14;
import java.util.Scanner;
public class ChainHashEx {
static Scanner sc = new Scanner(System.in);
static class Data {
private int no;
private String name;
public int keyCode() {
return no;
}
public String toString() {
return name;
}
void scanData(String guide, int sw) {
System.out.println(guide + "enter data to add");
if (sw == 1) {
System.out.print("number : ");
no = sc.nextInt();
System.out.print("name : ");
name = sc.next();
} else {
System.out.print("number : ");
no = sc.nextInt();
}
}
}
static void printMenu() {
System.out.println("1. add 2. delete 3. search 4. print 5. exit ");
}
public static void main(String[] args) {
int menu;
Data data;
Data temp = new Data();
ChainHash<Integer, Data> hash = new ChainHash<Integer, Data>(13);
do {
printMenu();
System.out.print("select menu: ");
switch(menu = sc.nextInt()) {
case 1 :
data = new Data();
data.scanData("add", 1);
hash.add(data.keyCode(), data);
break;
case 2 :
temp.scanData("delete", 2);
hash.remove(temp.keyCode());
break;
case 3 :
temp.scanData("search", 2);
Data t = hash.search(temp.keyCode());
if (t != null)
System.out.println("searched : " + t);
else
System.out.println("the data does not exist");
break;
case 4 :
hash.dump();
break;
}
} while (menu != 5);
System.out.println("stop program");
}
}
class ChainHash :
package week14;
public class ChainHash<K,V> {
class Node<K, V> {
private K key;
private V data;
private Node<K, V> next;
public Node(K key, V data, Node<K, V> next) {
this.key = key;
this.data = data;
this.next = next;
}
K getKey() {
return key;
}
V getValue() {
return data;
}
private int size;
private Node<K,V>[] table;
public void ChainHash(int capacity) {
try {
table = new Node[capacity];
this.size = capacity;
} catch (OutOfMemoryError e) {
this.size = 0;
}
}
public int hashValue(Object key) {
return key.hashCode() % size;
}
public V search(K key) {
int hash = hashValue(key);
Node<K,V> p = table[hash];
while (p != null) {
if (p.getKey().equals(key))
return p.getValue();
p = p.next;
}
return null;
}
public int add(K key, V data) {
int hash = hashValue(key);
Node<K,V> p = table[hash];
while (p != null) {
if (p.getKey().equals(key))
return 1;
p = p.next;
}
Node<K,V> temp = new Node<K,V>(key, data, table[hash]);
table[hash] = temp;
return 0;
}
public void dump() {
for (int i=0; i<size; i++) {
Node<K,V> p = table[i];
System.out.printf("%02d ", i);
while (p != null) {
System.out.printf("-> %s (%s) ", p.getKey(), p.getValue());
p = p.next;
}
System.out.println();
}
}
public int remove(K key) {
int hash = hashValue(key);
Node<K,V> p = table[hash];
Node<K,V> pp = null;
while (p != null) {
if (p.getKey().equals(key)) {
if (pp == null)
table[hash] = p.next;
else
pp.next = p.next;
return 0;
}
pp = p;
p = p.next;
}
return 1;
}
}
}
your ChainHash methods are defined inside the Node class, that's why they can't be found.
I think it's a "braces" error:
add a } after
V getValue() {
return data;
}
to "end" the Node class;
remove a } at the bottom of ChainHash;
remove void from the ChainHash constructor.
after these fixes the code should compile.

search(String target) method in a BinarySearchTree

I'm coding a dictionary system. This system should be run like this;
User enters a word that s/he wants to learn the definition.
The words and the definitions are storing in a linked list
while searching I should use a binary search tree
bst is comparing the words
search(String target) method should return the word+definition
Problems:
I've printed the linked list in a binary search tree but the method search() is couldn't return the word+definition? Where did I do a mistake?
public class BinarySearchTree {
private String data;
private BinarySearchTree left;
private BinarySearchTree right;
public BinarySearchTree() {
// I've deleted the getters/setters
public void addNode(String data) {
if (this.data == null) {
this.data = data;
} else {
if (this.data.compareTo(data)> 0) {
if (this.left != null) {
this.left.addNode(data);
} else {
this.left = new BinarySearchTree(data);
}
} else {
if (this.right != null) {
this.right.addNode(data);
} else {
this.right = new BinarySearchTree(data);
}
}
}
}
public boolean search(BinarySearchTree t,String key) {
if (t.data.equals(key)) return true;
if (t.left != null && search(t.left,key)) return true;
if (t.right != null && search(t.right,key)) return true;
return false;
}
}
public class Vocab {
public static void main(String args[])
{
LinkedList<String> ll
= new LinkedList<>();
Word word = new Word("Engineer", "Mühendis");
Word word2 = new Word("School", " Okul");
Word word3 = new Word("Pencil", "Kalem");
Word word4 = new Word("Window", "Pencere");
ll.add(word.toString());
ll.add(word2.toString());
ll.add(word3.toString());
ll.add(word4.toString());
for (int i = 0; i < ll.size(); i++) {
System.out.println(ll.get(i));
}
BinarySearchTree bst = new BinarySearchTree();
// Using the for each loop
for (String str : ll) {
bst.addNode(str);
}
System.out.println("search: " );
//here I want to return search() method and get the word+definition
}
}

Creating a binary search tree in java. But output is null

I am trying to create a rudimentary binary search tree in java with an insert and traverse method. The nodes have two local variables, a string and an int, the String value is used to sort the nodes.
Each BST has a local variable pointer to the root node and the nodes are inserted by traversing from the node. There seems to be a problem in creating the root node as my output is consistently producing null instead of.
THE
CAT
HAT
class BST
{
public Node root = null;
private class Node
{
private String key;
private int value;
private Node left;
private Node right;
public Node ()
{
}
public Node (String key, int value)
{
this.key = key;
this.value = value;
}
public String toString ()
{
return ("The key is: "+ this.key +" "+ this.value);
}
}
BST ()
{
}
public void put (String key, int value)
{
put (root, key, value);
}
private void put (Node x, String key, int value)
{
Node newNode = new Node(key, value);
if (x == null)
{
x = newNode;
System.out.println("new node added");
System.out.println(x);
}
int cmp = key.compareTo(x.key);
if (cmp < 0)
put(x.left, key, value);
else if (cmp > 0)
put(x.right, key, value);
else
x.value = value;
}
public void inorder (Node x)
{
if (x != null)
{
inorder (x.left);
System.out.println(x.key);
inorder (x.right);
}
}
public static void main (String [] args)
{
BST bst = new BST();
bst.put(bst.root,"THE", 1);
bst.put(bst.root,"CAT", 2);
bst.put("HAT", 1);
bst.inorder(bst.root);
}
}
Parameters are passed by value. Use the method's return value to alter something:
public void put (String key, int value)
{
root = put (root, key, value);
}
private Node put (Node x, String key, int value)
{
Node newNode = new Node(key, value);
if (x == null)
{
System.out.println("new node added");
System.out.println(x);
return newNode;
}
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left, key, value);
else if (cmp > 0)
x.right = put(x.right, key, value);
else
x.value = value;
return x;
}
Refer below link , good explanation of BST
http://www.java2novice.com/java-interview-programs/implement-binary-search-tree-bst/
A binary search tree is a node-based data structure, the whole idea of a binary search tree is to keep the data in sorted order so we can search the data in a little faster.There are three kinds of nodes are playing key role in this tree (Parent Node,Left Child Node & Right Child Node).The value of the left child node is always lesser than the value of the parent node, the same as the value of the right child node is always greater than the value of the parent node. Each parent node can have a link to one or two child nodes but not more than two child nodes.
Please find the source code from my tech blog - http://www.algonuts.info/create-a-binary-search-tree-in-java.html
package info.algonuts;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
class BinaryTreeNode {
int nodeValue;
BinaryTreeNode leftChildNode;
BinaryTreeNode rightChildNode;
public BinaryTreeNode(int nodeValue) {
this.nodeValue = nodeValue;
this.leftChildNode = null;
this.rightChildNode = null;
}
public void preorder() {
System.out.print(this.nodeValue+" ");
if(this.leftChildNode != null) {
this.leftChildNode.preorder();
}
if(this.rightChildNode != null) {
this.rightChildNode.preorder();
}
}
public void inorder() {
if(this.leftChildNode != null) {
this.leftChildNode.inorder();
}
System.out.print(this.nodeValue+" ");
if(this.rightChildNode != null) {
this.rightChildNode.inorder();
}
}
public void postorder() {
if(this.leftChildNode != null) {
this.leftChildNode.postorder();
}
if(this.rightChildNode != null) {
this.rightChildNode.postorder();
}
System.out.print(this.nodeValue+" ");
}
}
class BinaryTreeCompute {
private static BinaryTreeNode temp;
private static BinaryTreeNode newNode;
private static BinaryTreeNode headNode;
public static void setNodeValue(int nodeValue) {
newNode = new BinaryTreeNode(nodeValue);
temp = headNode;
if(temp != null)
{ mapping(); }
else
{ headNode = newNode; }
}
private static void mapping() {
if(newNode.nodeValue < temp.nodeValue) { //Check value of new Node is smaller than Parent Node
if(temp.leftChildNode == null)
{ temp.leftChildNode = newNode; } //Assign new Node to leftChildNode of Parent Node
else
{
temp = temp.leftChildNode; //Parent Node is already having leftChildNode,so temp object reference variable is now pointing leftChildNode as Parent Node
mapping();
}
}
else
{
if(temp.rightChildNode == null)
{ temp.rightChildNode = newNode; } //Assign new Node to rightChildNode of Parent Node
else
{
temp = temp.rightChildNode; //Parent Node is already having rightChildNode,so temp object reference variable is now pointing rightChildNode as Parent Node
mapping();
}
}
}
public static void preorder() {
if(headNode != null) {
System.out.println("Preorder Traversal:");
headNode.preorder();
System.out.println("\n");
}
}
public static void inorder() {
if(headNode != null) {
System.out.println("Inorder Traversal:");
headNode.inorder();
System.out.println("\n");
}
}
public static void postorder() {
if(headNode != null) {
System.out.println("Postorder Traversal:");
headNode.postorder();
System.out.println("\n");
}
}
}
public class BinaryTree {
//Entry Point
public static void main(String[] args) {
ArrayList <Integer> intList = new ArrayList <Integer>(Arrays.asList(50,2,5,78,90,20,4,6,98));
Iterator<Integer> ptr = intList.iterator();
while(ptr.hasNext())
{ BinaryTreeCompute.setNodeValue(ptr.next()); }
BinaryTreeCompute.preorder();
BinaryTreeCompute.inorder();
BinaryTreeCompute.postorder();
}
}
Adding to the answer by #Maurice,
Your code has several problems:
You expect JAVA to be pass by reference, when it is pass by value. You should use the code given by Maurice instead.
You are comparing "keys", when you should compare values.
I suggest that you use following modified code :
public class BST
{
public Node root = null;
private class Node
{
private String key;
private int value;
private Node left;
private Node right;
public Node ()
{
}
public Node (String key, int value)
{
this.key = key;
this.value = value;
}
public String toString ()
{
return ("The key is: "+ this.key +" "+ this.value);
}
}
BST ()
{
}
public void put (String key, int value)
{
root = putInTree (root, key, value);
}
private Node putInTree (Node x, String key, int value)
{
Node newNode = new Node(key, value);
if (x == null)
{
x = newNode;
System.out.println("new node added");
System.out.println(x);
return newNode;
}
//int cmp = key.compareTo(x.key);
if (value < x.value)
x.left = putInTree(x.left, key, value);
else /*if (value >= x.value)*/
x.right = putInTree(x.right, key, value);
/*else
x.value = value;*/
return x;
}
public void inorder (Node x)
{
if (x != null)
{
inorder (x.left);
System.out.println(x.key);
inorder (x.right);
}
}
public static void main (String[] args)
{
BST bst = new BST();
bst.put("THE", 1);
bst.put("CAT", 2);
bst.put("HAT", 1);
bst.inorder(bst.root);
}
}

Binary search tree Searching troubleshooting

I've spent about two days pouring over this and I have no idea what's missing. Originally my BST used comparables, then I switched it to int to simplify it when it wasn't working.
I add several items to a tree and it successfully prints them out in order. Then, the first time I call the search() method it returns true, as it should. Every other search call after that returns false whether it is true or false.
I'm including most of my code here in case the problem isn't related with the search method itself.
The output SHOULD be: 4 12 23 27 30 42 60 84 true true false true true
but instead I get: 4 12 23 27 30 42 60 84 true false false false false
public class BSTree {
TreeNode root;
static int comparison;
public void insert(int value) {
if (root == null) {
root = new TreeNode(value);
}
else {
root.insert(value);
}
}
public boolean search(int chicken) {
if (root != null ) {
return root.search(chicken);
}
return false;
}
public static int height(TreeNode b) {
return TreeNode.height(b);
}
public static void CompareSet() {
comparison++;
}
public int getCompare() {
return comparison;
}
public void ResetCompare() {
comparison = 0;
}
public static void traverseInOrder (TreeNode node) {
if (node != null) {
traverseInOrder(node.left);
System.out.print(" " + node.data);
traverseInOrder (node.right);
}
}
public static void main(String args[]) {
BSTree tree = new BSTree();
tree.insert(30);
tree.insert(42);
tree.insert(84);
tree.insert(12);
tree.insert(4);
tree.insert(23);
tree.insert(27);
tree.insert(60);
traverseInOrder(tree.root);
System.out.println("\n" + tree.search(30));
System.out.println("\n" + tree.search(4));
System.out.println("" + tree.search(50));
System.out.println("" + tree.search(27));
System.out.println("" + tree.search(42));
System.out.println(height(tree.root));
}
}
Here is the treeNode class:
public class TreeNode<T> {
int data;
TreeNode left;
TreeNode right;
TreeNode(int value){
this.data = value;
//right = null;
//left = null;
}
public void insert(int value) {
if (value == data) {
return;
}
if (value < data) {
if (left == null) {
left = new TreeNode(value);
}
else {
left.insert(value);
}
}
else {
if (right == null) {
right = new TreeNode(value);
}
else {
right.insert(value);
}
}
public boolean search(int value) {
BSTree.CompareSet();
if (data == value) return true;
if (data < value && left!=null)
return left.search(value);
else if(data > value && right != null)
return right.search(value);
return false;
}
public static int height(TreeNode b) {
if (b == null) return -1;
return 1 + Math.max(height(b.left), height(b.right));
}
public int getData(){
return data;
}
public TreeNode getLeftChild() {
return left;
}
public void setLeftChild(TreeNode leftChild) {
this.left = leftChild;
}
public TreeNode getRightChild() {
return right;
}
public void setRightChild(TreeNode rightChild) {
this.right = rightChild;
}
}
Your comparaisons are reversed :))) Here is the corrected TreeNode::search method :
public boolean search(int value) {
BSTree.CompareSet();
if (data == value) return true;
if (data > value && left!=null)
return left.search(value);
else if(data < value && right != null)
return right.search(value);
return false;
}
I think you swapped the data and the value variables.
just change your search method of TreeNode class to this:
public boolean search(int value) {
Test.CompareSet();
if (data == value) return true;
if (data < value && right!=null)
return right.search(value);
else if(data > value && left != null)
return left.search(value);
return false;
}
When your current node's data is greater than value you need to go to left sub-tree. And when current node's data is smaller than value you need to go to right sub-tree. You just have done the reverse one.

Binary search tree generic

I have the following classes and I want to let the user choose whether he wants to create a BST with integers or a BST with strings. How can i create a BST from integers when the user choose 5 or create a BST from strings when the user press 6? Also if anyone find something wrong with my generics pls let me know!
Thanks a lot
public class BSTNode <T extends Comparable<T>>
{
T value;
BSTNode<T> left;
BSTNode<T> right;
public BSTNode(T value, BSTNode<T> l,BSTNode<T> r)
{
this.value = value;
left = l;
right = r;
}
public BSTNode(T value)
{
this(value,null,null);
}
public T getValue()
{
return value;
}
public void setValue(T value)
{
this.value = value;
}
public BSTNode<T> getLeftChild()
{
return left;
}
public BSTNode<T> getRightChild()
{
return right;
}
public void setLeftChild(BSTNode<T> node)
{
left = node;
}
public void setRightChild(BSTNode<T> node)
{
right = node;
}
public boolean search(T value)
{
if (value.equals(this.value))
return true;
else if (value.compareTo(this.value) < 0)
{
if (left == null)
return false;
else
return left.search(value);
} else if (value.compareTo(this.value) > 0)
{
if (right == null)
return false;
else
return right.search(value);
}
return false;
}
public boolean add(T value)
{
if (value.compareTo(this.value)==0)
return false;
else if (value.compareTo(this.value) < 0)
{
if (left == null)
{
left = new BSTNode<T>(value);
return true;
} else
return left.add(value);
}
else if (value.compareTo(this.value) > 0)
{
if (right == null)
{
right = new BSTNode<T>(value);
return true;
}
else
return right.add(value);
}
return false;
}
public boolean remove(T value2, BSTNode<T> parent)
{
if (value2.compareTo(this.value)<0)
{
if (left != null)
return left.remove(value2, this);
else
return false;
}
else if (value2.compareTo(this.value)>0)
{
if (right != null)
return right.remove(value2, this);
else
return false;
}
else
{
if (left != null && right != null)
{
this.value = right.minValue();
right.remove(this.value, this);
}
else if (parent.left == this)
{
parent.left = (left != null) ? left : right;
}
else if (parent.right == this)
{
parent.right = (left != null) ? left : right;
}
return true;
}
}
public T minValue()
{
if (left == null)
return value;
else
return left.minValue();
}
}
public class BinarySearchTree <T extends Comparable<T>>
{
private BSTNode<T> root;
public BinarySearchTree(T value)
{
root = new BSTNode<T>(value);
}
public BSTNode getRoot()
{
return root;
}
public boolean search(T value)
{
if (root.equals(null))
return false;
else
return root.search(value);
}
public boolean add(T value)
{
if (root == null) {
root = new BSTNode(value);
return true;
} else
return root.add(value);
}
public boolean remove(T value) {
if (root == null)
return false;
else {
if (root.getValue() == value) {
BSTNode auxRoot = new BSTNode(null);
auxRoot.setLeftChild(root);
boolean result = root.remove(value, auxRoot);
root = auxRoot.getLeftChild();
return result;
} else {
return root.remove(value, null);
}
}
}
public static void displayInorder(BSTNode T)
{
if (T!=null)
{
if (T.getLeftChild()!=null)
{
displayInorder(T.getLeftChild());
}
System.out.print(T.getValue() + " ");
if(T.getRightChild()!=null)
{
displayInorder(T.getRightChild());
}
}
}
}
import java.util.Scanner;
public class main {
public static void main(String[] args) {
BinarySearchTree b = new BinarySearchTree(null);
boolean flag = true;
while (flag) {
Scanner scan = new Scanner(System.in);
System.out.println("Select 1 to add values in to BST\n"
+ "Select 2 to delete values from the BST \n"
+ "Select 3 to search for a value\n"
+ "Select 4 to display te values held in the BST\n"
+ "Select 5 to create a BST of strings\n"
+ "Select 6 to create a BST of integers\n"
+ "Select 7 to exit" );
int opt = scan.nextInt();
switch (opt) {
case 1: System.out.println("Insert the value of your choice: ");
String str = scan.next();
b.add(str);
break;
case 2: System.out.println("Insert the value of your choice: ");
str = scan.next();
b.remove( str);
break;
case 3:
System.out.println("Insert the value of your choice: ");
str = scan.next();
b.search(str);
break;
case 4:
BinarySearchTree.displayInorder(b.getRoot());
break;
case 5:
case 7:
flag=false;
break;
}
}
}
}
In order to get the most of generics in your code this is my suggestions:
I would add a method to process a string (the user input) into the appropriate type in the tree class:
...
import java.util.function.Function;
...
public class BinarySearchTree <T extends Comparable<T>>
{
private BSTNode<T> root;
private Function<String,T> valueDecoder
public BinarySearchTree(final Function<String,T> valueDecoder)
{
this.valueDecoder = valueDecoder;
root = new BSTNode<T>(null);
}
...
public boolean decodeAndAdd(final String encodedValue) {
return add(valueDecoder.apply(encodedValue));
}
public boolean decodeAndRemove(final String encodedValue) {
return remove(valueDecoder.apply(encodedValue));
}
}
```
Then you would leave the b variable undefined/null until you actually now the type of tree given the choice provided by the user. Since it might contain String or Integer here you can only use ? as the type parameter, perhaps ? extends Comparable<?> as that is part of the constraint... ? is fine in this case:
BinarySearchTree<?> b = null;
Now when the user ask for a String or Integer tree you need to provide the appropriate lambda to transfer the scanned string into the actual element value:
case 5:
b = new BinarySearchTree<>(scanStr -> scanStr);
break;
case 6:
b = new BinarySearchTree<>(scanStr -> Integer.parseInt(scanStr));
break;
Now add and remove are trivial:
case 1:
b.decodeAndAdd(scan.next());
break;
case 2:
b.decodeAndRemove(scan.next());
break;
If the user provides a non-valid integer string value when the tree is an Integer tree it would result in a NumberFormatException and the program would stop. Perhaps you would rather show an error message and allow the user to do another operator. For that:
case 6:
b = new BinarySearchTree<>(scanStr -> {
try {
return Integer.parseInt(scanStr);
} catch (NumberFormatException ex) {
throw new IllegalArgumentException("you must provide a valid integer value: '" + scanStr + "'");
}
});
break;
...
case 1:
try {
b.decodeAndAdd(scan.next());
} catch (final IllegalArgumentException ex) {
System.err.println("ERROR: " + ex.getMessage());
}
break;
case 2:
try {
b.decodeAndRemove(scan.next());
} catch (final IllegalArgumentException ex) {
System.err.println("ERROR: " + ex.getMessage());
}
break;
Perhaps is not ideal to add decodeAndAdd and decodeAndRemove to your BinarySearchTree class if you want to keep things a bit more modular as the BST might be used outside the user command line context described in the question.
In that case you could define a generic "struct" like class that contains a reference that contains a reference to the BST and the decoding lambda with their element type bound to be the same using a type-parameter. You could also extends the BST class in another user-interface specialized BST that add this functionality:
class CommandLineBST<T> {
public final BST<T> tree;
public final Function<String, T> valueDecoder;
public CommandLineBST(final BST<T> tree, final Function<String, T> decoder) {
this.tree = tree;
this.valueDecoder = decoder;
}
public boolean add(final String scanStr) {
return tree.add(valueDecoder.apply(scanStr));
}
public boolean remove(final String scanStr) {
return tree.remove(valueDecoder.apply(scanStr));
}
}
or
class CommandLineBST<T> extends BST<T> {
private Function<String, T> valueDecoder;
public CommandLineBST(final Function<String, T> valueDecoder) {
super(null);
this.valueDecoder = valueDecoder;
}
public boolean decodeAndAdd(final String scanStr) { ... }
public boolean decodeAndRemove(final String scanStr) { ... }
}

Categories

Resources