agh
This commit is contained in:
parent
31f634c637
commit
a48615c8f8
@ -0,0 +1,18 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project-shared-configuration>
|
||||
<!--
|
||||
This file contains additional configuration written by modules in the NetBeans IDE.
|
||||
The configuration is intended to be shared among all the users of project and
|
||||
therefore it is assumed to be part of version control checkout.
|
||||
Without this configuration present, some functionality in the IDE may be limited or fail altogether.
|
||||
-->
|
||||
<properties xmlns="http://www.netbeans.org/ns/maven-properties-data/1">
|
||||
<!--
|
||||
Properties that influence various parts of the IDE, especially code formatting and the like.
|
||||
You can copy and paste the single properties, into the pom.xml file and the IDE will pick them up.
|
||||
That way multiple projects can share the same settings (useful for formatting rules for example).
|
||||
Any value defined here will override the pom.xml file value but is only applicable to the current project.
|
||||
-->
|
||||
<netbeans.hint.jdkPlatform>Graal_JDK_20</netbeans.hint.jdkPlatform>
|
||||
</properties>
|
||||
</project-shared-configuration>
|
@ -27,7 +27,7 @@ public class ManyToManyFactory {
|
||||
createManyToMany()
|
||||
{
|
||||
return new ManyToMany<Many1, Many2>() {
|
||||
private HashSet parents = new HashSet();
|
||||
//private HashSet parents = new HashSet();
|
||||
|
||||
private Map<Object, Object> left = new HashMap();
|
||||
private Map<Object, Object> 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<Many2> add(Many1 parentLeft, Many2... childrenRight)
|
||||
{
|
||||
List<Many2> returnList = new ArrayList<Many2>();
|
||||
// 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<Object, Object>) parentLeft;
|
||||
}
|
||||
if (this.left != childrenRight && this.right != null) {
|
||||
returnList.add((Many2) this.right);
|
||||
this.left = (Map<Object, Object>) childrenRight;
|
||||
left.put(parentLeft, Arrays.asList(childrenRight));
|
||||
|
||||
|
||||
// Check for exceptions
|
||||
if (!childrenRight.equals(parentLeft.getClass())) {
|
||||
throw new ClassCastException();
|
||||
}
|
||||
left.put(parentLeft, new ArrayList<Many2>(Arrays.asList(childrenRight)));
|
||||
return returnList;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -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}
|
||||
-->
|
||||
</style>
|
||||
</head>
|
||||
@ -31,138 +32,163 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> edu.slcc.asdv.caleb.projecttrees_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.util.ArrayList;
|
||||
<span class="literal">import</span> java.util.Iterator;
|
||||
<span class="literal">import</span> java.util.LinkedList;
|
||||
<span class="literal">import</span> java.util.ListIterator;
|
||||
<span class="comment">import</span> <span class="comment">java</span><span class="comment">.</span><span class="comment">util</span><span class="comment">.</span><span class="comment">ListIterator</span><span class="comment">;</span>
|
||||
<span class="literal">import</span> java.util.Queue;
|
||||
<span class="literal">import</span> java.util.Stack;
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="comment">@author</span> <span class="comment">caleb</span>
|
||||
<span class="comment"> * </span><span class="comment">@param</span> <span class="ST1"><T></span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">public</span> <span class="literal">class</span> TreeASDV<T <span class="literal">extends</span> Comparable> {
|
||||
<span class="literal">public</span> <span class="literal">class</span> TreeASDV<T <span class="literal">extends</span> Comparable> <span class="literal">implements</span> Cloneable {
|
||||
|
||||
<span class="literal">private</span> Node<T> <span class="ST1">root</span>;
|
||||
<span class="literal">private</span> Node<T> <span class="ST2">root</span>;
|
||||
|
||||
<span class="literal">class</span> Node<T> {
|
||||
|
||||
T <span class="ST1">data</span>;
|
||||
Node<T> <span class="ST1">leftChild</span>;
|
||||
Node<T> <span class="ST1">rightChild</span>;
|
||||
T <span class="ST2">data</span>;
|
||||
Node<T> <span class="ST2">leftChild</span>;
|
||||
Node<T> <span class="ST2">rightChild</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">insert</span>(T t) {
|
||||
Node<T> newNode = <span class="literal">new</span> Node<>();
|
||||
newNode.<span class="ST1">data</span> = t;
|
||||
@Override
|
||||
<span class="literal">public</span> Object <span class="ST3">clone</span>()
|
||||
{
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Cloning...</span><span class="string">"</span>);
|
||||
TreeASDV<T> clone = <span class="literal">new</span> TreeASDV<T>();
|
||||
ArrayList<T> oldData = <span class="literal">this</span>.getBreadthFirstArray();
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = <span class="number">0</span>; i < oldData.size(); ++i) {
|
||||
<span class="comment">//System.out.println(oldData.get(i));</span>
|
||||
clone.insert(oldData.get(i));
|
||||
}
|
||||
<span class="literal">return</span> clone;
|
||||
}
|
||||
|
||||
<span class="literal">if</span> (<span class="ST1">root</span> == <span class="literal">null</span>) {
|
||||
<span class="ST1">root</span> = newNode;
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">insert</span>(T t)
|
||||
{
|
||||
Node<T> newNode = <span class="literal">new</span> Node<>();
|
||||
newNode.<span class="ST2">data</span> = t;
|
||||
|
||||
<span class="literal">if</span> (<span class="ST2">root</span> == <span class="literal">null</span>) {
|
||||
<span class="ST2">root</span> = newNode;
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
Node<T> current = <span class="ST1">root</span>;
|
||||
Node<T> current = <span class="ST2">root</span>;
|
||||
Node<T> parent = <span class="literal">null</span>;
|
||||
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span>) {
|
||||
parent = current;
|
||||
<span class="literal">if</span> (t.compareTo(current.<span class="ST1">data</span>) >= <span class="number">0</span>) {
|
||||
current = current.<span class="ST1">rightChild</span>;
|
||||
<span class="literal">if</span> (t.compareTo(current.<span class="ST2">data</span>) >= <span class="number">0</span>) {
|
||||
current = current.<span class="ST2">rightChild</span>;
|
||||
} <span class="literal">else</span> {
|
||||
current = current.<span class="ST1">leftChild</span>;
|
||||
current = current.<span class="ST2">leftChild</span>;
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">// At this point, 'parent' is the node where the new node should be inserted as a child</span>
|
||||
<span class="literal">if</span> (t.compareTo(parent.<span class="ST1">data</span>) >= <span class="number">0</span>) {
|
||||
parent.<span class="ST1">rightChild</span> = newNode;
|
||||
<span class="literal">if</span> (t.compareTo(parent.<span class="ST2">data</span>) >= <span class="number">0</span>) {
|
||||
parent.<span class="ST2">rightChild</span> = newNode;
|
||||
} <span class="literal">else</span> {
|
||||
parent.<span class="ST1">leftChild</span> = newNode;
|
||||
parent.<span class="ST2">leftChild</span> = newNode;
|
||||
}
|
||||
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
<span class="literal">private</span> <span class="literal">void</span> <span class="ST2">inOrder</span>(Node<T> p) {
|
||||
<span class="literal">private</span> <span class="literal">void</span> <span class="comment">inOrder</span>(Node<T> p)
|
||||
{
|
||||
<span class="literal">if</span> (p == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
|
||||
inOrder(p.<span class="ST1">leftChild</span>);
|
||||
System.<span class="ST3">out</span>.print(p.<span class="ST1">data</span> + <span class="string">"</span> <span class="string">"</span>);
|
||||
inOrder(p.<span class="ST1">rightChild</span>);
|
||||
inOrder(p.<span class="ST2">leftChild</span>);
|
||||
System.<span class="ST4">out</span>.print(p.<span class="ST2">data</span> + <span class="string">"</span> <span class="string">"</span>);
|
||||
inOrder(p.<span class="ST2">rightChild</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">inOrder</span>() {
|
||||
inOrder(<span class="literal">t</span><span class="literal">his</span>.<span class="ST1">root</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> Node<T> <span class="ST2">findNode</span>(T t) {
|
||||
Node<T> currentNode = <span class="ST1">root</span>;
|
||||
<span class="literal">public</span> Node<T> <span class="ST3">findNode</span>(T t)
|
||||
{
|
||||
Node<T> currentNode = <span class="ST2">root</span>;
|
||||
<span class="literal">while</span> (currentNode != <span class="literal">null</span>) {
|
||||
<span class="literal">if</span> (t.compareTo(currentNode.<span class="ST1">data</span>) == <span class="number">0</span>) {
|
||||
<span class="literal">if</span> (t.compareTo(currentNode.<span class="ST2">data</span>) == <span class="number">0</span>) {
|
||||
<span class="literal">return</span> currentNode;
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (t.compareTo(currentNode.<span class="ST1">data</span>) > <span class="number">0</span>) {
|
||||
currentNode = currentNode.<span class="ST1">rightChild</span>;
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (t.compareTo(currentNode.<span class="ST2">data</span>) > <span class="number">0</span>) {
|
||||
currentNode = currentNode.<span class="ST2">rightChild</span>;
|
||||
} <span class="literal">else</span> {
|
||||
currentNode = currentNode.<span class="ST1">leftChild</span>;
|
||||
currentNode = currentNode.<span class="ST2">leftChild</span>;
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> <span class="literal">null</span>;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">remove</span>(T t) {
|
||||
<span class="comment">// Initialize parent and current nodes for traversal</span>
|
||||
Node<T> parent = <span class="literal">null</span>;
|
||||
Node<T> current = <span class="ST2">root</span>;
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">remove</span>(T t) {
|
||||
<span class="comment">// Initialize parent and current nodes for traversal</span>
|
||||
Node<T> parent = <span class="literal">null</span>;
|
||||
Node<T> current = <span class="ST1">root</span>;
|
||||
|
||||
<span class="comment">// Search for the node to be removed</span>
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span> && !current.<span class="ST1">data</span>.equals(t)) {
|
||||
parent = current;
|
||||
<span class="literal">if</span> (t.compareTo(current.<span class="ST1">data</span>) > <span class="number">0</span>) {
|
||||
current = current.<span class="ST1">rightChild</span>;
|
||||
} <span class="literal">else</span> {
|
||||
current = current.<span class="ST1">leftChild</span>;
|
||||
}
|
||||
<span class="comment">// Search for the node to be removed</span>
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span> && !current.<span class="ST2">data</span>.equals(t)) {
|
||||
parent = current;
|
||||
<span class="literal">if</span> (t.compareTo(current.<span class="ST2">data</span>) > <span class="number">0</span>) {
|
||||
current = current.<span class="ST2">rightChild</span>;
|
||||
} <span class="literal">else</span> {
|
||||
current = current.<span class="ST2">leftChild</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// If node not found, return false</span>
|
||||
<span class="literal">if</span> (current == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// Case 1: Node with no children</span>
|
||||
<span class="literal">if</span> (current.<span class="ST1">leftChild</span> == <span class="literal">null</span> && current.<span class="ST1">rightChild</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">if</span> (current == <span class="ST1">root</span>) {
|
||||
<span class="ST1">root</span> = <span class="literal">null</span>; <span class="comment">// Removing root node</span>
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (parent.<span class="ST1">leftChild</span> == current) {
|
||||
parent.<span class="ST1">leftChild</span> = <span class="literal">null</span>; <span class="comment">// Removing a left child</span>
|
||||
} <span class="literal">else</span> {
|
||||
parent.<span class="ST1">rightChild</span> = <span class="literal">null</span>; <span class="comment">// Removing a right child</span>
|
||||
}
|
||||
} <span class="comment">// Case 2: Node with one child</span>
|
||||
<span class="literal">else</span> <span class="literal">if</span> (current.<span class="ST1">leftChild</span> == <span class="literal">null</span> || current.<span class="ST1">rightChild</span> == <span class="literal">null</span>) {
|
||||
Node<T> child = (current.<span class="ST1">leftChild</span> != <span class="literal">null</span>) ? current.<span class="ST1">leftChild</span> : current.<span class="ST1">rightChild</span>;
|
||||
<span class="literal">if</span> (current == <span class="ST1">root</span>) {
|
||||
<span class="ST1">root</span> = child; <span class="comment">// Replace root with its child</span>
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (parent.<span class="ST1">leftChild</span> == current) {
|
||||
parent.<span class="ST1">leftChild</span> = child; <span class="comment">// Replace parent's left child with the node's child</span>
|
||||
} <span class="literal">else</span> {
|
||||
parent.<span class="ST1">rightChild</span> = child; <span class="comment">// Replace parent's right child with the node's child</span>
|
||||
}
|
||||
} <span class="comment">// Case 3: Node with two children</span>
|
||||
<span class="literal">else</span> {
|
||||
Node<T> successor = getSuccessor(current);
|
||||
current.<span class="ST1">data</span> = successor.<span class="ST1">data</span>; <span class="comment">// Replace data with successor's data</span>
|
||||
<span class="comment">// Remove successor node (successor will have at most one right child)</span>
|
||||
remove(successor.<span class="ST1">data</span>);
|
||||
}
|
||||
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// If node not found, return false</span>
|
||||
<span class="literal">if</span> (current == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="literal">false</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// Case 1: Node with no children</span>
|
||||
<span class="literal">if</span> (current.<span class="ST2">leftChild</span> == <span class="literal">null</span> && current.<span class="ST2">rightChild</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">if</span> (current == <span class="ST2">root</span>) {
|
||||
<span class="ST2">root</span> = <span class="literal">null</span>; <span class="comment">// Removing root node</span>
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (parent.<span class="ST2">leftChild</span> == current) {
|
||||
parent.<span class="ST2">leftChild</span> = <span class="literal">null</span>; <span class="comment">// Removing a left child</span>
|
||||
} <span class="literal">else</span> {
|
||||
parent.<span class="ST2">rightChild</span> = <span class="literal">null</span>; <span class="comment">// Removing a right child</span>
|
||||
}
|
||||
} <span class="comment">// Case 2: Node with one child</span>
|
||||
<span class="literal">else</span> <span class="literal">if</span> (current.<span class="ST2">leftChild</span> == <span class="literal">null</span> || current.<span class="ST2">rightChild</span> == <span class="literal">null</span>) {
|
||||
Node<T> child = (current.<span class="ST2">leftChild</span> != <span class="literal">null</span>) ? current.<span class="ST2">leftChild</span> : current.<span class="ST2">rightChild</span>;
|
||||
<span class="literal">if</span> (current == <span class="ST2">root</span>) {
|
||||
<span class="ST2">root</span> = child; <span class="comment">// Replace root with its child</span>
|
||||
} <span class="literal">else</span> <span class="literal">if</span> (parent.<span class="ST2">leftChild</span> == current) {
|
||||
parent.<span class="ST2">leftChild</span> = child; <span class="comment">// Replace parent's left child with the node's child</span>
|
||||
} <span class="literal">else</span> {
|
||||
parent.<span class="ST2">rightChild</span> = child; <span class="comment">// Replace parent's right child with the node's child</span>
|
||||
}
|
||||
} <span class="comment">// Case 3: Node with two children</span>
|
||||
<span class="literal">else</span> {
|
||||
Node<T> successorParent = current;
|
||||
Node<T> successor = current.<span class="ST2">rightChild</span>;
|
||||
<span class="literal">while</span> (successor.<span class="ST2">leftChild</span> != <span class="literal">null</span>) {
|
||||
successorParent = successor;
|
||||
successor = successor.<span class="ST2">leftChild</span>;
|
||||
}
|
||||
current.<span class="ST2">data</span> = successor.<span class="ST2">data</span>; <span class="comment">// Replace data with successor's data</span>
|
||||
<span class="literal">if</span> (successorParent == current) {
|
||||
successorParent.<span class="ST2">rightChild</span> = successor.<span class="ST2">rightChild</span>;
|
||||
} <span class="literal">else</span> {
|
||||
successorParent.<span class="ST2">leftChild</span> = successor.<span class="ST2">rightChild</span>;
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">return</span> <span class="literal">true</span>;
|
||||
}
|
||||
|
||||
|
||||
<span class="comment">// Helper method to find in-order successor of a node</span>
|
||||
<span class="literal">private</span> Node<T> <span class="ST2">getSuccessor</span>(Node<T> node) {
|
||||
Node<T> current = node.<span class="ST1">rightChild</span>;
|
||||
<span class="literal">private</span> Node<T> <span class="comment">getSuccessor</span>(Node<T> node)
|
||||
{
|
||||
Node<T> current = node.<span class="ST2">rightChild</span>;
|
||||
Node<T> successorParent = node;
|
||||
Node<T> successor = node;
|
||||
|
||||
@ -170,94 +196,172 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span>) {
|
||||
successorParent = successor;
|
||||
successor = current;
|
||||
current = current.<span class="ST1">leftChild</span>;
|
||||
current = current.<span class="ST2">leftChild</span>;
|
||||
}
|
||||
|
||||
<span class="comment">// If the successor is not the right child of the node to be removed,</span>
|
||||
<span class="comment">// adjust the successor's parent's leftChild reference</span>
|
||||
<span class="literal">if</span> (successor != node.<span class="ST1">rightChild</span>) {
|
||||
successorParent.<span class="ST1">leftChild</span> = successor.<span class="ST1">rightChild</span>;
|
||||
successor.<span class="ST1">rightChild</span> = node.<span class="ST1">rightChild</span>;
|
||||
<span class="literal">if</span> (successor != node.<span class="ST2">rightChild</span>) {
|
||||
successorParent.<span class="ST2">leftChild</span> = successor.<span class="ST2">rightChild</span>;
|
||||
successor.<span class="ST2">rightChild</span> = node.<span class="ST2">rightChild</span>;
|
||||
}
|
||||
|
||||
<span class="literal">return</span> successor;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> ListIterator<T> <span class="ST2">listIterator</span>() {
|
||||
<span class="comment">//ListIterator it = new ListIterator<T>();</span>
|
||||
<span class="literal">return</span> <span class="literal">null</span>;
|
||||
<span class="comment">// Inorder ListIterator</span>
|
||||
<span class="literal">public</span> Iterator<T> <span class="ST3">listIterator</span>()
|
||||
{
|
||||
<span class="literal">return</span> <span class="literal">new</span> InorderIterator();
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">breadthFirstTraversal</span>() {
|
||||
<span class="literal">if</span> (<span class="ST1">root</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">private</span> <span class="literal">class</span> InorderIterator <span class="literal">implements</span> Iterator<T> {
|
||||
|
||||
<span class="literal">private</span> Stack<Node<T>> <span class="ST2">stack</span>;
|
||||
|
||||
<span class="literal">public</span> InorderIterator()
|
||||
{
|
||||
<span class="ST2">stack</span> = <span class="literal">new</span> Stack<>();
|
||||
pushLeft(<span class="ST2">r</span><span class="ST2">oot</span>);
|
||||
}
|
||||
|
||||
<span class="literal">private</span> <span class="literal">void</span> <span class="ST3">pushLeft</span>(Node<T> node)
|
||||
{
|
||||
<span class="literal">while</span> (node != <span class="literal">null</span>) {
|
||||
<span class="ST2">stack</span>.push(node);
|
||||
node = node.<span class="ST2">leftChild</span>;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">hasNext</span>()
|
||||
{
|
||||
<span class="literal">return</span> !<span class="ST2">stack</span>.isEmpty();
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> T <span class="ST3">next</span>()
|
||||
{
|
||||
<span class="literal">if</span> (!hasNext()) {
|
||||
<span class="literal">throw</span> <span class="literal">new</span> java.util.NoSuchElementException();
|
||||
}
|
||||
Node<T> current = <span class="ST2">stack</span>.pop();
|
||||
pushLeft(current.<span class="ST2">rightChild</span>);
|
||||
<span class="literal">return</span> current.<span class="ST2">data</span>;
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">remove</span>()
|
||||
{
|
||||
<span class="literal">throw</span> <span class="literal">new</span> UnsupportedOperationException();
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> ArrayList<T> <span class="ST3">getBreadthFirstArray</span>()
|
||||
{
|
||||
ArrayList<T> returnArray = <span class="literal">new</span> ArrayList<T>();
|
||||
<span class="literal">if</span> (<span class="ST2">root</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> returnArray;
|
||||
}
|
||||
Queue<Node<T>> queue = <span class="literal">new</span> LinkedList<>();
|
||||
|
||||
queue.offer(<span class="ST2">r</span><span class="ST2">oot</span>);
|
||||
|
||||
<span class="literal">while</span> (!queue.isEmpty()) {
|
||||
Node<T> current = queue.poll();
|
||||
returnArray.add(current.<span class="ST2">data</span>);
|
||||
|
||||
<span class="literal">if</span> (current.<span class="ST2">leftChild</span> != <span class="literal">null</span>) {
|
||||
queue.offer(current.<span class="ST2">leftChild</span>);
|
||||
}
|
||||
<span class="literal">if</span> (current.<span class="ST2">rightChild</span> != <span class="literal">null</span>) {
|
||||
queue.offer(current.<span class="ST2">rightChild</span>);
|
||||
}
|
||||
}
|
||||
<span class="literal">return</span> returnArray;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">breadthFirstTraversal</span>()
|
||||
{
|
||||
<span class="literal">if</span> (<span class="ST2">root</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
|
||||
Queue<Node<T>> queue = <span class="literal">new</span> LinkedList<>();
|
||||
queue.offer(<span class="ST1">r</span><span class="ST1">oot</span>);
|
||||
queue.offer(<span class="ST2">r</span><span class="ST2">oot</span>);
|
||||
|
||||
<span class="literal">while</span> (!queue.isEmpty()) {
|
||||
Node<T> current = queue.poll();
|
||||
System.<span class="ST3">out</span>.print(current.<span class="ST1">data</span> + <span class="string">"</span> <span class="string">"</span>);
|
||||
System.<span class="ST4">out</span>.print(current.<span class="ST2">data</span> + <span class="string">"</span> <span class="string">"</span>);
|
||||
|
||||
<span class="literal">if</span> (current.<span class="ST1">leftChild</span> != <span class="literal">null</span>) {
|
||||
queue.offer(current.<span class="ST1">leftChild</span>);
|
||||
<span class="literal">if</span> (current.<span class="ST2">leftChild</span> != <span class="literal">null</span>) {
|
||||
queue.offer(current.<span class="ST2">leftChild</span>);
|
||||
}
|
||||
<span class="literal">if</span> (current.<span class="ST1">rightChild</span> != <span class="literal">null</span>) {
|
||||
queue.offer(current.<span class="ST1">rightChild</span>);
|
||||
<span class="literal">if</span> (current.<span class="ST2">rightChild</span> != <span class="literal">null</span>) {
|
||||
queue.offer(current.<span class="ST2">rightChild</span>);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">equals</span>(Object obj) {
|
||||
<span class="literal">return</span> <span class="literal">this</span>.getBreadthFirstArray().equals(((TreeASDV) obj).getBreadthFirstArray());
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">height</span>() {
|
||||
<span class="literal">return</span> calculateHeight(<span class="ST1">r</span><span class="ST1">oot</span>);
|
||||
<span class="literal">public</span> <span class="literal">int</span> <span class="ST3">height</span>()
|
||||
{
|
||||
<span class="literal">return</span> calculateHeight(<span class="ST2">r</span><span class="ST2">oot</span>);
|
||||
}
|
||||
|
||||
<span class="literal">private</span> <span class="literal">int</span> <span class="ST2">calculateHeight</span>(Node<T> node) {
|
||||
<span class="literal">private</span> <span class="literal">int</span> <span class="ST3">calculateHeight</span>(Node<T> node)
|
||||
{
|
||||
<span class="literal">if</span> (node == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="number">0</span>;
|
||||
}
|
||||
|
||||
<span class="literal">int</span> leftHeight = calculateHeight(node.<span class="ST1">leftChild</span>);
|
||||
<span class="literal">int</span> rightHeight = calculateHeight(node.<span class="ST1">rightChild</span>);
|
||||
<span class="literal">int</span> leftHeight = calculateHeight(node.<span class="ST2">leftChild</span>);
|
||||
<span class="literal">int</span> rightHeight = calculateHeight(node.<span class="ST2">rightChild</span>);
|
||||
|
||||
<span class="literal">return</span> <span class="number">1</span> + Math.<span class="ST4">max</span>(leftHeight, rightHeight);
|
||||
<span class="literal">return</span> <span class="number">1</span> + Math.<span class="ST5">max</span>(leftHeight, rightHeight);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST2">isFullBST</span>() {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">boolean</span> <span class="ST3">isFullBST</span>()
|
||||
{
|
||||
<span class="literal">int</span> height = height();
|
||||
<span class="literal">int</span> nodeCount = countNodes(<span class="ST1">r</span><span class="ST1">oot</span>);
|
||||
<span class="literal">int</span> nodeCount = countNodes(<span class="ST2">r</span><span class="ST2">oot</span>);
|
||||
<span class="literal">return</span> nodeCount == (<span class="number">1</span> << height) - <span class="number">1</span>; <span class="comment">// Formula for full binary tree</span>
|
||||
}
|
||||
|
||||
<span class="literal">private</span> <span class="literal">int</span> <span class="ST2">countNodes</span>(Node<T> node) {
|
||||
<span class="literal">private</span> <span class="literal">int</span> <span class="ST3">countNodes</span>(Node<T> node)
|
||||
{
|
||||
<span class="literal">if</span> (node == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span> <span class="number">0</span>;
|
||||
}
|
||||
<span class="literal">return</span> <span class="number">1</span> + countNodes(node.<span class="ST1">leftChild</span>) + countNodes(node.<span class="ST1">rightChild</span>);
|
||||
<span class="literal">return</span> <span class="number">1</span> + countNodes(node.<span class="ST2">leftChild</span>) + countNodes(node.<span class="ST2">rightChild</span>);
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">inorder</span>() {
|
||||
<span class="literal">if</span> (<span class="ST1">root</span> == <span class="literal">null</span>) {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">void</span> <span class="ST3">inOrder</span>()
|
||||
{
|
||||
<span class="literal">if</span> (<span class="ST2">root</span> == <span class="literal">null</span>) {
|
||||
<span class="literal">return</span>;
|
||||
}
|
||||
|
||||
Stack<Node<T>> stack = <span class="literal">new</span> Stack<>();
|
||||
Node<T> current = <span class="ST1">root</span>;
|
||||
Node<T> current = <span class="ST2">root</span>;
|
||||
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span> || !stack.isEmpty()) {
|
||||
<span class="literal">while</span> (current != <span class="literal">null</span>) {
|
||||
stack.push(current);
|
||||
current = current.<span class="ST1">leftChild</span>;
|
||||
current = current.<span class="ST2">leftChild</span>;
|
||||
}
|
||||
current = stack.pop();
|
||||
System.<span class="ST3">out</span>.print(current.<span class="ST1">data</span> + <span class="string">"</span> <span class="string">"</span>);
|
||||
current = current.<span class="ST1">rightChild</span>;
|
||||
System.<span class="ST4">out</span>.print(current.<span class="ST2">data</span> + <span class="string">"</span> <span class="string">"</span>);
|
||||
current = current.<span class="ST2">rightChild</span>;
|
||||
}
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST5">main</span>(String[] args) {
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST6">main</span>(String[] args) <span class="literal">throws</span> CloneNotSupportedException
|
||||
{
|
||||
TreeASDV<Integer> tree = <span class="literal">new</span> TreeASDV<>();
|
||||
<span class="comment">// Insert some elements into the tree</span>
|
||||
tree.insert(<span class="number">5</span>);
|
||||
@ -269,20 +373,31 @@ table {color: #888888; background-color: #313335; font-family: monospace; font-w
|
||||
tree.insert(<span class="number">8</span>);
|
||||
|
||||
<span class="comment">// Test breadth-first traversal</span>
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Breadth-First Traversal:</span><span class="string">"</span>);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Breadth-First Traversal:</span><span class="string">"</span>);
|
||||
tree.breadthFirstTraversal();
|
||||
System.<span class="ST3">out</span>.println();
|
||||
System.<span class="ST4">out</span>.println();
|
||||
|
||||
<span class="comment">// Test height calculation</span>
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Height of the tree: </span><span class="string">"</span> + tree.height());
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Height of the tree: </span><span class="string">"</span> + tree.height());
|
||||
|
||||
<span class="comment">// Test if the tree is a full binary tree</span>
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Is the tree a full binary tree? </span><span class="string">"</span> + tree.isFullBST());
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Is the tree a full binary tree? </span><span class="string">"</span> + tree.isFullBST());
|
||||
|
||||
<span class="comment">// Test inorder traversal without recursion</span>
|
||||
System.<span class="ST3">out</span>.println(<span class="string">"</span><span class="string">Inorder Traversal without Recursion:</span><span class="string">"</span>);
|
||||
tree.inorder();
|
||||
System.<span class="ST3">out</span>.println();
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Inorder Traversal without Recursion:</span><span class="string">"</span>);
|
||||
tree.inOrder();
|
||||
System.<span class="ST4">out</span>.println();
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">array list from breadth traversal</span><span class="string">"</span>);
|
||||
System.<span class="ST4">out</span>.println(tree.getBreadthFirstArray());
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Cloned Tree:</span><span class="string">"</span>);
|
||||
TreeASDV<Integer> clonedTree = (TreeASDV<Integer>) tree.clone();
|
||||
clonedTree.breadthFirstTraversal();
|
||||
System.<span class="ST4">out</span>.println();
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Does the original tree and the clone tree equal? </span><span class="string">"</span> + (tree.equals(clonedTree) ? <span class="string">"</span><span class="string">yes</span><span class="string">"</span> : <span class="string">"</span><span class="string">no</span><span class="string">"</span>) );
|
||||
tree.insert(<span class="number">3</span><span class="number">000000</span>);
|
||||
System.<span class="ST4">out</span>.println(<span class="string">"</span><span class="string">Does the original tree and the clone tree equal? </span><span class="string">"</span> + (tree.equals(clonedTree) ? <span class="string">"</span><span class="string">yes</span><span class="string">"</span> : <span class="string">"</span><span class="string">no</span><span class="string">"</span>) );
|
||||
tree.remove(<span class="number">5</span>);
|
||||
tree.breadthFirstTraversal();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,57 +98,65 @@ public class TreeASDV<T extends Comparable> implements Cloneable {
|
||||
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)
|
||||
{
|
||||
@ -360,5 +368,7 @@ public class TreeASDV<T extends Comparable> 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();
|
||||
}
|
||||
}
|
||||
|
BIN
Semester 4/ZIPs/ProjectTrees_CalebFontenot.zip
Normal file
BIN
Semester 4/ZIPs/ProjectTrees_CalebFontenot.zip
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user