Reset author name to chosen name

This commit is contained in:
2025-10-19 22:02:41 -05:00
parent 168b35c94a
commit 578c33de70
316 changed files with 17381 additions and 0 deletions

View File

@@ -0,0 +1,791 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.chloe.hashing_chloefontenot;
/**
*
* @author chloe
*/
import asdv.ListASDV;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.function.BiConsumer;//needed in putAll
public class MapASDV<K, V> implements Map<K, V>, Cloneable
{
private int capacity = 4;
private double loadFactor = 0.75;
private ArrayList<ListASDV<EntryASDV<K, V>>> map = new ArrayList<ListASDV<EntryASDV<K, V>>>();
private Set<K> sharedKeySet = new SharedSet<K>();
private Set<Entry<K, V>> sharedEntrySet = new SharedSet<Entry<K, V>>();
private Collection<V> sharedValuesCollection = new SharedCollection<V>();
private class SharedCollection<V> extends ArrayList<V>
{
public boolean addValue(V v)
{
return this.add(v);
}
@Override
public boolean remove(Object o)
{
//> The parameter is key not entry if we are here
//>>remove value) and key) from the map
return MapASDV.this.removeFirstValue(o);
//>>remove key from shared values set
//return super.remove(o);
}
/**
* Removes one value from the collection. This method is meant to be
* called from out class. The overridden remove(Object o) of this inner
* class, calls the remove of the outer class(MapASDV), and the remove
* of the outer class, calls remove(V v, boolean callFromOuterClass)
* instead of remove(Object o) to avoid Stack Overflow when remover of
* inner calls remove of outer and vice versa.
*
* @param o - the key
* @param callFromOuterClass - dummy variable.
* @return true if the key was removed from the Set
*/
public boolean remove(V v, boolean callFromOuterClass)
{
//remove key from shared keys set
boolean b = super.remove(v);
return b;
}
@Override
public Object clone()
{
return super.clone();
}
@Override
public void clear()
{
super.clear();
}
}
private class SharedSet<E> extends HashSet<E>
{
@Override
public boolean add(E e)
{
throw new UnsupportedOperationException("Not supported....");
}
@Override
public boolean removeAll(Collection<?> c)
{
throw new UnsupportedOperationException("Not supported....");
}
/**
* Adds an EntryASDV to the set. It is private and cannot be used by the
* user of the Set. It is used from the MapASDV to add EntriesASDV to
* the SharedSet. Without this method we wouldn't be able to create a
* Set of keys or a Set of entries to give to the user via methods
* keySet() and entrySet() of this Map
*
* @param e EntryASDV
* @return true if the entry was added false otherwise
*/
private boolean addEntry(E e)
{
return super.add(e);
}
@Override
public boolean remove(Object o)
{
//>if parameter oo is EntryASDV call auxiliary method removeEntry
if (o instanceof EntryASDV)
{
return removeEntry((EntryASDV) o);
}
//> The parameter is key not entry if we are here
//>>remove key from the map
MapASDV.this.remove(o);
//>>remove key from shared keys set
return super.remove(o);
}
/**
* Removes the entry for the shared set
*
* @param entry the entry to be removed
* @return true if the entry was removed, false otherwise
*/
private boolean removeEntry(EntryASDV<K, V> entry)
{
MapASDV.this.remove(entry.getKey());
return super.remove(entry);
}
/**
* Removes the key from the set. This method is meant to be called from
* out class. The overridden remove(Object o) of this inner class calls
* the remove of the out class, and the remove of the outer class calls
* remove(K o, boolean callFromOuterClass) instead of remove(Object o)
* to avoid Stack Overflow when remover of inner calls remove of outer
* and vice versa.
*
* @param o - the key
* @param callFromOuterClass - dummy variable.
* @return true if the key was removed from the Set
*/
public boolean remove(E o, boolean callFromOuterClass)
{
//remove key from shared keys set
return super.remove(o);
}
@Override
public Object clone()
{
return super.clone();
}
@Override
public void clear()
{
super.clear();
}
}
public MapASDV()
{
for (int i = 0; i < capacity; ++i)
{
map.add(new ListASDV<EntryASDV<K, V>>());
}
}
/**
* Double the size of the Map and rehashes the entries of the Map
*/
private void doubleTheSizeOfTheMapAndRehash()
{
capacity *= 2;
//>create a new arrayList of ListsASDV
ArrayList<ListASDV<EntryASDV<K, V>>> newMap = new ArrayList<ListASDV<EntryASDV<K, V>>>();
//>Add at every enetry of the arrayList a new ASDVList
for (int i = 0; i < capacity; ++i)
{
newMap.add(new ListASDV<EntryASDV<K, V>>());
}
//>for the size of the OLD arrayList
for (int i = 0; i < map.size(); ++i)//array list
{
//>> get The ASDVlist at i
ListASDV<EntryASDV<K, V>> list = map.get(i);
//>>for the size() of the ASDVlist
for (int j = 0; j < list.size(); ++j)
{
///>>>hash and put in the the new array
int index = hash( list.get(j).getKey().hashCode() );
newMap.get(index).add(list.get(j));
}
}
map = newMap;
}
class EntryASDV<K, V> implements Entry<K, V>, Comparable<K>
{
K key;
V value;
public EntryASDV(K key, V value)
{
this.key = key;
this.value = value;
}
@Override
public K getKey()
{
return key;
}
@Override
public V getValue()
{
return value;
}
@Override
public V setValue(V value)
{
V oldValue = this.value;
this.value = value;
return oldValue;
}
@Override
public String toString()
{
return "EntryASDV{" + "key=" + key + ", value=" + value + '}';
}
@Override
public boolean equals(Object obj)
{
if (this == obj)
{
return true;
}
if (obj == null)
{
return false;
}
if (getClass() != obj.getClass())
{
return false;
}
final EntryASDV<?, ?> other = (EntryASDV<?, ?>) obj;
if (!Objects.equals(this.key, other.key))
{
return false;
}
return true;
}
/**
*
* @param o
* @return throws IllegalArgumentException if parameter class is not K
*/
@Override
public int compareTo(K o)
{
if (getClass() != o.getClass())
{
throw new IllegalArgumentException("ellegal parameter " + o);
}
return ((Comparable) key).compareTo(o);
}
}
@Override
public int size()
{
return this.keySet().size();
}
@Override
public boolean isEmpty()
{
throw new UnsupportedOperationException("Not supported....");
}
@Override
public boolean containsKey(Object key)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Return an entry of the map
*
* @param key the key of the entry to be returned
* @return the entry, or null if the key is not in the map
*/
private EntryASDV<K, V> getEntryForKey(Object key)
{
for (ListASDV<EntryASDV<K, V>> list : map)
{
Iterator<EntryASDV<K, V>> it = list.iterator();
while (it.hasNext())
{
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey()))
{
return entry;
}
}
}
return null;
}
/**
* Returns the index of the given key
*
* @param key a key of the map
* @return the index of a key in the map or -1, if the key is not in the map
*/
private int getIndexForKey(Object key)
{
for (int i = 0; i < map.size(); ++i)
{
Iterator<EntryASDV<K, V>> it = map.get(i).iterator();
while (it.hasNext())
{
EntryASDV<K, V> entry = it.next();
if (key.equals(entry.getKey()))
{
return i;
}
}
}
return -1;
}
/**
* Returns true if this map maps one or more keys to the specified value.
* More formally, returns true if and only if this map contains at least one
* mapping to a value v such that (value==null ? v==null : value.equals(v)).
* This operation will probably require time linear in the map size for most
* implementations of the Map interface.
*
* Parameters: value - value whose presence in this map is to be tested
* Returns: true if this map maps one or more keys to the specified value
* Throws: ClassCastException - if the value is of an inappropriate type for
* this map (optional) NullPointerException - if the specified value is null
* and this map does not permit null values (optional)
*
* @param value - value whose presence in this map is to be tested
* @return true if this map maps one or more keys to the specified value
* @throws NullPointerException - if the specified value is null and this
* map does not permit null values (optional)
*/
@Override
public boolean containsValue(Object value)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Returns the value to which the specified key is mapped, or null if this
* map contains no mapping for the key.
*
* More formally, if this map contains a mapping from a key k to a value v
* such that (key==null ? k==null : key.equals(k)), then this method returns
* v; otherwise it returns null. (There can be at most one such mapping.)
*
* If this map permits null values, then a return value of null does not
* necessarily indicate that the map contains no mapping for the key; it's
* also possible that the map explicitly maps the key to null. The
* containsKey operation may be used to distinguish these two cases.
*
* @param key - the key whose associated value is to be returned
* @return the value to which the specified key is mapped, or null if this
* map contains no mapping for the key
* @throws NullPointerException - if the specified key is null and this map
* does not permit null keys (optional)
*/
@Override
public V get(Object key)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Associates the specified value with the specified key in this map
* (optional operation). If the map previously contained a mapping for the
* key, the old value is replaced by the specified value. (A map m is said
* to contain a mapping for a key k if and only if m.containsKey(k) would
* return true.)
*
* @param key - key with which the specified value is to be associated
* @param value - value to be associated with the specified key
* @return the previous value associated with key, or null if there was no
* mapping for key. (A null return can also indicate that the map previously
* associated null with key, if the implementation supports null values.)
* @throws NullPointerException - if specified key or value is null and this
* map does not permit null keys
*/
@Override
public V put(K key, V value)
{
if (key == null || value == null)
{
throw new NullPointerException("parm(s) null");
}
//>if contains the key, replace the key's value
EntryASDV<K, V> entry = getEntryForKey(key);
if (entry != null)
{
V oldValue = entry.value;
entry.value = value;
return oldValue;
}
///>>hash and put in the array
int code = this.hash(key.hashCode());
int index = hash(code);
ListASDV<EntryASDV<K, V>> list = map.get(index);
EntryASDV e = new EntryASDV(key, value);
list.add(e);
//>>add the key to the shared keys-set
((SharedSet<K>) this.sharedKeySet).addEntry(key);
((SharedSet<Entry<K, V>>) this.sharedEntrySet).addEntry(e);
//>>get the value of this entry
V v = list.get(list.size() - 1).getValue();
//>> add value to the value collection
((SharedCollection<V>) this.sharedValuesCollection).addValue(v);
//>> if reach 75% capacity double the size
if ((double) this.size() / capacity >= 0.75)
{
this.doubleTheSizeOfTheMapAndRehash();
}
//>>return the value of Entry just added
return v;
}
int hash(int keyHashCode)
{
int h = hashHash(keyHashCode);
return Math.abs(h % capacity - 1);
}
/**
* Removes the first entry with the given values.
*
* @param value - the value to be removed
* @return true if removed, false otherwise
* @throws NullPointerException if the value is null
*/
private boolean removeFirstValue(Object value)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Ensure hash code is evenly distributed
*
* @param h - hash code
* @return evenly distributed hash code
*/
private static int hashHash(int h)
{
h ^= (h >>> 20) ^ (h >>> 12);
return h ^ (h >>> 7) ^ (h >>> 4);
}
/**
* Removes the mapping for a key from this map if it is present (optional
* operation). More formally, if this map contains a mapping from key k to
* value v such that (key==null ? k==null : key.equals(k)), that mapping is
* removed. (The map can contain at most one such mapping.)
*
* @param key - key whose mapping is to be removed from the map
* @return the previous value associated with key, or null if there was no
* mapping for key.
* @throws NullPointerException - if the specified key is null and this map
* does not permit null keys
*/
@Override
public V remove(Object key)
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Copies all of the mappings from the specified map to this map (optional
* operation). The effect of this call is equivalent to that of calling
* put(k, v) on this map once for each mapping from key k to value v in the
* specified map. The behavior of this operation is undefined if the
* specified map is modified while the operation is in progress.
*
* @param m - mappings to be stored in this map
* @throws NullPointerException - if the specified map is null, or if this
* map does not permit null keys or values, and the specified map contains
* null keys
*/
@Override
public void putAll(Map<? extends K, ? extends V> m)
{
if (m == null)
{
throw new NullPointerException("null parameter");
}
BiConsumer consumeEachEntry = new BiConsumer<K, V>()
{
MapASDV<K, V> mapForConsumer = MapASDV.this;
@Override
public void accept(K k, V v)
{
mapForConsumer.put(k, v);
}
};
m.forEach(consumeEachEntry);
}
/**
* Removes all of the mappings from this map (optional operation). The map
* will be empty after this call returns. Any shared sets are also cleared.
*/
@Override
public void clear()
{
throw new UnsupportedOperationException("Not supported yet....");
}
/**
* Returns a Set view of the keys contained in this map. The set is backed
* by the map, so changes to the map are reflected in the set, and
* vice-versa. If the map is modified while an iteration over the set is in
* progress (except through the iterator's own remove operation), the
* results of the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* Iterator.remove, Set.remove, removeAll, retainAll, and clear operations.
* It does not support the add or addAll operations.
*
* @return a set view of the keys contained in this map
*/
@Override
public Set<K> keySet()
{
return this.sharedKeySet;
}
/**
* Returns a Collection view of the values contained in this map. The
* collection is backed by the map, so changes to the map are reflected in
* the collection, and vice-versa. If the map is modified while an iteration
* over the collection is in progress (except through the iterator's own
* remove operation), the results of the iteration are undefined. The
* collection supports element removal, which removes the corresponding
* mapping from the map, via the Iterator.remove, Collection.remove,
* removeAll, retainAll and clear operations. It does not support the add or
* addAll operations.
*
* @return
*/
@Override
public Collection<V> values()
{
return sharedValuesCollection;
}
/**
* Returns a Set view of the mappings contained in this map. The set is
* backed by the map, so changes to the map are reflected in the set, and
* vice-versa. If the map is modified while an iteration over the set is in
* progress (except through the iterator's own remove operation, or through
* the setValue operation on a map entry returned by the iterator) the
* results of the iteration are undefined. The set supports element removal,
* which removes the corresponding mapping from the map, via the
* Iterator.remove, Set.remove, removeAll, retainAll and clear operations.
* It does not support the add or addAll operations.
*
*
* @return a set view of the mappings contained in this map
*/
@Override
public Set<Entry<K, V>> entrySet()
{
return this.sharedEntrySet;
}
@Override
public String toString()
{
String s = "[ ";
for (int i = 0; i < capacity; ++i)
{
s += map.get(i).toString() + "\n";
}
s += " ]";
return s;
}
/**
* Created a deep copy of the MapASDV
*
* @return the deep copy of the map
*/
@Override
public Object clone()
{
/*
MapASDV<K, V> clonedMap = new MapASDV<K, V>();
//clonedMap.putAll(this);
for (ListASDV< EntryASDV<K, V>> list : this.map)
{
ListASDV< EntryASDV<K, V>> l = (ListASDV< EntryASDV<K, V>>) list.clone();
clonedMap.map.add(l);
}
return clonedMap;
*/
MapASDV<K, V> clonedMap = new MapASDV<K, V>();
clonedMap.putAll(this);
return clonedMap;
}
public static void main(String[] args)
{
MapASDV<String, Integer> map = new MapASDV();
System.out.println("---------------------------testing put(K, V)");
map.put("ann", 20);
map.put("coco", 25);
System.out.println(map);
mapASDV.MapASDV<String, Integer> clonedMap = ( mapASDV.MapASDV<String, Integer>) map.clone();
System.out.println("\n\n---------------------------testing double-the-size-and-rehash by reaching 0.75 load factor with another put(K, V)");
map.put("Jonathan", 30);
System.out.println(map);
map.put("Jonhathan", 45);
System.out.println(map);
map.put("Alexander", 33);
System.out.println(map);
System.out.println("\n\n---------------------------testing putAll(Map<K,V>)");
Map<String, Integer> anotherJavaMap = new HashMap();
anotherJavaMap.put("lion king", 45);
anotherJavaMap.put("HYENA", 6);
map.putAll(anotherJavaMap);
System.out.println(map);
System.out.println("\n\n---------------------------testing containsKey");
System.out.println(map.containsKey("Alexander"));
System.out.println(map.containsKey("alexander"));
System.out.println("\n\n---------------------------testing containsValue");
System.out.println(map.containsValue(33));
System.out.println(map.containsValue(34));
System.out.println("\n\n---------------------------testing getEntryForKey");
Entry<String, Integer> e = map.getEntryForKey("Alexander");
System.out.println( map.getEntryForKey("Alexander"));
System.out.println( map.getEntryForKey("Alex"));
System.out.println("\n\n---------------------------testing get");
System.out.println(map.get( "Alexander") );
System.out.println(map.get( "Alex") );
try{ map.get( null);}catch (NullPointerException ex ){System.out.println(ex.getMessage());}
System.out.println("\n\n---------------------------testing getIndexForKey");
System.out.println(map.getIndexForKey("Alexander"));
System.out.println(map.getIndexForKey("Alex"));
System.out.println("\n\n---------------------------testing isEmpty");
System.out.println( map.isEmpty());
System.out.println("\n\n---------------------------testing size");
System.out.println( map.size());
System.out.println( map);
System.out.println("\n\n---------------------------testing entrySet()");
Set<Entry<String, Integer>> entries = map.entrySet();
System.out.println( entries);
System.out.println("\n\n---------------------------testing keySet()");
Set<String> keys = map.keySet();
System.out.println( keys );
System.out.println("\n\n---------------------------testing values()");
Collection<Integer> values = map.values();
System.out.println( values);
System.out.println("\n\n---------------------------testing remove( K) coco 25");
map.remove("coco");
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing Entry-Collection remove ");
entries.remove( e);
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing Set Keys remove ");
keys.remove( "ann");
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing Set Values remove ");
values.remove( 45);
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println(values);
System.out.println("\n\n---------------------------testing clear ");
map.clear();
System.out.println(map);
System.out.println(entries);
System.out.println(keys);
System.out.println("\n\n---------------------------testing add of sets and collections ");
try{ keys.add( "a");}catch (Exception ex ){System.out.println(ex.getMessage());}
try{ values.add( 33);}catch (Exception ex ){System.out.println(ex.getMessage());}
try{ entries.add( e);}catch (Exception ex ){System.out.println(ex.getMessage());}
System.out.println("\n\n---------------------------testing clone");
System.out.println( clonedMap);
System.out.println("---------------------------testing put(K, V) AGAIN");
map.put("Nicholas", 100);
map.put("a", 200);
map.put("b", -20);
System.out.println( map);
}
}

