Implement an array-based binary tree, in Java. import net.datastructures.Abstrac
ID: 3590180 • Letter: I
Question
Implement an array-based binary tree, in Java.
import net.datastructures.AbstractBinaryTree;
import net.datastructures.Position;
public class ArrayBinaryTree<E> extends AbstractBinaryTree<E> {
@Override
public Position<E> left(Position<E> p) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> right(Position<E> p) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> root() {
// TODO Auto-generated method stub
return null;
}
@Override
public Position<E> parent(Position<E> p) throws IllegalArgumentException {
// TODO Auto-generated method stub
return null;
}
}
Explanation / Answer
Hey, as i see in your question, it is not clear, i think some thing is missing in your question,
if you post any question just clearly explain everything, what you want to be in your answer..
as you have given the code in your question, i am assuming that you have knowledge of java language..
here i am posting my code for implementation of an array-based binary tree in java, and
if you are satisfied with my answer, then like it,
and if you are not, then post your question again with full explaination i will answer it again..
Here is my code in java:-
package jss2;
import java.util.Iterator;
import jss2.exceptions.*;
public class ArrayBinaryTree<T> implements BinaryTreeADT<T>
{
protected int count;
protected T[] tree;
private final int capacity = 50;
public ArrayBinaryTree()
{
count = 0;
tree = (T[]) new Object[capacity];
} // constructor BinaryTree
public ArrayBinaryTree (T element)
{
count = 1;
tree = (T[]) new Object[capacity];
tree[0] = element;
} // constructor BinaryTree
protected void expandCapacity()
{
T[] temp = (T[]) new Object[tree.length * 2];
for (int ct=0; ct < tree.length; ct++)
temp[ct] = tree[ct];
tree = temp;
}
public void removeLeftSubtree()
{
} // method removeLeftSubtree
public void removeRightSubtree()
{
} // method removeRightSubtree
public void removeAllElements()
{
count = 0;
for (int ct=0; ct<tree.length; ct++)
tree[ct] = null;
} // method removeAllElements
public boolean isEmpty()
{
return (count == 0);
} // method isEmpty
public int size()
{
return count;
} // method size
public boolean contains (T targetElement)
{
boolean found = false;
for (int ct=0; ct<count && !found; ct++)
if (targetElement.equals(tree[ct]))
found = true;
return found;
} // method contains
public T find (T targetElement) throws ElementNotFoundException
{
T temp=null;
boolean found = false;
for (int ct=0; ct<count && !found; ct++)
if (targetElement.equals(tree[ct]))
{
found = true;
temp = tree[ct];
}
if (!found)
throw new ElementNotFoundException("binary tree");
return temp;
} // method find
public String toString()
{
ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
inorder (0, templist);
return templist.toString();
} // method toString
public Iterator<T> iteratorInOrder()
{
ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
inorder (0, templist);
return templist.iterator();
} // method inorder
protected void inorder (int node, ArrayUnorderedList<T> templist)
{
if (node < tree.length)
if (tree[node] != null)
{
inorder ((node+1)*2-1, templist);
templist.addToRear(tree[node]);
inorder ((node+1)*(2+1)-1, templist);
}//if
} // method inorder
public Iterator<T> iteratorPreOrder()
{
ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
preorder (0, templist);
return templist.iterator();
} // method preorder
protected void preorder (int node, ArrayUnorderedList<T> templist)
{
if (node < tree.length)
if (tree[node] != null)
{
templist.addToRear(tree[node]);
inorder ((node+1)*2-1, templist);
inorder ((node+1)*(2+1)-1, templist);
}//if
} // method preorder
public Iterator<T> iteratorPostOrder()
{
ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
postorder (0, templist);
return templist.iterator();
} // method postorder
protected void postorder (int node, ArrayUnorderedList<T> templist)
{
if (node < tree.length)
if (tree[node] != null)
{
inorder ((node+1)*2-1, templist);
inorder ((node+1)*(2+1)-1, templist);
templist.addToRear(tree[node]);
}//if
} // method postorder
public Iterator<T> iteratorLevelOrder()
{
ArrayUnorderedList<T> templist = new ArrayUnorderedList<T>();
for (int ct=0; ct<count; ct++)
templist.addToRear(tree[ct]);
return templist.iterator();
} // method levelorder
} // class BinaryTree
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.