Markou is absolutely insane
This commit is contained in:
14
Semester 4/Assignments/ProjectTrees_CalebFontenot/pom.xml
Normal file
14
Semester 4/Assignments/ProjectTrees_CalebFontenot/pom.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>edu.slcc.asdv.caleb</groupId>
|
||||
<artifactId>ProjectTrees_CalebFontenot</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>20</maven.compiler.target>
|
||||
<exec.mainClass>edu.slcc.asdv.caleb.projecttrees_calebfontenot.ProjectTrees_CalebFontenot</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@@ -0,0 +1,16 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package edu.slcc.asdv.caleb.projecttrees_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class ProjectTrees_CalebFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
@@ -0,0 +1,184 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
|
||||
*/
|
||||
package edu.slcc.asdv.caleb.projecttrees_calebfontenot;
|
||||
|
||||
import java.util.ListIterator;
|
||||
import java.util.Stack;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
public class TreeASDV<T extends Comparable> {
|
||||
|
||||
private Node<T> root;
|
||||
|
||||
class Node<T> {
|
||||
|
||||
T data;
|
||||
Node<T> leftChild;
|
||||
Node<T> rightChild;
|
||||
}
|
||||
|
||||
public boolean insert(T t)
|
||||
{
|
||||
Node<T> currentNode = root;
|
||||
Node<T> trailCurrent = root;
|
||||
Node<T> newNode = new Node<T>();
|
||||
newNode.data = t;
|
||||
System.out.println(t);
|
||||
|
||||
if (this.root == null) {
|
||||
this.root = newNode;
|
||||
return true;
|
||||
}
|
||||
|
||||
while (currentNode != null) {
|
||||
trailCurrent = currentNode;
|
||||
if (t.compareTo(currentNode.data) >= 0) {
|
||||
currentNode = currentNode.rightChild;
|
||||
} else {
|
||||
currentNode = currentNode.leftChild;
|
||||
}
|
||||
if (t.compareTo(trailCurrent.data) >= 0) { // Make the node a right child.
|
||||
trailCurrent.rightChild = newNode;
|
||||
} else { // Make the node a left child.
|
||||
trailCurrent.leftChild = newNode;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private void inOrder(Node<T> p)
|
||||
{
|
||||
if (p == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
inOrder(p.leftChild);
|
||||
System.out.print(p.data + " ");
|
||||
inOrder(p.rightChild);
|
||||
}
|
||||
|
||||
public void inOrder()
|
||||
{
|
||||
inOrder(this.root);
|
||||
}
|
||||
|
||||
public Node<T> findNode(T t)
|
||||
{
|
||||
Node<T> currentNode = root;
|
||||
while (currentNode != null) {
|
||||
if (t.compareTo(currentNode.data) == 0) {
|
||||
return currentNode;
|
||||
} else if (t.compareTo(currentNode.data) > 0) {
|
||||
currentNode = currentNode.rightChild;
|
||||
} else {
|
||||
currentNode = currentNode.leftChild;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public boolean remove(T t)
|
||||
{
|
||||
// case: no children
|
||||
if (root.leftChild == null & root.rightChild == null) {
|
||||
root = null;
|
||||
return true;
|
||||
}
|
||||
Node<T> current = this.root;
|
||||
Node<T> currentParent = this.root;
|
||||
|
||||
while (current.data.equals(t)) {
|
||||
if (t.compareTo(current.data) == 0) {
|
||||
break;
|
||||
}
|
||||
else if (t.compareTo(currentParent.data) > 0) {
|
||||
currentParent = current;
|
||||
current = current.rightChild;
|
||||
} else {
|
||||
currentParent = current;
|
||||
current = current.leftChild;
|
||||
}
|
||||
}
|
||||
if (current == null) {
|
||||
return false;
|
||||
}
|
||||
if (current.data.compareTo(currentParent.data) <= 0) {
|
||||
currentParent.leftChild = null;
|
||||
} else {
|
||||
currentParent.rightChild = null;
|
||||
}
|
||||
// case: single child
|
||||
if (current.leftChild == null || current.rightChild == null) {
|
||||
// if the current is the right child of its parent
|
||||
if (current.data.compareTo(t) > 0) {
|
||||
|
||||
} else {
|
||||
|
||||
}
|
||||
}
|
||||
// case: two children
|
||||
else {
|
||||
// take a left
|
||||
Node<T> p = current;
|
||||
p = current.leftChild;
|
||||
Node<T> pTrailParent = p;
|
||||
Node<T> pTrail = p;
|
||||
while (p != null) { // keep going right
|
||||
pTrailParent = pTrail;
|
||||
pTrail = p;
|
||||
p = p.rightChild;
|
||||
}
|
||||
// swap the data of pTrail with the current
|
||||
current.data = pTrail.data;
|
||||
if (pTrail == pTrailParent) {
|
||||
pTrailParent.leftChild = pTrail.leftChild;
|
||||
} else {
|
||||
pTrailParent.rightChild = pTrail.leftChild;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public ListIterator<T> listIterator(){
|
||||
ListIterator it = new ListIterator<T>();
|
||||
return null;
|
||||
}
|
||||
public void breathFirst() {
|
||||
Stack<Node<T>> stack = new Stack();
|
||||
Node<T> p = root;
|
||||
System.out.println(p.data + " ");
|
||||
stack.push(p.rightChild);
|
||||
|
||||
while (stack.empty() == false) {
|
||||
// go to the left
|
||||
p = p.leftChild;
|
||||
if (p != null) {
|
||||
System.out.println(p.data);
|
||||
}
|
||||
Node<T> rightChild = stack.pop();
|
||||
System.out.println();
|
||||
}
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
TreeASDV tree = new TreeASDV();
|
||||
tree.insert(100);
|
||||
tree.insert(80);
|
||||
tree.insert(90);
|
||||
tree.insert(95);
|
||||
tree.insert(93);
|
||||
tree.insert(70);
|
||||
tree.inOrder();
|
||||
tree.remove(100);
|
||||
System.out.println();
|
||||
tree.inOrder();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user