View File

@@ -0,0 +1,114 @@
/*
* 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.chloe.mp1_manytomany_chloefontenot;
/**
*
* @author chloe
*/
import java.util.List;
import java.util.Set;
/**
*
* @author A. V. Markou
* @param <Many1>
* @param <Many2>
*/
public interface ManyToMany<Many1, Many2>
{
/**
* 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.
* @return the previous value associated with Many1, or null if there was no
* Many1 with the same "key".
* @throws ClassCastException - if the class of the specified Many1, or
* Many2 or value prevents it from being stored in this map( i,e Many1 is
* String and you pass an object)
* @throws NullPointerException - if the specified Many1 or Many2 is null
* and ManyToMany does not permit nulls as values.
* @throw IllegalArgumentException - if a duplicate exists in childrenRight
* list ( s1 --> p1,p2,p2 is not allowed).
* @return the previous value associated with parentLeft, or null if there
* was no childrenRight for parentLeft.
*/
List<Many2> add(Many1 parentLeft, Many2... childrenRight);
/**
* Returns the List of all left children of the parentRight.
*
* @param parentRight a parent at the RHS of the many to many relationship.
* @return the List of all left children of the parentRight.
* @throw IllegalArgumentException if the value of parameter parentRight
* does not exist in the RHS of the many to many relationship.
*/
List<Many1> getChildrenLeft(Many2 parentRight);
/**
* Returns the List of all right children of the parentLeft.
*
* @param parentLeft a parent at the LHS of the many to many relationship.
* @return the List of all right children of the parentLeft.
* @throws IllegalArgumentException if the value of parameter parentLeft
* does not exist on the LHS of the many to many relationship.
*/
List<Many2> getChildrenRight(Many1 parentLeft);
/**
* Returns a set of the Many1 elements that exist on the LHS of the many to
* many relationship.
*
* @return Set of Many1
*/
Set<Many1> getParentsLeft();
/**
* Returns a set of the Many2 elements that exist on the RHS of the many to
* many relationship.
*
* @return Set of Many2
*/
Set<Many2> getParentsRight();
/**
* Removes the many1 parameter from the LHS of the many relationship AND all
* its corresponding values that exist in the RHS of the many to many
* relationship. For example given: ( LHS e1: p1, p2 e2: p2, p3 RHS: p1: e1
* p2: e1, e2 p3: e2 after removing e1 from the LHS will results into: ( LHS
* e2: p2, p3 RHS: p2: e2 p3: e2
*
* @param many1 the unique element on the LHS to be removed.
* @throws NullPointerException if parameter many1 is null.
* @return true if the removal occurred, false if many1 does not exist in
* the LHS of the many to many relationship.
*/
boolean removeLeft(Many1 many1);
/**
* Removes the many1 parameter from the RHS of the many relationship AND all
* its corresponding values that exist in the LHS of the many to many
* relationship. For example given: LHS e2: p2, p3 RHS: p2: e2 p3: e2 after
* removing p2 from the RHS will results into: LHS e2: p3 RHS p3: e2
*
*
* @param many2 the unique element on the LHS to be removed.
* @throws NullPointerException if parameter many1 is null.
* @return true if the removal occurred, false if many1 does not exist in
* the LHS of the many to many relationship.
*/
boolean removeRight(Many2 many2);
/**
* Clears all.
*
*/
void clear();
}

