Intellij idea is giving me this error in class internalNode when trying to compare two T values: compare(capture,capture) cannot be applied to (T,T). I can't understand why. I've already changed the parametrized type to T and it still shows me the message but instead of capture just T.
Any help is very appreciated
public class BSpecialTree<T> {
private Node<T> root;
private int keysNumber;
private Comparator<? super T> cmp;
public BSpecialTree(Comparator<? super T> cmp, int keysNumber) {
this.cmp = cmp;
this.keysNumber = keysNumber;
}
public boolean belongs(T key) {
if(key == null) {
if(root == null)
return true;
return false;
}
return root.belongs(key);
}
private interface Node<T> {
boolean belongs(T key);
}
private class internalNode<T> implements Node<T> {
private T key;
private Node<T> left, right;
public internalNode(T key) {
this.key = key;
}
public boolean belongs(T key) {
if(cmp.compare(this.key, key) > 0) //ERROR
return left.belongs(key);
return right.belongs(key);
}
}
private class leafNode<T> implements Node<T> {
private List<T> keys;
public leafNode() {
keys = new ArrayList<T>(keysNumber);
}
public boolean belongs(T key) {
return keys.indexOf(key) != -1;
}
}
Your inner classes are introducing their own generic type <T> which hides <T> from BSpecialTree<T>, but comparator is declared to be using T from BSpecialTree, not from internalNode.
Maybe try with
private class internalNode implements Node<T> {
// ^remove `<T>` from here
Related
For this assignment I need to create a linked stack class that contains a getMin() and getMax(). I cannot change the class header which was provided by the instructor. Both getMin and getMax should be O(1) time.
My thought is that I need to use the compareTo method to compare entries as they are pushed or poped so that I can set variables minValue and maxValue equal to their respective values. However, I don't understand the section in the class header <T extends Comparable<? super T>> nor do I know how or where to implement Comparable. I tried having my class Node<E> implement Comparable but it asked me to override the compareTo method and I'm not sure how that would work.
Any help would be greatly appreciated! Below is my code for this assignment:
public class MinMaxStack <T extends Comparable<? super T>> implements StackADT<T> {
private Node<T> top;
private int size;
public MinMaxStack() {
clear();
}
private class Node<E>{
E data;
Node<E> previous;
}
public T getMin() {
if(isEmpty()) {
throw new EmptyCollectionException("The stack is empty but is trying to getMin.");
} else {
return null;
}
}
public T getMax() {
return null;
}
#Override
public T pop() {
if(isEmpty()) {
throw new EmptyStackException("Stack is empty but trying to pop.");
}else {
T dataToReturn = top.data;
top = top.previous;
size -= 1;
return dataToReturn;
}
}
#Override
public T peek() {
if(isEmpty()) {
throw new EmptyStackException("Stack is empty but trying to peek");
}else {
return top.data;
}
}
#Override
public void push(T newItem) {
Node<T> newNode = new Node<>();
newNode.data = newItem;
if(!isEmpty()) {
newNode.previous = top;
}
top = newNode;
size += 1;
}
#Override
public int getSize() {
return size;
}
#Override
public void clear() {
while(!isEmpty()) {
top = null;
size = 0;
}
}
#Override
public boolean isEmpty() {
return size == 0;
}
}
In order to practice data structures I'm implementing my own library of Trees. I've begun with BST and in following I'm about to implement AVL Tree, Red-Black Tree and maybe more. AVL & RBT are BST trees as well, so some class hierarchy is rather obvious. The problem I came into is that all those trees have other types of Nodes - AvlNode has balance factor flag, RgbNode has color flag, BstNode doesn't need any additional info (despite of references to parent, children and value which all Nodes need). So I've a hierarchy of Nodes and a hierarchy of Trees. I could give some flag attribute to BstNode and use it in extending classes but it is surely not a good way to do it.
The problem is how to deal with the fact that for example Bst.findNode() will return BstNode but in Avl I need AvlNode despite the findNode() methods will be the same in both (apart of return type).
I need help with planning the hierarchies or if those parallel hierarchies (as a code smell) are in general a bad idea, I need a workaround because I have no clue how to do it in proper way.
BstTree Class:
public class BstTree<T extends Comparable> implements Iterable
{
private BstNode<T> root;
public void addValue(T value)
{
BstNode node = new BstNode(value);
addNode(node);
}
public void addNode(BstNode<T> node)
{
...
}
public boolean removeNode(T value)
{
...
}
public BstNode findNode(T value)
{
...
}
//other less significant methods
}
BstNode class:
public class BstNode<T extends Comparable>
{
private static int lastId = 0;
private int id;
private T value;
private BstNode parent = null;
private BstNode leftChild = null;
private BstNode rightChild = null;
public BstNode(T value) {
this.id = ++lastId;
this.value = value;
}
public boolean isGreaterThan(BstNode n)
{
//...
}
public boolean hasLeftChild()
{
//...
}
public boolean hasRightChild()
{
//...
}
public boolean hasParent()
{
//...
}
public boolean isLeaf()
{
//...
}
public boolean hasOnlyOneChild()
{
//...
}
public BstNode getOnlyChild(BstNode node)
{
...
}
public boolean isLeftChildren()
{
...
}
public BstNode getConsequentNode()
{
...
}
}
I can guess that the separation of responsibilities above may not be perfect, if it's wrong then I might get some of the methods from Node to Tree class but this thing is not a big problem.
I would do something like this:
public abstract class BstTree<T extends Comparable,N extends BstNode<T,N>> {
private N root;
...
public void addValue(T value)
{
N node = newNode(value);
addNode(node);
}
public abstract N newNode(T value);
public void addNode(N node)
{
// ...
}
}
public class BstNode<T extends Comparable,N extends BstNode<T,N>>
{
private T value;
private N parent = null;
private N leftChild = null;
private N rightChild = null;
public BstNode(T value) {
this.value = value;
}
public N getOnlyChild(N node)
{
// ...
}
...
}
public class AVLTree<T extends Comparable> extends BstTree<T,AVLNode<T>> {
...
#Override
public AVLNode<T> newNode(T value) {
return new AVLNode<>(value);
}
}
public class AVLNode<T extends Comparable> extends BstNode<T,AVLNode<T>> {
...
public AVLNode(T value) {
super(value);
}
#Override
public AVLNode<T> getOnlyChild(AVLNode<T> node) {
return super.getOnlyChild(node);
}
...
}
I am new to generics and want to solve a little Task.
I want to give two Objects of type "V extends Comparable" to the class ComparePredicate and then check in method "isOk" if the int-value "value" of a Tree class is between these two Objects.
I choosed the compareTo method because Integer and V should be of type comparable but the compiler gives an error.
I think its just an syntactical problem.
So how do i need to write it correct. Hope you guys can help me.
Thanks for your answers.
Class ComparePredicate
public class ComparePredicate<V extends Comparable<V>> implements TreePredicate<V> {
V minEle;
V maxEle;
public ComparePredicate(V minEle, V maxEle) {
this.minEle = minEle;
this.maxEle = maxEle;
}
#Override
public boolean isOk(Tree<V> tree) {
return minEle.compareTo(Integer.valueOf(tree.getValue())) > 0 &&
maxEle.compareTo(Integer.valueOf(tree.getValue())) < 1;
//COMPILER ERROR: "The method compareTo(V) in the type Comparable<V> is not applicable for the arguments (Integer)"
return false;
}
}
Class Tree
public class Tree<T> {
private int value;
private final Tree<T> left;
private final Tree<T> right;
public Tree(int v, Tree<T> l, Tree<T> r) {
this.value = v;
this.left = l;
this.right = r;
}
public int getValue() {
return this.value;
}
public Tree<T> getLeft() {
return this.left;
}
public Tree<T> getRight() {
return this.right;
}
}
Change Tree class
static class Tree<T> {
private T value;
private final Tree<T> left;
private final Tree<T> right;
public Tree(T v, Tree<T> l, Tree<T> r) {
this.value = v;
this.left = l;
this.right = r;
}
public T getValue() {
return this.value;
}
// getters ...
}
And also change isOk()
#Override
public boolean isOk(Tree<V> tree) {
return minEle.compareTo(tree.getValue()) <= 0 &&
maxEle.compareTo(tree.getValue()) >= 0;
}
import tree.BinaryTree;
public class TreeMap<K extends Comparable<K>, V> implements MyMap<K, V> {
private BinaryTree<Element> map;
java.util.Set<K> keys;
private int size;
#Override
public java.util.Set<K> keySet() {
inorder(map);
return keys;
}
public String toString() {
return map.toString();
}
private class Element {
K key;
V value;
public Element(K key, V value) {
this.key = key;
this.value = value;
}
public int compareTo(Element that) {
return this.key.compareTo(that.key);
}
public String toString() {
return (key.toString());
}
}
private void inorder(BinaryTree<Element> tree) {
if (tree != null) {
inorder(tree.getLeft());
keys.add(tree.getRoot().key);
inorder(tree.getRight());
}
}
}
Hey guys! :)
I'm having a lot of trouble with adding keys in inorder to my set of keys.
How do I append keys to the set? :/
The worst part is that it isn't showing me the exception all it says is
at TreeMap.inorder(TreeMap.java:188)
at TreeMap.keySet(MyTreeMap.java:60)
at TreeMap.main(MyTreeMap.java:244)
Java Result: 1
This is just a snippet of my code, everything works but my keySet/inorder methods. Line 188 on my code is
keys.add(tree.getRoot().key);
I have searched, retried, and searched again but cant get anywhere. I would appreciate any help you guys can give me.
Thanks in advance! :)
Here is the BinaryTree class
public class BinaryTree<E>
{
private E root;
private BinaryTree<E> left;
private BinaryTree<E> right;
public BinaryTree(E paramE, BinaryTree<E> paramBinaryTree1, BinaryTree<E> paramBinaryTree2)
{
this.root = paramE;
this.left = paramBinaryTree1;
this.right = paramBinaryTree2;
}
public BinaryTree(E paramE)
{
this(paramE, null, null);
}
public E getRoot()
{
return (E)this.root;
}
public BinaryTree<E> getLeft()
{
return this.left;
}
public BinaryTree<E> getRight()
{
return this.right;
}
public E setRoot(E paramE)
{
Object localObject = this.root;
this.root = paramE;
return (E)localObject;
}
public BinaryTree<E> setLeft(BinaryTree<E> paramBinaryTree)
{
BinaryTree localBinaryTree = this.left;
this.left = paramBinaryTree;
return localBinaryTree;
}
public BinaryTree<E> setRight(BinaryTree<E> paramBinaryTree)
{
BinaryTree localBinaryTree = this.right;
this.right = paramBinaryTree;
return localBinaryTree;
}
public String toString()
{
StringBuilder localStringBuilder = new StringBuilder("" + this.root);
if (!isLeaf())
{
localStringBuilder.append("(");
if (this.left != null) {
localStringBuilder.append(this.left);
}
if (this.right != null) {
localStringBuilder.append("," + this.right);
}
localStringBuilder.append(")");
}
return localStringBuilder + "";
}
public boolean isLeaf()
{
return (this.left == null) && (this.right == null);
}
}
the TreeSet, according to your code, was never initialized
TreeSet ts = new TreeSet();
I think that should fix it...
Hello I would like to know how to set up an object oriented BST class that has a private Node class.(Both classes being generic)
so far I have this but i am having some compilation errors. Some explanation would be nice. I copied this code, but I know there are mistakes to fix. Also how would you set up the constructor of the bst?
#SuppressWarnings("unchecked")
//public class BinarySearchTree<T extends Comparable<? super T>> {
public class BST<T extends Comparable<T>> implements Iterable<T>
{
private Node <T> root;
// public BST(){
// root=null;
// }
private T search(T target, BST <T> p)
{
int comp=target.compareTo(p.data);
T c=target.compareTo(P.data);
if(comp==0)
return c;
}
private class Node<T extends Comparable<T>> implements Iterable {
T data;
Node<T> left, right;
public Node(T t)
{
data=t;
}
#Override
public Iterator iterator() {
// TODO Auto-generated method stub
return null;
}
public T search(T target)
{
return search(target, root);
}
}
#Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
}
The default constructor should be fine that you've originally commented out should be fine. Is there a particular use case you have in mind that it doesn't satisfy?
Something like this. Because the Node class is a private inner class, it doesn't have to be generic and can instead use the type specified in its parent class, which is what I assume you want anyway.
The node class doesn't really need a search method because it only contains one value. There's no need to search if there's just one value. This is also the same reason it doesn't need a iterator either. There's really no need to iterate over just one value.
When designing an abstract data type such as a BST, it's good to consider how you envision it will be used: what operations should it support, aka. its API. The implementation below supports 2 operations: insert and search. Possible extensions might include a remove and/or a contains operation.
Operations on a tree are typically recursive. This is because you start at the root and have to traverse through inner nodes which themselves can be viewed as roots of their respective subtrees. Try walking through a few example inserts and searches to convince yourself why it works that way.
import java.util.Iterator;
public class BST<T extends Comparable<T>> implements Iterable<T> {
private Node root;
public BST(){
root=null;
}
private void insertInternal(T value, Node parent) {
int comp=value.compareTo(parent.data);
if(comp < 0) {
if(parent.left == null) {
parent.left = new Node(value);
}
else {
insertInternal(value, parent.left);
}
}
else if(comp > 0) {
if(parent.right == null) {
parent.right = new Node(value);
}
else {
insertInternal(value, parent.right);
}
}
}
public void insert(T value) {
if(root == null) {
root = new Node(value);
return;
}
insertInternal(value, root);
}
private Node searchInternal(T target, Node node) {
if(node == null) {
return null;
}
int comp=target.compareTo(node.data);
if(comp < 0) {
return searchInternal(target, node.left);
}
else if(comp > 0) {
return searchInternal(target, node.right);
}
return node;
}
public Node search(T target) {
return searchInternal(target, root);
}
private class Node {
T data;
Node left, right;
public Node(T t) {
data=t;
}
}
#Override
public Iterator<T> iterator() {
// TODO Auto-generated method stub
return null;
}
public static void main(String[] args) {
BST<Integer> bst = new BST<Integer>();
bst.insert(2);
bst.insert(6);
System.out.println(bst.search(2) != null);
System.out.println(bst.search(6) != null);
System.out.println(bst.search(8) == null);
}
}