diff --git a/Semester 4/Assignments/ProjectTrees_CalebFontenot/Printed HTMLs/TreeASDV.html b/Semester 4/Assignments/ProjectTrees_CalebFontenot/Printed HTMLs/TreeASDV.html new file mode 100644 index 0000000..f07dcca --- /dev/null +++ b/Semester 4/Assignments/ProjectTrees_CalebFontenot/Printed HTMLs/TreeASDV.html @@ -0,0 +1,290 @@ + + + +TreeASDV.java + + + + +
/home/caleb/ASDV-Java/Semester 4/Assignments/ProjectTrees_CalebFontenot/src/main/java/edu/slcc/asdv/caleb/projecttrees_calebfontenot/TreeASDV.java
+
+/*
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
+ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
+ */
+package edu.slcc.asdv.caleb.projecttrees_calebfontenot;
+
+import java.util.LinkedList;
+import java.util.ListIterator;
+import java.util.Queue;
+import java.util.Stack;
+
+/**
+ *
+ * @author caleb
+ */
+public class TreeASDV<T extends Comparable> {
+
+    private Node<T> root;
+
+    class Node<T> {
+
+        T data;
+        Node<T> leftChild;
+        Node<T> rightChild;
+    }
+
+    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> parent = null;
+
+        while (current != null) {
+            parent = current;
+            if (t.compareTo(current.data) >= 0) {
+                current = current.rightChild;
+            } else {
+                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;
+        } else {
+            parent.leftChild = newNode;
+        }
+
+        return true;
+    }
+
+    private void inOrder(Node<T> p) {
+        if (p == null) {
+            return;
+        }
+
+        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;
+        while (currentNode != null) {
+            if (t.compareTo(currentNode.data) == 0) {
+                return currentNode;
+            } else if (t.compareTo(currentNode.data) > 0) {
+                currentNode = currentNode.rightChild;
+            } else {
+                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;
+
+        // 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;
+    }
+
+// Helper method to find in-order successor of a node
+    private Node<T> getSuccessor(Node<T> node) {
+        Node<T> current = node.rightChild;
+        Node<T> successorParent = node;
+        Node<T> successor = node;
+
+        // Find the leftmost node in the right subtree (in-order successor)
+        while (current != null) {
+            successorParent = successor;
+            successor = current;
+            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;
+        }
+
+        return successor;
+    }
+
+    public ListIterator<T> listIterator() {
+        //ListIterator it = new ListIterator<T>();
+        return null;
+    }
+
+       public void breadthFirstTraversal() {
+        if (root == null) {
+            return;
+        }
+
+        Queue<Node<T>> queue = new LinkedList<>();
+        queue.offer(root);
+
+        while (!queue.isEmpty()) {
+            Node<T> current = queue.poll();
+            System.out.print(current.data + " ");
+
+            if (current.leftChild != null) {
+                queue.offer(current.leftChild);
+            }
+            if (current.rightChild != null) {
+                queue.offer(current.rightChild);
+            }
+        }
+    }
+
+    
+    public int height() {
+        return calculateHeight(root);
+    }
+
+    private int calculateHeight(Node<T> node) {
+        if (node == null) {
+            return 0;
+        }
+
+        int leftHeight = calculateHeight(node.leftChild);
+        int rightHeight = calculateHeight(node.rightChild);
+
+        return 1 + Math.max(leftHeight, rightHeight);
+    }
+    
+    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) {
+        if (node == null) {
+            return 0;
+        }
+        return 1 + countNodes(node.leftChild) + countNodes(node.rightChild);
+    }
+    
+    public void inorder() {
+        if (root == null) {
+            return;
+        }
+
+        Stack<Node<T>> stack = new Stack<>();
+        Node<T> current = root;
+
+        while (current != null || !stack.isEmpty()) {
+            while (current != null) {
+                stack.push(current);
+                current = current.leftChild;
+            }
+            current = stack.pop();
+            System.out.print(current.data + " ");
+            current = current.rightChild;
+        }
+    }
+
+    public static void main(String[] args) {
+        TreeASDV<Integer> tree = new TreeASDV<>();
+        // Insert some elements into the tree
+        tree.insert(5);
+        tree.insert(3);
+        tree.insert(7);
+        tree.insert(2);
+        tree.insert(4);
+        tree.insert(6);
+        tree.insert(8);
+
+        // Test breadth-first traversal
+        System.out.println("Breadth-First Traversal:");
+        tree.breadthFirstTraversal();
+        System.out.println();
+
+        // Test height calculation
+        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());
+
+        // Test inorder traversal without recursion
+        System.out.println("Inorder Traversal without Recursion:");
+        tree.inorder();
+        System.out.println();
+    }
+}
+
+
+ diff --git a/Semester 4/Assignments/ProjectTrees_CalebFontenot/nb-configuration.xml b/Semester 4/Assignments/ProjectTrees_CalebFontenot/nb-configuration.xml new file mode 100644 index 0000000..6ecd386 --- /dev/null +++ b/Semester 4/Assignments/ProjectTrees_CalebFontenot/nb-configuration.xml @@ -0,0 +1,18 @@ + + + + + + Graal_JDK_20 + + 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 439cfc1..f79934f 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 @@ -4,7 +4,9 @@ */ package edu.slcc.asdv.caleb.projecttrees_calebfontenot; +import java.util.LinkedList; import java.util.ListIterator; +import java.util.Queue; import java.util.Stack; /** @@ -22,37 +24,38 @@ public class TreeASDV { Node rightChild; } - public boolean insert(T t) - { - Node currentNode = root; - Node trailCurrent = root; - Node newNode = new Node(); + public boolean insert(T t) { + Node newNode = new Node<>(); newNode.data = t; - System.out.println(t); - if (this.root == null) { - this.root = newNode; + if (root == null) { + root = newNode; return true; } - while (currentNode != null) { - trailCurrent = currentNode; - if (t.compareTo(currentNode.data) >= 0) { - currentNode = currentNode.rightChild; + Node current = root; + Node parent = null; + + while (current != null) { + parent = current; + if (t.compareTo(current.data) >= 0) { + current = current.rightChild; } else { - currentNode = currentNode.leftChild; - } - if (t.compareTo(trailCurrent.data) >= 0) { // Make the node a right child. - trailCurrent.rightChild = newNode; - } else { // Make the node a left child. - trailCurrent.leftChild = newNode; + current = current.leftChild; } } - return false; + + // 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; + } else { + parent.leftChild = newNode; + } + + return true; } - private void inOrder(Node p) - { + private void inOrder(Node p) { if (p == null) { return; } @@ -62,13 +65,11 @@ public class TreeASDV { inOrder(p.rightChild); } - public void inOrder() - { + public void inOrder() { inOrder(this.root); } - public Node findNode(T t) - { + public Node findNode(T t) { Node currentNode = root; while (currentNode != null) { if (t.compareTo(currentNode.data) == 0) { @@ -82,103 +83,178 @@ public class TreeASDV { return null; } - public boolean remove(T t) - { - // case: no children - if (root.leftChild == null & root.rightChild == null) { - root = null; - return true; - } - Node current = this.root; - Node currentParent = this.root; + public boolean remove(T t) { + // Initialize parent and current nodes for traversal + Node parent = null; + Node current = root; - while (current.data.equals(t)) { - if (t.compareTo(current.data) == 0) { - break; - } - else if (t.compareTo(currentParent.data) > 0) { - currentParent = current; + // 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 { - currentParent = current; current = current.leftChild; } } + + // If node not found, return false if (current == null) { return false; } - if (current.data.compareTo(currentParent.data) <= 0) { - currentParent.leftChild = null; - } else { - currentParent.rightChild = null; - } - // case: single child - if (current.leftChild == null || current.rightChild == null) { - // if the current is the right child of its parent - if (current.data.compareTo(t) > 0) { - + + // 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: two children + } // 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 { - // take a left - Node p = current; - p = current.leftChild; - Node pTrailParent = p; - Node pTrail = p; - while (p != null) { // keep going right - pTrailParent = pTrail; - pTrail = p; - p = p.rightChild; - } - // swap the data of pTrail with the current - current.data = pTrail.data; - if (pTrail == pTrailParent) { - pTrailParent.leftChild = pTrail.leftChild; - } else { - pTrailParent.rightChild = pTrail.leftChild; - } - - + 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; } - public ListIterator listIterator(){ - ListIterator it = new ListIterator(); +// Helper method to find in-order successor of a node + private Node getSuccessor(Node node) { + Node current = node.rightChild; + Node successorParent = node; + Node successor = node; + + // Find the leftmost node in the right subtree (in-order successor) + while (current != null) { + successorParent = successor; + successor = current; + 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; + } + + return successor; + } + + public ListIterator listIterator() { + //ListIterator it = new ListIterator(); return null; -} - public void breathFirst() { - Stack> stack = new Stack(); - Node p = root; - System.out.println(p.data + " "); - stack.push(p.rightChild); - - while (stack.empty() == false) { - // go to the left - p = p.leftChild; - if (p != null) { - System.out.println(p.data); + } + + public void breadthFirstTraversal() { + if (root == null) { + return; + } + + Queue> queue = new LinkedList<>(); + queue.offer(root); + + while (!queue.isEmpty()) { + Node current = queue.poll(); + System.out.print(current.data + " "); + + if (current.leftChild != null) { + queue.offer(current.leftChild); + } + if (current.rightChild != null) { + queue.offer(current.rightChild); } - Node rightChild = stack.pop(); - System.out.println(); } } + - public static void main(String[] args) - { - TreeASDV tree = new TreeASDV(); - tree.insert(100); - tree.insert(80); - tree.insert(90); - tree.insert(95); - tree.insert(93); - tree.insert(70); - tree.inOrder(); - tree.remove(100); + public int height() { + return calculateHeight(root); + } + + private int calculateHeight(Node node) { + if (node == null) { + return 0; + } + + int leftHeight = calculateHeight(node.leftChild); + int rightHeight = calculateHeight(node.rightChild); + + return 1 + Math.max(leftHeight, rightHeight); + } + + public boolean isFullBST() { + int height = height(); + int nodeCount = countNodes(root); + return nodeCount == (1 << height) - 1; // Formula for full binary tree + } + + private int countNodes(Node node) { + if (node == null) { + return 0; + } + return 1 + countNodes(node.leftChild) + countNodes(node.rightChild); + } + + public void inorder() { + if (root == null) { + return; + } + + Stack> stack = new Stack<>(); + Node current = root; + + while (current != null || !stack.isEmpty()) { + while (current != null) { + stack.push(current); + current = current.leftChild; + } + current = stack.pop(); + System.out.print(current.data + " "); + current = current.rightChild; + } + } + + public static void main(String[] args) { + TreeASDV tree = new TreeASDV<>(); + // Insert some elements into the tree + tree.insert(5); + tree.insert(3); + tree.insert(7); + tree.insert(2); + tree.insert(4); + tree.insert(6); + tree.insert(8); + + // Test breadth-first traversal + System.out.println("Breadth-First Traversal:"); + tree.breadthFirstTraversal(); + System.out.println(); + + // Test height calculation + 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()); + + // Test inorder traversal without recursion + System.out.println("Inorder Traversal without Recursion:"); + tree.inorder(); System.out.println(); - tree.inOrder(); } }