View File

@@ -0,0 +1,233 @@
/*
* 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.chloe.mp1_manytomany_chloefontenot;
/**
*
* @author chloe
*/
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
*
* @author asdv5
*/
public class ManyToManyFactory {
public static <Many1, Many2> //generic types to be used in the method
ManyToMany< Many1, Many2>//return type
createManyToMany() {
return new ManyToMany<Many1, Many2>() {
private HashSet<Many1> parents = new HashSet<>();
private Map<Object, Object> left = new HashMap<>();
private Map<Object, Object> right = new HashMap<>();
@Override
public String toString() {
return "{" + "left=" + left + ", right=" + right + '}';
}
/**
* 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.
* @return the previous value associated with Many1, or null if there was no Many1 with the same "key".
* @throws ClassCastException - if the class of the specified Many1, or Many2 or value prevents it from being stored in this map( i,e Many1 is String and you pass an object)
* @throws NullPointerException - if the specified Many1 or Many2 is null and ManyToMany does not permit nulls as values.
* @throw IllegalArgumentException - if a duplicate exists in childrenRight list ( s1 --> p1,p2,p2 is not allowed).
* @return the previous value associated with parentLeft, or null if there was no childrenRight for parentLeft.
*/
@Override
public List<Many2> add(Many1 parentLeft, Many2... childrenRight) {
HashSet<Many2> verifyUnique = new HashSet<>();
List<Many2> returnList = new ArrayList<>();
// Check for exceptions
if (!childrenRight.equals(parentLeft.getClass())) {
//throw new ClassCastException();
}
if (parentLeft == null || childrenRight == null) {
throw new NullPointerException();
}
// Try to add all elements from childrenRight into HashSet. If we're unable to, through an IllegalArgumentException, assuming HashSet doesn't throw one already.
verifyUnique.addAll(Arrays.asList(childrenRight));
if (left.size() > 0 && childrenRight.length > 0) {
for (Object e : left.values()) {
returnList.add((Many2) e);
}
//returnList.addAll((<? extends Many2>) left.values());
}
// Keep track of the parents so we can manipulate the RHS
parents.add(parentLeft);
// Handle LHS
//for (Many2 e : childrenRight) {
left.put(parentLeft, new ArrayList<Many2>(Arrays.asList(childrenRight)));
//}
//Handle RHS
/*
for (Many1 e: parents) {
right.put(, e );
}
*/
if (returnList.size() == 0) {
return null;
}
return returnList;
}
/**
* Returns the List of all left children of the parentRight.
*
* @param parentRight a parent at the RHS of the many to many relationship.
* @return the List of all left children of the parentRight.
* @throw IllegalArgumentException if the value of parameter parentRight does not exist in the RHS of the many to many relationship.
*/
@Override
public List<Many1> getChildrenLeft(Many2 parentRight) {
List<Many1> returnList = new ArrayList<Many1>();
for (Entry<Object, Object> ee : left.entrySet()) {
System.out.println(ee.getKey() + ", " + parentRight);
if (ee.getKey().equals(parentRight)) {
returnList.add((Many1) ee.getValue());
}
}
if (returnList.size() > 0) {
return null;
}
return returnList;
}
/**
* Returns the List of all right children of the parentLeft.
*
* @param parentLeft a parent at the LHS of the many to many relationship.
* @return the List of all right children of the parentLeft.
* @throws IllegalArgumentException if the value of parameter parentLeft does not exist on the LHS of the many to many relationship.
*/
@Override
public List<Many2> getChildrenRight(Many1 parentLeft) {
List<Many2> returnList = new ArrayList<>();
for (Entry<Object, Object> ee : right.entrySet()) {
if (ee.getKey() == parentLeft) {
returnList.add((Many2) ee.getValue());
}
}
if (returnList.size() > 0) {
return null;
}
return returnList;
}
/**
* Returns a set of the Many1 elements that exist on the LHS of the many to many relationship.
*
* @return Set of Many1
*/
@Override
public Set<Many1> getParentsLeft() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public Set<Many2> getParentsRight() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public boolean removeLeft(Many1 many1) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public boolean removeRight(Many2 many2) {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
@Override
public void clear() {
throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
}
};
}
public static void main(String[] args) throws InterruptedException {
ManyToMany<String, String> mm = ManyToManyFactory.createManyToMany();
//mm.add(1, 1);///will not compile, we have Many1, Many2 as String
System.out.println("add(e1, p1, p2)returns: " + mm.add("e1", "p1", "p2"));
System.out.println("add(e2, p2, p3)returns: " + mm.add("e2", "p2", "p3"));
//System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
//System.out.println("getParentsRight returns: " + mm.getParentsRight());
System.out.println("getChildrenLeft(p1) returns: " + mm.getChildrenLeft("p2"));
System.out.println("getChildrenLeft(p2) returns: " + mm.getChildrenLeft("p3"));
System.out.println("getChildrenRight(e1) returns: " + mm.getChildrenRight("e1"));
System.out.println("getChildrenRight(e1) returns: " + mm.getChildrenRight("e2"));
System.out.println("-----------------------------------------------");
System.out.println("The internal hash maps of ManyToMany after insertions: " + mm);
System.out.println("----------AFTER REMOVAL of e1 LEFT----------------------------------------");
System.out.println("removeLeft(e1) returns: " + mm.removeLeft("e1"));
System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
System.out.println("getParentsRight returns: " + mm.getParentsRight());
System.out.println("getChildrenRight(e2) returns: " + mm.getChildrenRight("e2"));
System.out.println("getChildrenLeft(p2) returns: " + mm.getChildrenLeft("p2"));
System.out.println("getChildrenLeft(p3) returns: " + mm.getChildrenLeft("p3"));
System.out.println("The internal hash maps of ManyToMany after removal LEFT: " + mm);
System.out.println("\n----------AFTER REMOVAL of p2 RIGHT----------------------------------------");
System.out.println("removeLeft(p2) returns: " + mm.removeRight("p2"));
System.out.println("getParentsLeft returns: " + mm.getParentsLeft());
System.out.println("getParentsRight returns: " + mm.getParentsRight());
System.out.println("getChildrenRight(e2) returns: " + mm.getChildrenRight("e2"));
System.out.println("getChildrenLeft(p3) returns: " + mm.getChildrenLeft("p3"));
System.out.println("--------------------------------------------------");
System.out.println("mm.removeLeft(e1) returns: " + mm.removeLeft("e1"));
System.out.println("The internal hash maps of ManyToMany after removal RIGHT: " + mm);
Thread.sleep(5000, 0);
System.out.println("---------------CATCHING EXCEPTIONS -----------------------------------");
try {
mm.getChildrenRight("e10");//e10 dos not exist
} catch (RuntimeException e) {
System.err.println("getChildrenRight for e10 throws exception:" + e);
}
try {
System.out.println(mm.add("e6", new String[]{
"value1", null
}));
} catch (RuntimeException e) {
System.err.println("add(e6, new String[]{null } ) throws exception: " + e);
}
try {
System.out.println(mm.add(null, "p1"));
} catch (RuntimeException e) {
System.err.println("add(null, p1)returns throws exception: " + e);
}
try {
System.out.println(mm.getChildrenLeft("p1"));
} catch (RuntimeException e) {
System.err.println("getChildrenRight(p1) throws exception: " + e);
}
try {
mm.add((String) new Object(), (String) new Object());
} catch (RuntimeException e) {
System.err.println("add((String) new Object(), (String) new Object()) throws exception: " + e);
}
mm.clear();
System.out.println("-----------------------------------------------");
System.out.println("The internal hash maps of ManyToMany after clear: " + mm);
}
}

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.chloefontenot.mp2_chloefontenot;
/**
*
* @author chloe
*/
public class MP2_ChloeFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,142 @@
/*
* 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 com.chloefontenot.mp2_chloefontenot;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveTask;
/**
*
* @author chloe
*/
public class Matrices {
public static void main(String[] args) {
Matrices.multiplyParallel();
}
public static ArrayList<ArrayList<BigInteger>> createRandomMatrix(int rows, int columns) {
ArrayList<ArrayList<BigInteger>> matrix = new ArrayList<ArrayList<BigInteger>>(rows);
for (int i = 0; i < rows; ++i) {
ArrayList<BigInteger> row = new ArrayList<BigInteger>();
for (int j = 0; j < columns; ++j) {
row.add(new BigInteger(Integer.toString(1 + (int) (Math.random() * 9))));
}
matrix.add(row);
}
return matrix;
}
public static void multiplyParallel() {
/*
ArrayList<ArrayList<BigInteger>> A = new ArrayList<>();
ArrayList<BigInteger> row1 = new ArrayList<>();
row1.add(new BigInteger("6"));
row1.add(new BigInteger("1"));
ArrayList<BigInteger> row2 = new ArrayList<>();
row2.add(new BigInteger("7"));
row2.add(new BigInteger("2"));
A.add(row1);
A.add(row2);
ArrayList<ArrayList<BigInteger>> B = new ArrayList<>();
row1 = new ArrayList<BigInteger>();
row1.add(new BigInteger("4"));
row1.add(new BigInteger("1"));
row1.add(new BigInteger("2"));
row2 = new ArrayList<BigInteger>();
row2.add(new BigInteger("1"));
row2.add(new BigInteger("9"));
row2.add(new BigInteger("5"));
B.add(row1);
B.add(row2);
*/
ArrayList<ArrayList<BigInteger>> A = Matrices.createRandomMatrix(2, 2);
ArrayList<ArrayList<BigInteger>> B = Matrices.createRandomMatrix(2, 3);
RecursiveTask<ArrayList<ArrayList<BigInteger>>> rt
= new Matrices.MatricesMultiplication(0, A.size() - 1, A, B);
ForkJoinPool pool = new ForkJoinPool();
ArrayList<ArrayList<BigInteger>> mul = pool.invoke(rt);
System.out.println("MATRIX A");
printMatrix(A);
System.out.println("\nMATRIX B");
printMatrix(B);
System.out.println("\nMATRIX AxB");
printMatrix(mul);
}
private static void printMatrix(ArrayList<ArrayList<BigInteger>> matrix) {
for (int i = 0; i < matrix.size(); ++i) {
System.out.println(matrix.get(i));
}
}
static class MatricesMultiplication extends RecursiveTask<ArrayList<ArrayList<BigInteger>>> {
ArrayList<ArrayList<BigInteger>> A;
ArrayList<ArrayList<BigInteger>> B;
ArrayList<ArrayList<BigInteger>> AxB;
final int HOW_MANY_ROWS_IN_PARALLEL = 3;// threshold
int startIndex;
int endIndex;
public MatricesMultiplication(int startIndex, int endIndex, ArrayList<ArrayList<BigInteger>> A, ArrayList<ArrayList<BigInteger>> B) {
this.startIndex = startIndex;
this.endIndex = endIndex;
this.A = A;
this.B = B;
}
@Override
protected ArrayList<ArrayList<BigInteger>> compute() {
// Base case: if the number of rows in A is less than or equal to the threshold,
// perform matrix multiplication sequentially
if (endIndex - startIndex + 1 <= HOW_MANY_ROWS_IN_PARALLEL) {
ArrayList<ArrayList<BigInteger>> result = new ArrayList<>();
for (int i = startIndex; i <= endIndex; i++) {
ArrayList<BigInteger> rowResult = new ArrayList<>();
for (int j = 0; j < B.get(0).size(); j++) {
BigInteger sum = BigInteger.ZERO;
for (int k = 0; k < A.get(0).size(); k++) {
sum = sum.add(A.get(i).get(k).multiply(B.get(k).get(j)));
}
rowResult.add(sum);
}
result.add(rowResult);
}
return result;
} else {
// Split the task into smaller subtasks
int middle = (startIndex + endIndex) / 2;
MatricesMultiplication leftTask = new MatricesMultiplication(startIndex, middle, A, B);
MatricesMultiplication rightTask = new MatricesMultiplication(middle + 1, endIndex, A, B);
// Fork the subtasks
leftTask.fork();
ArrayList<ArrayList<BigInteger>> rightResult = rightTask.compute();
// Join the results of the subtasks
ArrayList<ArrayList<BigInteger>> leftResult = leftTask.join();
// Merge the results
ArrayList<ArrayList<BigInteger>> result = new ArrayList<>();
for (int i = 0; i < leftResult.size(); i++) {
result.add(new ArrayList<>(leftResult.get(i)));
}
for (int i = 0; i < rightResult.size(); i++) {
result.add(new ArrayList<>(rightResult.get(i)));
}
return result;
}
}
}
}

View File

@@ -0,0 +1,86 @@
/*
* 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.chloe.multithreading_chloefontenot;
/**
*
* @author chloe
*/
import javafx.application.Application;
import javafx.application.Platform;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
public class FlashText extends Application
{
private String text = "";
@Override // Override the start method in the Application class
public void start(Stage primaryStage)
{
StackPane pane = new StackPane();
Label lblText = new Label("Programming is fun");
pane.getChildren().add(lblText);
Thread t1 = new Thread(
new Runnable()
{
public void run()
{
try
{
while (true)
{
if (lblText.getText().trim().length() == 0)
text = "Welcome";
else
text = "";
Platform.runLater(
new Runnable()
{
@Override
public void run()
{
lblText.setText(text);
}
});
/*
Thread t2 =new Thread( new Runnable() {
@Override
public void run()
{
lblText.setText(text);
}
});
*/
Thread.sleep(300);
}
}
catch (InterruptedException ex)
{
}
}
});
t1.start();
// Create a scene and place it in the stage
Scene scene = new Scene(pane, 200, 50);
primaryStage.setTitle("FlashText"); // Set the stage title
primaryStage.setScene(scene); // Place the scene in the stage
primaryStage.show(); // Display the stage
}
public static void main(String[] args)
{
launch(args);
}
}

View File

@@ -0,0 +1,59 @@
/*
* 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.chloe.multithreading_chloefontenot;
/**
*
* @author chloe
*/
public class PrintChar implements Runnable{
private char charToPrint;
private int times;
public PrintChar(char charToPrint, int times)
{
this.charToPrint = charToPrint;
this.times = times;
}
@Override
public void run()
{
for (int i = 0; i < times; ++i) {
System.out.println(charToPrint);
}
}
public static void main(String[] args)
{
for (int i = 0; i < 1000; ++i) {
System.out.println("lmao");
}
}
}
class PrintNum implements Runnable {
private int lastNum;
public PrintNum(int lastNum)
{
this.lastNum = lastNum;
}
public void run()
{
for (int i = 1; i <= lastNum; ++i) {
System.out.print(" " + i);
Thread.yield();
if (i % 10 == 0) {
System.out.println();
}
}
}
}

View File

@@ -0,0 +1,15 @@
/*
* 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.chloe.multithreading_chloefontenot;
/**
*
* @author chloe
*/
public class RunFlash {
public static void main(String[] args) {
FlashText.main(args);
}
}

View File

@@ -0,0 +1,29 @@
/*
* 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.chloe.multithreading_chloefontenot;
/**
*
* @author chloe
*/
public class Threads {
public static void main(String[] args)
{
// Create tasks
Runnable printA = new PrintChar('a', 100);
Runnable printB = new PrintChar('b', 100);
Runnable print100 = new PrintNum(100);
// Create threads
Thread thread1 = new Thread(printA);
Thread thread2 = new Thread(printB);
Thread thread3 = new Thread(print100);
// Start threads
thread1.start();
thread2.start();
thread3.start();
}
}

View File

@@ -0,0 +1,20 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.chloe.multithreading_chloefontenot;
import java.io.IOException;
/**
*
* @author chloe
*/
public class forkbomb
{
public static void main(String[] args) throws IOException
{
Runtime.getRuntime().exec(new String[]{"java", "-cp", System.getProperty("java.class.path"), "forkbomb"});
}
}

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.chloe.projecttrees_chloefontenot;
/**
*
* @author chloe
*/
public class ProjectTrees_ChloeFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

View File

@@ -0,0 +1,374 @@
/*
* 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.chloe.projecttrees_chloefontenot;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Queue;
import java.util.Stack;
/**
*
* @author chloe
* @param <T>
*/
public class TreeASDV<T extends Comparable> implements Cloneable {
private Node<T> root;
class Node<T> {
T data;
Node<T> leftChild;
Node<T> rightChild;
}
@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;
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 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> 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;
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;
}
// Inorder ListIterator
public Iterator<T> listIterator()
{
return new InorderIterator();
}
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);
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);
}
}
}
@Override
public boolean equals(Object obj) {
return this.getBreadthFirstArray().equals(((TreeASDV) obj).getBreadthFirstArray());
}
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) throws CloneNotSupportedException
{
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();
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();
}
}

View File

@@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.chloe.threads_chloefontenot;
/**
*
* @author chloe
*/
public class Threads_ChloeFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}