- #1
DODGEVIPER13
- 672
- 0
Mod note: I added [ code ] and [ /code ] tags.
I am having touble with errors in this code and honestly I have nothing on why it does not work so if anyone can help it will be much appreciated! The errors are in the BinaryTree class I will bold the mistakes in the program.
first class Tree:
second class Abstracttree:
Mod note: It looks like you pasted the Abstracttree class in twice.
final class BinaryTree:
Homework Statement
I am having touble with errors in this code and honestly I have nothing on why it does not work so if anyone can help it will be much appreciated! The errors are in the BinaryTree class I will bold the mistakes in the program.
Homework Equations
The Attempt at a Solution
first class Tree:
Code:
public interface Tree<E extends Comparable<E>> {
public boolean search(E e);
public boolean insert(E e);
public boolean delete(E e);
public void inOrder();
public void postorder();
public void preorder();
public int getSize();
public boolean isEmpty();
public java.util.Iterator iterator();
}
second class Abstracttree:
Mod note: It looks like you pasted the Abstracttree class in twice.
Code:
public abstract class Abstracttree<E extends Comparable<E>>implements Tree<E> {
public void inorder() {
}
public void postorder() {
}
public void preorder() {
}
public boolean isEmpty() {
return getSize() == 0;
}
public java.util.Iterator iterator() {
return null;
}
}public abstract class Abstracttree<E extends Comparable<E>>implements Tree<E> {
public void inorder() {
}
public void postorder() {
}
public void preorder() {
}
public boolean isEmpty() {
return getSize() == 0;
}
public java.util.Iterator iterator() {
return null;
}
}
Code:
public class BinaryTree<E extends Comparable<E>> extends Abstracttree<E> {
protected TreeNode<E> root;
protected int size = 0;
public BinaryTree(){
}
public BinaryTree(E[] objects){
for(int i = 0;i < objects.length; i++)
insert(objects[i]);
}
public boolean search(E e){
while ([B] current [/B]!= null){
if(e.compareTo([B]current[/B].element)< 0){
[B]current[/B] = [B]current[/B].left;
}
else if (e.compareTo([B]current[/B].element) > 0){
[B] current [/B]= [B]current[/B].right;
}
else
return true;
}
return false;
}
public boolean insert(E e){
if(root == null)
root = createNewNode(e);
else{
TreeNode<E> parent = null;
TreeNode<E> current = root;
while(current != null)
if (e.compareTo(current.element) < 0){
parent = current;
current = current.left;
}
else if (e.compareTo(current.element) > 0){
parent = current;
current = current.right;
}
else
return false;
if(e.compareTo(parent.element)< 0)
parent.left = createNewNode(e);
else
parent.right = createNewNode(e);
}
size++;
return true;
}
protected TreeNode<E> createNewNode(E e){
return new TreeNode<E>(e);
}
public void inorder(){
inorder(root);
}
protected void inorder(TreeNode<E> root){
[B] if(root = null) return;[/B] inorder(root.left);
System.out.print(root.element + " ");
inorder(root.right);
}
public void postorder(){
postorder(root);
}
protected void postorder(TreeNode<E> root){
[B] if(root = null) return;[/B]
postorder(root.left);
postorder(root.right);
System.out.print(root.element + " ");
}
public void preorder() {
preorder(root);
}
protected void preorder(TreeNode<E> root){
if(root == null) return;
System.out.print(root.element + " ");
preorder(root.left);
preorder(root.right);
}
public static class TreeNode<E extends Comparable<E>>{
E element;
TreeNode<E> left;
TreeNode<E> right;
public TreeNode(E e){
element = e;
}
}
public int getSize(){
return size;
}
public TreeNode getRoot(){
return root;
}
public java.util.ArrayList<TreeNode<E>> path(E e) {
java.util.ArrayList<TreeNode<E>> list = new java.util.ArrayList<TreeNode<E>>();
TreeNode<E> current = root;
while(current != null){
list.add(current);
if(e.compareTo(current.element) < 0){
current = current.left;
}
else if (e.compareTo(current.element) > 0){
current = current.right;
}
else break;
}
return list;
}
public boolean delete(E e){
TreeNode<E> parent = null;
TreeNode<E> current = root;
while(current != null){
if(e.compareTo(current.element) < 0){
parent = current;
current = current.left;
}
else if (e.compareTo(current.element) > 0){
parent = current;
current = current.right;
}
else
break;
}
if(current == null)
return false;
if(current.left == null){
if(parent == null){
root = current.right;
}
else{
if(e.compareTo(parent.element) < 0)
parent.left = current.right;
else
parent.right = current.right;
}
}
else{
TreeNode<E> parentOfRightMost = current;
TreeNode<E> rightMost = current.left;
while(rightMost.right != null){
parentOfRightMost = rightMost;
rightMost = rightMost.right;
}
current.element = rightMost.element;
if(parentOfRightMost.right == rightMost)
parentOfRightMost.right = rightMost.left;
else
parentOfRightMost.left = rightMost.left;
}
size--;
return true;
}
public java.util.Iterator iterator(){
return inorderIterator();
}
public java.util.Iterator inorderIterator(){
return new InorderIterator();
}
class InorderIterator implements java.util.Iterator {
private java.util.ArrayList<E> list = new java.util.ArrayList<E>();
private int current = 0;
public InorderIterator(){
[B] inorder();[/B]
}
public void inorder(TreeNode<E> root){
if(root == null) return;
inorder(root.left);
list.add(root.element);
inorder(root.right);
}
public boolean hasNext(){
if(current < list.size())
return true;
return false;
}
public Object next(){
return list.get(current++);
}
public void remove(){
delete(list.get(current));
list.clear();
[B] inorder();[/B]
}
}
public void clear(){
root = null;
size = 0;
}
}
Last edited by a moderator: