How do you insert items into a binary tree in java so that they are in order? I want to use random values and sort them from smallest to largest then insert them into a binary tree in this order:
1
2 3
4 5 6 7
8 9
When you say 'in order' you need to clarify, do you mean in sorted order or do you mean insertion order, etc.
There are lots of resources available on inserting into binary trees or the difference between to types of binary trees, or how to print a binary tree diagram, that I suspect this is a duplicate.
What is different about your example? Having '1' as the root node means you must not have a rebalancing tree since both '2' and '3' are larger than the value for your root node. Your insert rule seems inconsistent since if '1' is the first node inserted then all other values will cascade to the right branch of the tree unless you use a different rule for the root then at the other levels which would be a pain to code.
Something like this?:
public class BinaryTree {
private List<Integer> list = new ArrayList<Integer>();
public class BinaryTreeNode {
private int p;
public BinaryTreeNode(int p) {
this.p = p;
}
private BinaryTreeNode getChild(int childP){
BinaryTreeNode result= null;
if (childP < list.size()){
result = new BinaryTreeNode(childP);
}
return result;
}
public BinaryTreeNode getLeft(){
return getChild(p*2+1);
}
public BinaryTreeNode getRight(){
return getChild(p*2+2);
}
public int getValue(){
return list.get(p);
}
}
public void add(int item){
list.add(item);
}
public BinaryTreeNode getRoot(){
BinaryTreeNode result = null;
if (!list.isEmpty()){
result = new BinaryTreeNode(0);
}
return result;
}
}
In Naftalin, Walder Java Collections and Generics I've faced with this implementation that I love best:
public interface TreeVisitor<E, R> {
public R visit(E leaf);
public R visit(E value, Tree<E> left, Tree<E> right);
}
public abstract class Tree<E> {
public abstract <R> R accept(TreeVisitor<E, R> visitor);
public static <E> Tree<E> leaf(final E leaf) {
return new Tree<E>() {
#Override
public <R> R accept(TreeVisitor<E, R> visitor) {
return visitor.visit(leaf);
}
};
}
public static <E> Tree<E> branch(final E value, final Tree<E> left, final Tree<E> right){
return new Tree<E>(){
#Override
public <R> R accept(TreeVisitor<E, R> visitor) {
return visitor.visit(value, left, right);
}
};
}
}
Now you can add any operation you want and create your tree as follows:
Tree<Integer> t = Tree.branch(1,
Tree.branch(2,
Tree.branch(4, Tree.leaf(8), Tree.leaf(9)), Tree.leaf(5)),
Tree.branch(3, Tree.leaf(6), Tree.leaf(7));
I found the answer that I needed from this one.
Create a Complete Binary Tree using Linked Lists w/o comparing node values
Some of the other things I was pointed to, either weren't quite what I wanted, or didn't work past like 8 or so values.
Related
One of the advantages of streams is that you can avoid visiting the whole structure for some operations, like anyMatch or filter+findFirst.
However, if you have your own data structure, depending on how you turn it into a stream you may end up visiting it all anyway.
What is the right way to turn a custom tree data type into a stream?
Consider the following example:
interface Tree{
void forEach(Consumer<Integer> c);
}
final class EmptyTree implements Tree{
public void forEach(Consumer<Integer> c){}
}
interface NonEmptyTree extends Tree{}
record Leave(int label) implements NonEmptyTree{
public void forEach(Consumer<Integer> c){
System.out.println("In forEachLeave "+label);
c.accept(label);
}
}
record Node(NonEmptyTree left, NonEmptyTree right) implements NonEmptyTree{
public void forEach(Consumer<Integer> c){
left.forEach(c); right.forEach(c);
}
}
The two main ways to turn a tree into a stream would be
var sb=Stream.<Integer>builder();
myTree.forEach(sb);
sb.build()
or
Stream.of(myTree).mapMulti(Tree::forEach)
However, both of them call forEach, thus both of them will visit all the tree (and call the prints for all the labels, in this example).
How do you implement a .stream() method in the Tree type so that it would not even visit the whole tree if it is not needed? (because of .anyMatch, for example)
Ok, I sorted it.
I'm quite sure that what I'm doing is pretty standard with immutable trees
(parent fields only make sense in mutable trees)
Here is my result, for reference for future programmers doing streams on immutable trees.
The class TreeIterator<E> is the one really relevant to this ordeal.
I could make nested classes to be able to make more stuff private, but as a code example I think it is more clear in this non nested form.
interface Tree<E> extends Iterable<E>{
Tree<E> and(Tree<E> other);
default Tree<E> left(){ return empty(); }
default Tree<E> right(){ return empty(); }
default E label(Supplier<E> orElse){ return orElse.get(); }
#SuppressWarnings("unchecked")
static <E> Tree<E> empty(){ return (Tree<E>)EmptyTree.empty; }
static <E> Tree<E> leaf(E label){ return new Leaf<E>(label); }
default Stream<E> stream(){ return StreamSupport.stream(spliterator(), false); }
}
final class EmptyTree<E> implements Tree<E>{
public Tree<E> and(Tree<E> other){ return other; }
private EmptyTree(){} //Singleton pattern: only one EmptyTree can exists
static final Tree<?> empty = new EmptyTree<>();
public Iterator<E> iterator(){ return List.<E>of().iterator(); }
public String toString(){ return "<EMPTY>"; }
}
interface NonEmptyTree<E> extends Tree<E>{
Leaf<E> itAdvance(ArrayList<NonEmptyTree<E>> stack);
default Tree<E> and(Tree<E> other){
if (!(other instanceof NonEmptyTree<E> net)){ return this; }
return new Node<E>(this, net);
}
}
record Leaf<E>(E label) implements NonEmptyTree<E>{
public E label(Supplier<E> orElse){ return label; }
public Leaf<E> itAdvance(ArrayList<NonEmptyTree<E>> stack){ return this; }
public Iterator<E> iterator(){ return List.<E>of(label).iterator(); }
public String toString(){ return label+""; }
}
record Node<E>(NonEmptyTree<E> left, NonEmptyTree<E> right) implements NonEmptyTree<E>{
public Node{ assert left!=null && right!=null; }//null is not a valid tree
public Leaf<E> itAdvance(ArrayList<NonEmptyTree<E>> stack){
stack.add(right);
return left.itAdvance(stack);
}
public Iterator<E> iterator(){ return new TreeIterator<E>(this); }
public String toString(){ return "("+left+", "+right+")"; }
}
class TreeIterator<E> implements Iterator<E>{
private final ArrayList<NonEmptyTree<E>> stack = new ArrayList<>(32);
public boolean hasNext(){ return !stack.isEmpty(); }
public TreeIterator(Node<E> n){ stack.add(n); }
public E next(){
if(stack.isEmpty()){ throw new NoSuchElementException(); }
var last=stack.remove(stack.size()-1);
return last.itAdvance(stack).label();
}
}
Looking at record definition Leave(int label) implements NonEmptyTree, I have two questions:
Did you mean "leaf"?
A tree consists of nodes (either a leaf or an internal node), but a node or leaf do not implement a tree, i.e., they are not are specific type of a tree. Are you sure about your node/leaf/tree implementation?
I would recommend a simple implementation like this one here: https://www.baeldung.com/java-binary-tree
When it comes to stream, you have two options:
Implement your own Stream-enabled class (see discussion here)
Provide a method that returns a (specific) stream, e.g. filtered.
Keep in mind that there are many different trees out there, e.g. red–black tree, n-ary tree, AVL Tree, B-Tree ...
I do not see the use of the interface,
why can't we directly implement the getElement() method directly in the Node Class?
public interface Position <T> {
public T getElement();
}
Hereby the SNODE class:
public class SNode<T> implements Position<T> {
private T element;
private SNode<T> next;
public SNode(T e, SNode<T> n) {
element = e;
next = n;
}
public SNode<T> getNext() {
return next;
}
public void setNext(SNode<T> next) {
this.next = next;
}
public void setElement(T element) {
this.element = element;
}
#Override
public T getElement() {
return element;
}
}
The position interface provides a general abstraction for the location of an element within a structure. A position acts as a marker/token within a broader list. A position p, which is associated with some element e in a list L, does not change, even if the index of e changes in L due to insertions or deletions elsewhere in the list. Nor does position p change if we replace the element e stored at p with another element. The only way in which a position becomes invalid is if that position is removed.
The reason for having it's definition of a position type allows position to serve as parameters to some methods and return values from other methods of a list.
I have (recursively) defined a class for implementing a binary tree (in Java):
class BinaryTree {
protected int key;
protected BinaryTree left, right;
// some methods...
}
from which I want to implement a binary search tree, like this:
class BinarySearchTree extends BinaryTree {
// ...
public BinarySearchTree search(int x) {
if (x == key)
return this;
if (x < key)
if (left != null)
return left.search(x); // (*)
else
if (right != null)
return right.search(x); // (*)
return null;
}
}
but of course the lines marked with // (*) won't compile beacause left and right are just BinaryTrees, without any search() method.
So I am wondering if theres is a way to define BinarySearchTree from the BinaryTree superclass but with left and right being actually BinarySearchTrees.
Or maybe there is a better way of implementing the relationship between binary trees and the search ones: should I define a separate Node class? should I use templates? should I avoid recursive definitions at all? ...
You can use recursive generics.
Define a recursive generic type variable, say, B:
class BinaryTree<B extends BinaryTree<B>> {
and make your fields of this type:
protected B left, right;
Then define:
class BinarySearchTree extends BinaryTree<BinarySearchTree> {
Now left and right are of type BinarySearchTree too, allowing you to call left.search and right.search.
I feel BinaryTreeNode should be created as an inner class ofBinaryTree.java. BinaryTreeNode can have int data, and two references of type BinaryTreeNode for left and right node
BinaryTree.java should have an reference of type BinaryTreeNode which will be the root of the tree.
Now BinarySearchTree extends BinaryTree looks good, you can include an method in it as below signature.
BinaryTreeNode `search( int k, BinaryTreeNode root)`
Now you can define the recursive method.
Please see Sample code with basic skeleton.
BinaryTreeNode.java
public class BinaryTreeNode {
private int data;
private BinaryTreeNode left, right;
public BinaryTreeNode(int data) {
this.setData(data);
}
public BinaryTreeNode getLeft() {
return left;
}
public void setLeft(BinaryTreeNode left) {
this.left = left;
}
public BinaryTreeNode getRight() {
return right;
}
public void setRight(BinaryTreeNode right) {
this.right = right;
}
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
}
BinaryTree.java
public class BinaryTree {
protected BinaryTreeNode root;
// other basic methods needed for creating the Binary tree.
}
BinarySearchTree.java
public class BinarySearchTree extends BinaryTree {
public BinaryTreeNode search(int k) {
return search(k, root);
}
private BinaryTreeNode search(int k, BinaryTreeNode root) {
if (root.getData() == k) {
return root;
}
if (root.getData() < k) {
return search(k, root.getRight());
} else {
return search(k, root.getLeft());
}
}
// add other methods needed for creating the Binary search tree.
// also override the methods which needs to be modified for their behavior
// for binary search tree
}
I've been working on this program for a few days now and I've implemented a few of the primary methods in my BinarySearchTree class such as insert and delete. Insert seemed to be working fine, but once I try to delete I kept getting errors. So after playing around with the code I wanted to test my compareTo methods. I created two new nodes and tried to compare them and I get this error:
Exception in thread "main" java.lang.ClassCastException: TreeNode cannot be cast to java.lang.Integer
at java.lang.Integer.compareTo(Unknown Source)
at TreeNode.compareTo(TreeNode.java:16)
at BinarySearchTree.myComparision(BinarySearchTree.java:177)
at main.main(main.java:14)
Here is my class for creating the nodes:
public class TreeNode<T> implements Comparable
{
protected TreeNode<T> left, right;
protected Object element;
public TreeNode(Object obj)
{
element=obj;
left=null;
right=null;
}
public int compareTo(Object node)
{
return ((Comparable) this.element).compareTo(node);
}
}
Am I doing the compareTo method all wrong? I would like to create trees that can handle integers and strings (seperatly of course)
To be sure that the element indeed is a comparable object, and avoid all the casts, you could do something like this:
public class TreeNode<T extends Comparable<? super T>>
implements Comparable<TreeNode<T>> {
protected TreeNode<T> left, right;
protected T element;
public TreeNode(T obj) {
element = obj;
left = null;
right = null;
}
#Override
public int compareTo(TreeNode<T> node) {
return element.compareTo(node.element);
}
}
For an usage example:
TreeNode<Integer> node1 = new TreeNode<Integer>(2);
TreeNode<Integer> node2 = new TreeNode<Integer>(3);
System.out.println(node1.compareTo(node2));
The above snippet prints -1 on the console.
compareTo method is applied against TreeNode (passed as node parameter), while you compare it with this.element, which is an Object contained in the TreeNode. Simply change to:
return ((Comparable) this.element).compareTo(node.getElement());
assuming you have getElement method.
Try
public <T> int compareTo(Object node)
{
return ((Comparable) this.element).compareTo( ( TreeNode<T> ) node ).element);
}
I need to write a tree class in Java where each level has a unique object type. The way it is written below does not take advantage of generics and causes alot of duplicate code. Is there a way to write this with Generics ?
public class NodeB {
private String nodeValue;
//private List<NodeB> childNodes;
// constructors
// getters/setters
}
public class NodeA {
private String value;
private List<NodeB> childNodes;
// constructors
// getters/setters
}
public class Tree {
private String value;
private List<NodeA> childNodes;
// constructors
// tree methods
}
This is simplistic implementation, but enough to give general idea:
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class GenericNode {
public static abstract class AbstractNode<V, N> {
private V value;
private List<N> children;
public AbstractNode(V value, N... children) {
this.value = value;
this.children = children != null ? Arrays.asList(children)
: Collections.<N> emptyList();
}
public V getValue() {
return value;
}
public List<N> getChildren() {
return children;
}
public int getNumberOfChildren() {
return children.size();
}
#Override
public String toString() {
return value.toString() + "->" + children.toString();
}
}
// leaf node type, ignore type of children
public static class NodeB extends AbstractNode<String, Object> {
public NodeB(String value, Object... nodes) {
super(value, nodes);
}
}
// example of typical node in the mid of tree
public static class NodeA extends AbstractNode<String, NodeB> {
public NodeA(String value, NodeB... nodes) {
super(value, nodes);
}
}
// top level node type
public static class Tree extends AbstractNode<String, NodeA> {
public Tree(String value, NodeA... nodes) {
super(value, nodes);
}
}
#SuppressWarnings({ "rawtypes", "unchecked" })
public static <V, N extends AbstractNode> int getNodeCount(
AbstractNode<V, N> node) {
int nodeCount = node.getChildren().size();
for (N child : node.getChildren()) {
nodeCount += getNodeCount(child);
}
return nodeCount;
}
public static void main(String[] args) {
NodeB nodeB1 = new NodeB("Leaf node 1");
NodeB nodeB2 = new NodeB("Leaf node 2");
NodeA nodeA = new NodeA("Node with children", nodeB1, nodeB2);
NodeA emptyNodeA = new NodeA("Empty node");
Tree tree = new Tree("Tree", nodeA, emptyNodeA);
System.out.println(tree);
System.out.println(1 + getNodeCount(tree));
}
}
You could make N and V types implement specific interfaces so it will be possible to call some common operations on values and/or children.
EDIT: updated implementation with recursive method for node count retrieval
All you need is a Pair<A, B>. Example of trees:
Pair<A, Pair<B, C>>
Pair<Pair<A, B>, Pair<C, D>>
Pair<Pair<Pair<A, B>, Pair<C, D>>, Pair<Pair<E, F>, Pair<G, H>>
ps: don't do this. :)
This is an ideal spot for everything inheriting from "Node", but even that is unnecessary.\
What you probably want is a single generic "Node" object that contains references to your different classes (use composition before inheritance).
At that point, each of your different classes probably has something that can be done to them (otherwise why are they all in the same data structure?) Have them implement a common interface with this common functionality. The node class can delegate to this interface, or some other class can extract the class by this interface and act on it.
This would be better than trying to force something to also BE a node--do one simple thing and do it well.
--edit--
I can't really add an example that is relevant to you because you didn't post anything about your scenario.
But let's say that you have these different classes A, B * C. First of all are they related AT ALL aside from all being children of Object? Let's say they all implement interface "Iface". (If not, you can just replace Iface with "Object", but this really implies a bad design.)
Anyway, your "Node" object is just one object--
public class Node {
private List<node> children;
private Iface myObject;
... setters, getters, tree implementation, tree navigation, related garbage...
}
Now this is enough to create your tree. One thing that you might be able to do to make things smoother, have "Node implements Iface" and delegate any calls to it's object. For instance, if Iface contains an eat(Food foodtype) method, your node could implement Iface and have a method:
public void eat(Food foodtype) {
myObject.eat(foodtype);
}
This would make the "Node" class act as though it was the class it contained.
By the way--another relatively good idea at this point would be to make myObject "private final" and ensure it is not null in the constructor. That way you would always know it was set and none of your delegated members would have to do null checks.
I don't think generics are going to help you much in this case. Instead of having a different class for each level in the tree. What about one node class that has children and store a different class on each level. That should help eliminate a lot of the duplication.
I'm fairly new to Java, so this might have issues I'm not aware of, but it seems to work on a simple level at least.
Define your main Node class - this one will be the root of the tree.
public class NodeA {
private String _value;
private ArrayList<NodeA> _children;
private int _depth;
public NodeA (String value, int depth) {
_value = value;
_children = new ArrayList<NodeA>();
_depth = depth;
}
//probably want getters for _children and _value here
//this makes a new child, with its type depending on the depth of the tree it will
//be placed at. NodeB and NodeC will both inherit from NodeA
public void add(String value) {
switch (_depth) {
case 0:
_children.add(new NodeB(value, _depth+1));
break;
case 1:
_children.add(new NodeC(value, _depth+1));
break;
}
}
The add() method is going to create a new child for the node using the specified value. If you initialize the root of the tree as a NodeA with depth 0, you can add children to nodes and the tree should end up populated so that the next level contains all NodeB's, and the next all NodeC's. The code for NodeB and NodeC is super simple and could be replicated to create an arbitrary amount of Node levels (here is that code).
public class NodeB extends NodeA {
public NodeB(String value, int depth) {
super(value, depth);
}
//nothing else needed!
The code for NodeC is identical, except for the obvious replacements of B's with C's.
Hope this helps / is the kind of answer you wanted!