Question
Please help been having alot of trouble with this project especially since our professor really did not explain how th syntax for trees work! 1)
Please help been having alot of trouble with this project especially since our professor really did not explain how th syntax for trees work!
1) Add a method to class MyBinaryTree that searches for a given element, returning true if the element is found, false if not. Add code to class MyBinaryTreeExample to demonstrate this method.
2) Add a method to class MyBinaryTree that removes all deleted nodes from the binary tree. Tip: Use preorder traversal to create a new tree. Add code to class MyBinaryTreeExample that demonstrates this method.
3) Add a method to class MyBinaryTree that displays the elements of the nodes that are leaves. Add code to class MyBinaryTreeExample that demonstrates this method.
package mybinarytreeexample;
public class MyBinaryTree
private Node
public class Node
public E e = null; public Node
public boolean insert(E e) { // if empty tree, insert a new node as the root node // and assign the elementy to it if (root == null) { root = new Node(); root.e = e; return true; }
// otherwise, binary search until a null child pointer // is found Node
while (child != null) { if (e.compareTo(child.e) < 0) { parent = child; child = child.left; } else if (e.compareTo(child.e) > 0) { parent = child; child = child.right; } else { return false; } }
// if e < parent.e create a new node, link it to // the binary tree and assign the element to it if (e.compareTo(parent.e) < 0) { parent.left = new Node(); parent.left.e = e; } else { parent.right = new Node(); parent.right.e = e; } return true; }
public void inorder() { System.out.print("inorder: "); inorder(root); System.out.println(); }
private void inorder(Node
public void preorder() { System.out.print("preorder: "); preorder(root); System.out.println(); }
private void preorder(Node
public void postorder() { System.out.print("postorder: "); postorder(root); System.out.println(); }
private void postorder(Node
//first finding element using binary search // binary search until found or not in list boolean found = false; Node
while (child != null) { if (e.compareTo(child.e) < 0) { parent = child; child = child.left; } else if (e.compareTo(child.e) > 0) { parent = child; child = child.right; } else { if (child.v == true)//means node is not deleted.... { found = true; } break; } }
if (found)//if node found, then we have to delete it.. { //marking v to false...means deleting it.. child.v = false; return true;//returning true for successfully deleting node } else { return false;//because no such node found...
}
/* // binary search until found or not in list boolean found = false; Node
// an iterator allows elements to be modified, but can mess with // the order if element not written with immutable key; it is better // to use delete to remove and delete/insert to remove or replace a // node public java.util.Iterator
private class PreorderIterator implements java.util.Iterator
private java.util.LinkedList
// create a LinkedList object that uses a linked list of nodes that // contain references to the elements of the nodes of the binary tree // in preorder public PreorderIterator() { buildListInPreorder(root); pit = ll.iterator(); }
private void buildListInPreorder(Node
// check to see if their is another node in the LinkedList @Override public boolean hasNext() { return pit.hasNext(); }
// reference the next node in the LinkedList and return a // reference to the element in the node of the binary tree @Override public E next() { return pit.next(); }
@Override public void remove() { throw new UnsupportedOperationException("NO!"); } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started