honey, its time for you to learn something else in java! \n yes honey... :depressed:
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.projecttrees_calebfontenot;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedList;
|
||||
import java.util.ListIterator;
|
||||
import java.util.Queue;
|
||||
@@ -12,8 +14,9 @@ import java.util.Stack;
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
* @param <T>
|
||||
*/
|
||||
public class TreeASDV<T extends Comparable> {
|
||||
public class TreeASDV<T extends Comparable> implements Cloneable {
|
||||
|
||||
private Node<T> root;
|
||||
|
||||
@@ -24,7 +27,21 @@ public class TreeASDV<T extends Comparable> {
|
||||
Node<T> rightChild;
|
||||
}
|
||||
|
||||
public boolean insert(T t) {
|
||||
@Override
|
||||
public Object clone()
|
||||
{
|
||||
System.out.println("Cloning...");
|
||||
TreeASDV<T> clone = new TreeASDV<T>();
|
||||
ArrayList<T> oldData = this.getBreadthFirstArray();
|
||||
for (int i = 0; i < oldData.size(); ++i) {
|
||||
//System.out.println(oldData.get(i));
|
||||
clone.insert(oldData.get(i));
|
||||
}
|
||||
return clone;
|
||||
}
|
||||
|
||||
public boolean insert(T t)
|
||||
{
|
||||
Node<T> newNode = new Node<>();
|
||||
newNode.data = t;
|
||||
|
||||
@@ -55,7 +72,8 @@ public class TreeASDV<T extends Comparable> {
|
||||
return true;
|
||||
}
|
||||
|
||||
private void inOrder(Node<T> p) {
|
||||
private void inOrder(Node<T> p)
|
||||
{
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
@@ -65,11 +83,8 @@ public class TreeASDV<T extends Comparable> {
|
||||
inOrder(p.rightChild);
|
||||
}
|
||||
|
||||
public void inOrder() {
|
||||
inOrder(this.root);
|
||||
}
|
||||
|
||||
public Node<T> findNode(T t) {
|
||||
public Node<T> findNode(T t)
|
||||
{
|
||||
Node<T> currentNode = root;
|
||||
while (currentNode != null) {
|
||||
if (t.compareTo(currentNode.data) == 0) {
|
||||
@@ -82,8 +97,9 @@ public class TreeASDV<T extends Comparable> {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean remove(T t) {
|
||||
|
||||
public boolean remove(T t)
|
||||
{
|
||||
// Initialize parent and current nodes for traversal
|
||||
Node<T> parent = null;
|
||||
Node<T> current = root;
|
||||
@@ -134,7 +150,8 @@ public class TreeASDV<T extends Comparable> {
|
||||
}
|
||||
|
||||
// Helper method to find in-order successor of a node
|
||||
private Node<T> getSuccessor(Node<T> node) {
|
||||
private Node<T> getSuccessor(Node<T> node)
|
||||
{
|
||||
Node<T> current = node.rightChild;
|
||||
Node<T> successorParent = node;
|
||||
Node<T> successor = node;
|
||||
@@ -156,12 +173,80 @@ public class TreeASDV<T extends Comparable> {
|
||||
return successor;
|
||||
}
|
||||
|
||||
public ListIterator<T> listIterator() {
|
||||
//ListIterator it = new ListIterator<T>();
|
||||
return null;
|
||||
// Inorder ListIterator
|
||||
public Iterator<T> listIterator()
|
||||
{
|
||||
return new InorderIterator();
|
||||
}
|
||||
|
||||
public void breadthFirstTraversal() {
|
||||
private class InorderIterator implements Iterator<T> {
|
||||
|
||||
private Stack<Node<T>> stack;
|
||||
|
||||
public InorderIterator()
|
||||
{
|
||||
stack = new Stack<>();
|
||||
pushLeft(root);
|
||||
}
|
||||
|
||||
private void pushLeft(Node<T> node)
|
||||
{
|
||||
while (node != null) {
|
||||
stack.push(node);
|
||||
node = node.leftChild;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext()
|
||||
{
|
||||
return !stack.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next()
|
||||
{
|
||||
if (!hasNext()) {
|
||||
throw new java.util.NoSuchElementException();
|
||||
}
|
||||
Node<T> current = stack.pop();
|
||||
pushLeft(current.rightChild);
|
||||
return current.data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove()
|
||||
{
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<T> getBreadthFirstArray()
|
||||
{
|
||||
ArrayList<T> returnArray = new ArrayList<T>();
|
||||
if (root == null) {
|
||||
return returnArray;
|
||||
}
|
||||
Queue<Node<T>> queue = new LinkedList<>();
|
||||
|
||||
queue.offer(root);
|
||||
|
||||
while (!queue.isEmpty()) {
|
||||
Node<T> current = queue.poll();
|
||||
returnArray.add(current.data);
|
||||
|
||||
if (current.leftChild != null) {
|
||||
queue.offer(current.leftChild);
|
||||
}
|
||||
if (current.rightChild != null) {
|
||||
queue.offer(current.rightChild);
|
||||
}
|
||||
}
|
||||
return returnArray;
|
||||
}
|
||||
|
||||
public void breadthFirstTraversal()
|
||||
{
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
@@ -182,12 +267,18 @@ public class TreeASDV<T extends Comparable> {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return this.getBreadthFirstArray().equals(((TreeASDV) obj).getBreadthFirstArray());
|
||||
}
|
||||
|
||||
public int height() {
|
||||
public int height()
|
||||
{
|
||||
return calculateHeight(root);
|
||||
}
|
||||
|
||||
private int calculateHeight(Node<T> node) {
|
||||
private int calculateHeight(Node<T> node)
|
||||
{
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
@@ -197,21 +288,24 @@ public class TreeASDV<T extends Comparable> {
|
||||
|
||||
return 1 + Math.max(leftHeight, rightHeight);
|
||||
}
|
||||
|
||||
public boolean isFullBST() {
|
||||
|
||||
public boolean isFullBST()
|
||||
{
|
||||
int height = height();
|
||||
int nodeCount = countNodes(root);
|
||||
return nodeCount == (1 << height) - 1; // Formula for full binary tree
|
||||
}
|
||||
|
||||
private int countNodes(Node<T> node) {
|
||||
private int countNodes(Node<T> node)
|
||||
{
|
||||
if (node == null) {
|
||||
return 0;
|
||||
}
|
||||
return 1 + countNodes(node.leftChild) + countNodes(node.rightChild);
|
||||
}
|
||||
|
||||
public void inorder() {
|
||||
|
||||
public void inOrder()
|
||||
{
|
||||
if (root == null) {
|
||||
return;
|
||||
}
|
||||
@@ -230,7 +324,8 @@ public class TreeASDV<T extends Comparable> {
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws CloneNotSupportedException
|
||||
{
|
||||
TreeASDV<Integer> tree = new TreeASDV<>();
|
||||
// Insert some elements into the tree
|
||||
tree.insert(5);
|
||||
@@ -254,7 +349,16 @@ public class TreeASDV<T extends Comparable> {
|
||||
|
||||
// Test inorder traversal without recursion
|
||||
System.out.println("Inorder Traversal without Recursion:");
|
||||
tree.inorder();
|
||||
tree.inOrder();
|
||||
System.out.println();
|
||||
System.out.println("array list from breadth traversal");
|
||||
System.out.println(tree.getBreadthFirstArray());
|
||||
System.out.println("Cloned Tree:");
|
||||
TreeASDV<Integer> clonedTree = (TreeASDV<Integer>) tree.clone();
|
||||
clonedTree.breadthFirstTraversal();
|
||||
System.out.println();
|
||||
System.out.println("Does the original tree and the clone tree equal? " + (tree.equals(clonedTree) ? "yes" : "no") );
|
||||
tree.insert(3000000);
|
||||
System.out.println("Does the original tree and the clone tree equal? " + (tree.equals(clonedTree) ? "yes" : "no") );
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user