diff --git a/Semester 4/Assignments/MP1_ManyToMany_CalebFontenot/nb-configuration.xml b/Semester 4/Assignments/MP1_ManyToMany_CalebFontenot/nb-configuration.xml new file mode 100644 index 0000000..6ecd386 --- /dev/null +++ b/Semester 4/Assignments/MP1_ManyToMany_CalebFontenot/nb-configuration.xml @@ -0,0 +1,18 @@ + + + + + + Graal_JDK_20 + + diff --git a/Semester 4/Assignments/MP1_ManyToMany_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp1_manytomany_calebfontenot/ManyToManyFactory.java b/Semester 4/Assignments/MP1_ManyToMany_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp1_manytomany_calebfontenot/ManyToManyFactory.java index 14794bf..cc64074 100644 --- a/Semester 4/Assignments/MP1_ManyToMany_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp1_manytomany_calebfontenot/ManyToManyFactory.java +++ b/Semester 4/Assignments/MP1_ManyToMany_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/mp1_manytomany_calebfontenot/ManyToManyFactory.java @@ -27,7 +27,7 @@ public class ManyToManyFactory { createManyToMany() { return new ManyToMany() { - private HashSet parents = new HashSet(); + //private HashSet parents = new HashSet(); private Map left = new HashMap(); private Map right = new HashMap(); @@ -39,7 +39,9 @@ public class ManyToManyFactory { } /** - * Creates a Many to Many relationship between the parentLeft and the childrenRight. Example for ( Many1 a, Many2 1, 2, 3) it creates --> (a 1) ( a 2 ) ( a 3 ) and ( 1, a ), ( 2, a ), ( 3, a). No duplicate values of Many2 are allowed. + * Creates a Many to Many relationship between the parentLeft and the childrenRight. + * Example for ( Many1 a, Many2 1, 2, 3) it creates --> (a 1) ( a 2 ) ( a 3 ) and ( 1, a ), ( 2, a ), ( 3, a). + * No duplicate values of Many2 are allowed. * * @param parentLeft - exactly one Many1 object. * @param childrenRight - one or more Many2 objects. @@ -53,17 +55,13 @@ public class ManyToManyFactory { public List add(Many1 parentLeft, Many2... childrenRight) { List returnList = new ArrayList(); - // Check to see if values already exist in this many to many object - if (this.left != parentLeft && this.left != null) { - returnList.add((Many2) this.left); - this.left = (Map) parentLeft; - } - if (this.left != childrenRight && this.right != null) { - returnList.add((Many2) this.right); - this.left = (Map) childrenRight; - left.put(parentLeft, Arrays.asList(childrenRight)); - + + // Check for exceptions + if (!childrenRight.equals(parentLeft.getClass())) { + throw new ClassCastException(); } + left.put(parentLeft, new ArrayList(Arrays.asList(childrenRight))); + return returnList; } @Override diff --git a/Semester 4/Assignments/ProjectTrees_CalebFontenot/Printed HTMLs/TreeASDV.html b/Semester 4/Assignments/ProjectTrees_CalebFontenot/Printed HTMLs/TreeASDV.html index f07dcca..20a38e5 100644 --- a/Semester 4/Assignments/ProjectTrees_CalebFontenot/Printed HTMLs/TreeASDV.html +++ b/Semester 4/Assignments/ProjectTrees_CalebFontenot/Printed HTMLs/TreeASDV.html @@ -8,17 +8,18 @@ body {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold} pre {color: #a9b7c6; background-color: #2b2b2b; font-family: monospace; font-weight: bold} table {color: #888888; background-color: #313335; font-family: monospace; font-weight: bold} -.number {color: #6897bb} .string {color: #6a8759} -.ST1 {color: #9876aa} -.ST2 {color: #ffc66d} +.number {color: #6897bb} +.ST2 {color: #9876aa} +.ST3 {color: #ffc66d} +.ST1 {color: #8a653b} .comment {color: #808080} .whitespace {color: #505050} -.ST3 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic} -.ST5 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic} +.ST4 {color: #9876aa; font-family: monospace; font-weight: bold; font-style: italic} +.ST6 {color: #ffc66d; font-family: monospace; font-weight: bold; font-style: italic} .ST0 {color: #287bde} .literal {color: #cc7832} -.ST4 {font-family: monospace; font-weight: bold; font-style: italic} +.ST5 {font-family: monospace; font-weight: bold; font-style: italic} --> @@ -31,138 +32,163 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w */ 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.ListIterator; import java.util.Queue; 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; + private Node<T> root; class Node<T> { - T data; - Node<T> leftChild; - Node<T> rightChild; + T data; + Node<T> leftChild; + Node<T> rightChild; } - public boolean insert(T t) { - Node<T> newNode = new Node<>(); - newNode.data = 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; + } - if (root == null) { - root = newNode; + public boolean insert(T t) + { + Node<T> newNode = new Node<>(); + newNode.data = t; + + if (root == null) { + root = newNode; return true; } - Node<T> current = root; + Node<T> current = root; Node<T> parent = null; while (current != null) { parent = current; - if (t.compareTo(current.data) >= 0) { - current = current.rightChild; + if (t.compareTo(current.data) >= 0) { + current = current.rightChild; } else { - current = current.leftChild; + current = current.leftChild; } } // At this point, 'parent' is the node where the new node should be inserted as a child - if (t.compareTo(parent.data) >= 0) { - parent.rightChild = newNode; + if (t.compareTo(parent.data) >= 0) { + parent.rightChild = newNode; } else { - parent.leftChild = newNode; + parent.leftChild = newNode; } return true; } - private void inOrder(Node<T> p) { + private void inOrder(Node<T> p) + { if (p == null) { return; } - inOrder(p.leftChild); - System.out.print(p.data + " "); - inOrder(p.rightChild); + inOrder(p.leftChild); + System.out.print(p.data + " "); + inOrder(p.rightChild); } - public void inOrder() { - inOrder(this.root); - } - - public Node<T> findNode(T t) { - Node<T> currentNode = root; + public Node<T> findNode(T t) + { + Node<T> currentNode = root; while (currentNode != null) { - if (t.compareTo(currentNode.data) == 0) { + if (t.compareTo(currentNode.data) == 0) { return currentNode; - } else if (t.compareTo(currentNode.data) > 0) { - currentNode = currentNode.rightChild; + } else if (t.compareTo(currentNode.data) > 0) { + currentNode = currentNode.rightChild; } else { - currentNode = currentNode.leftChild; + currentNode = currentNode.leftChild; } } return null; } + +public boolean remove(T t) { + // Initialize parent and current nodes for traversal + Node<T> parent = null; + Node<T> current = root; - public boolean remove(T t) { - // Initialize parent and current nodes for traversal - Node<T> parent = null; - Node<T> current = root; - - // Search for the node to be removed - while (current != null && !current.data.equals(t)) { - parent = current; - if (t.compareTo(current.data) > 0) { - current = current.rightChild; - } else { - current = current.leftChild; - } + // Search for the node to be removed + while (current != null && !current.data.equals(t)) { + parent = current; + if (t.compareTo(current.data) > 0) { + current = current.rightChild; + } else { + current = current.leftChild; } - - // If node not found, return false - if (current == null) { - return false; - } - - // Case 1: Node with no children - if (current.leftChild == null && current.rightChild == null) { - if (current == root) { - root = null; // Removing root node - } else if (parent.leftChild == current) { - parent.leftChild = null; // Removing a left child - } else { - parent.rightChild = null; // Removing a right child - } - } // Case 2: Node with one child - else if (current.leftChild == null || current.rightChild == null) { - Node<T> child = (current.leftChild != null) ? current.leftChild : current.rightChild; - if (current == root) { - root = child; // Replace root with its child - } else if (parent.leftChild == current) { - parent.leftChild = child; // Replace parent's left child with the node's child - } else { - parent.rightChild = child; // Replace parent's right child with the node's child - } - } // Case 3: Node with two children - else { - Node<T> successor = getSuccessor(current); - current.data = successor.data; // Replace data with successor's data - // Remove successor node (successor will have at most one right child) - remove(successor.data); - } - - return true; } + // If node not found, return false + if (current == null) { + return false; + } + + // Case 1: Node with no children + if (current.leftChild == null && current.rightChild == null) { + if (current == root) { + root = null; // Removing root node + } else if (parent.leftChild == current) { + parent.leftChild = null; // Removing a left child + } else { + parent.rightChild = null; // Removing a right child + } + } // Case 2: Node with one child + else if (current.leftChild == null || current.rightChild == null) { + Node<T> child = (current.leftChild != null) ? current.leftChild : current.rightChild; + if (current == root) { + root = child; // Replace root with its child + } else if (parent.leftChild == current) { + parent.leftChild = child; // Replace parent's left child with the node's child + } else { + parent.rightChild = child; // Replace parent's right child with the node's child + } + } // Case 3: Node with two children + else { + Node<T> successorParent = current; + Node<T> successor = current.rightChild; + while (successor.leftChild != null) { + successorParent = successor; + successor = successor.leftChild; + } + current.data = successor.data; // Replace data with successor's data + if (successorParent == current) { + successorParent.rightChild = successor.rightChild; + } else { + successorParent.leftChild = successor.rightChild; + } + } + + return true; +} + + // Helper method to find in-order successor of a node - private Node<T> getSuccessor(Node<T> node) { - Node<T> current = node.rightChild; + private Node<T> getSuccessor(Node<T> node) + { + Node<T> current = node.rightChild; Node<T> successorParent = node; Node<T> successor = node; @@ -170,94 +196,172 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w while (current != null) { successorParent = successor; successor = current; - current = current.leftChild; + current = current.leftChild; } // If the successor is not the right child of the node to be removed, // adjust the successor's parent's leftChild reference - if (successor != node.rightChild) { - successorParent.leftChild = successor.rightChild; - successor.rightChild = node.rightChild; + if (successor != node.rightChild) { + successorParent.leftChild = successor.rightChild; + successor.rightChild = node.rightChild; } 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() { - if (root == null) { + 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; } Queue<Node<T>> queue = new LinkedList<>(); - queue.offer(root); + queue.offer(root); while (!queue.isEmpty()) { Node<T> current = queue.poll(); - System.out.print(current.data + " "); + System.out.print(current.data + " "); - if (current.leftChild != null) { - queue.offer(current.leftChild); + if (current.leftChild != null) { + queue.offer(current.leftChild); } - if (current.rightChild != null) { - queue.offer(current.rightChild); + if (current.rightChild != null) { + queue.offer(current.rightChild); } } } + @Override + public boolean equals(Object obj) { + return this.getBreadthFirstArray().equals(((TreeASDV) obj).getBreadthFirstArray()); + } - public int height() { - return calculateHeight(root); + public int height() + { + return calculateHeight(root); } - private int calculateHeight(Node<T> node) { + private int calculateHeight(Node<T> node) + { if (node == null) { return 0; } - int leftHeight = calculateHeight(node.leftChild); - int rightHeight = calculateHeight(node.rightChild); + int leftHeight = calculateHeight(node.leftChild); + int rightHeight = calculateHeight(node.rightChild); - return 1 + Math.max(leftHeight, rightHeight); + return 1 + Math.max(leftHeight, rightHeight); } - - public boolean isFullBST() { + + public boolean isFullBST() + { int height = height(); - int nodeCount = countNodes(root); + 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); + return 1 + countNodes(node.leftChild) + countNodes(node.rightChild); } - - public void inorder() { - if (root == null) { + + public void inOrder() + { + if (root == null) { return; } Stack<Node<T>> stack = new Stack<>(); - Node<T> current = root; + Node<T> current = root; while (current != null || !stack.isEmpty()) { while (current != null) { stack.push(current); - current = current.leftChild; + current = current.leftChild; } current = stack.pop(); - System.out.print(current.data + " "); - current = current.rightChild; + System.out.print(current.data + " "); + current = current.rightChild; } } - 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); @@ -269,20 +373,31 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w tree.insert(8); // Test breadth-first traversal - System.out.println("Breadth-First Traversal:"); + System.out.println("Breadth-First Traversal:"); tree.breadthFirstTraversal(); - System.out.println(); + System.out.println(); // Test height calculation - System.out.println("Height of the tree: " + tree.height()); + System.out.println("Height of the tree: " + tree.height()); // Test if the tree is a full binary tree - System.out.println("Is the tree a full binary tree? " + tree.isFullBST()); + System.out.println("Is the tree a full binary tree? " + tree.isFullBST()); // Test inorder traversal without recursion - System.out.println("Inorder Traversal without Recursion:"); - tree.inorder(); - System.out.println(); + System.out.println("Inorder Traversal without Recursion:"); + 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") ); + tree.remove(5); + tree.breadthFirstTraversal(); } } diff --git a/Semester 4/Assignments/ProjectTrees_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/projecttrees_calebfontenot/TreeASDV.java b/Semester 4/Assignments/ProjectTrees_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/projecttrees_calebfontenot/TreeASDV.java index f84692b..f5eb493 100644 --- a/Semester 4/Assignments/ProjectTrees_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/projecttrees_calebfontenot/TreeASDV.java +++ b/Semester 4/Assignments/ProjectTrees_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/projecttrees_calebfontenot/TreeASDV.java @@ -98,57 +98,65 @@ public class TreeASDV implements Cloneable { return null; } - public boolean remove(T t) - { - // Initialize parent and current nodes for traversal - Node parent = null; - Node current = root; +public boolean remove(T t) { + // Initialize parent and current nodes for traversal + Node parent = null; + Node current = root; - // Search for the node to be removed - while (current != null && !current.data.equals(t)) { - parent = current; - if (t.compareTo(current.data) > 0) { - current = current.rightChild; - } else { - current = current.leftChild; - } + // Search for the node to be removed + while (current != null && !current.data.equals(t)) { + parent = current; + if (t.compareTo(current.data) > 0) { + current = current.rightChild; + } else { + current = current.leftChild; } - - // If node not found, return false - if (current == null) { - return false; - } - - // Case 1: Node with no children - if (current.leftChild == null && current.rightChild == null) { - if (current == root) { - root = null; // Removing root node - } else if (parent.leftChild == current) { - parent.leftChild = null; // Removing a left child - } else { - parent.rightChild = null; // Removing a right child - } - } // Case 2: Node with one child - else if (current.leftChild == null || current.rightChild == null) { - Node child = (current.leftChild != null) ? current.leftChild : current.rightChild; - if (current == root) { - root = child; // Replace root with its child - } else if (parent.leftChild == current) { - parent.leftChild = child; // Replace parent's left child with the node's child - } else { - parent.rightChild = child; // Replace parent's right child with the node's child - } - } // Case 3: Node with two children - else { - Node successor = getSuccessor(current); - current.data = successor.data; // Replace data with successor's data - // Remove successor node (successor will have at most one right child) - remove(successor.data); - } - - return true; } + // If node not found, return false + if (current == null) { + return false; + } + + // Case 1: Node with no children + if (current.leftChild == null && current.rightChild == null) { + if (current == root) { + root = null; // Removing root node + } else if (parent.leftChild == current) { + parent.leftChild = null; // Removing a left child + } else { + parent.rightChild = null; // Removing a right child + } + } // Case 2: Node with one child + else if (current.leftChild == null || current.rightChild == null) { + Node child = (current.leftChild != null) ? current.leftChild : current.rightChild; + if (current == root) { + root = child; // Replace root with its child + } else if (parent.leftChild == current) { + parent.leftChild = child; // Replace parent's left child with the node's child + } else { + parent.rightChild = child; // Replace parent's right child with the node's child + } + } // Case 3: Node with two children + else { + Node successorParent = current; + Node successor = current.rightChild; + while (successor.leftChild != null) { + successorParent = successor; + successor = successor.leftChild; + } + current.data = successor.data; // Replace data with successor's data + if (successorParent == current) { + successorParent.rightChild = successor.rightChild; + } else { + successorParent.leftChild = successor.rightChild; + } + } + + return true; +} + + // Helper method to find in-order successor of a node private Node getSuccessor(Node node) { @@ -360,5 +368,7 @@ public class TreeASDV implements Cloneable { 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") ); + tree.remove(5); + tree.breadthFirstTraversal(); } } diff --git a/Semester 4/ZIPs/ProjectTrees_CalebFontenot.zip b/Semester 4/ZIPs/ProjectTrees_CalebFontenot.zip new file mode 100644 index 0000000..e4f7249 Binary files /dev/null and b/Semester 4/ZIPs/ProjectTrees_CalebFontenot.zip differ