Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] Challenge 11 and 12 (Unreviewed) #381

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
205 changes: 205 additions & 0 deletions challenge_11/java/zmiller91/BST.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,205 @@
import java.lang.Integer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
* @author zmiller
*/
class Node {
public int data;
public Node left;
public Node right;
public Node parent;

public Node(int data) {
this.data = data;
}
}

class BST extends YOP {

/**
* Inserts a value into a BST.
*
* @param data - what to insert
* @param head - where to insert
* @return insert node
*/
public Node insert (int data, Node head) {

if (head == null) {
return new Node(data);
}

if (data < head.data) {
if (head.left != null) {
return insert(data, head.left);
}

head.left = new Node(data);
head.left.parent = head;
return head;
}

if (data > head.data) {
if (head.right != null) {
return insert(data, head.right);
}

head.right = new Node(data);
head.right.parent = head;
return head;
}

// Ignore duplicates
return null;
}

/**
* Removes an element from the list; `n` will remain the head
* after removal;
*
* @param data - value to delete
* @param n - head of subtree
*/
public void remove(int data, Node n) {
remove(search(data, n), n);
}

/**
* Removes an element from the list; `n` will remain the head
* after removal;
*
* @param data - value to delete
* @param n - head of subtree
* @param head - a reference to the head of the tree
*/
protected void remove(Node remove, Node n) {

if (remove == null){
return;
}

// Get the smallest on the right or largest on the left, depending on
// what children are set
Node swap = remove.right != null ? smallest(remove.right) :
remove.left != null ? largest(remove.left) : null;

// Not a leaf node, recurse
if (swap != null) {
remove.data = swap.data;
remove(swap, remove);
}

// Leaf node, unset the reference
else {
Node parent = remove.parent;
parent.left = parent.left != remove ? parent.left : null;
parent.right = parent.right != remove ? parent.right : null;
}
}

/**
* Returns the object containing the provided data point
*
* @param data - value to be deleted
* @param n - tree to look at
*/
protected Node search(int data, Node n) {

// Not found
if (n == null) {
return null;
}

// Found, return the node
if (data == n.data) {
return n;
}

// Search the next tree
Node next = data < n.data ? n.left : n.right;
return search(data, next);
}

/**
* Finds the smallest element in a BST
*
* @param n - root of the tree
* @return the smallest node in the tree
*/
private Node smallest(Node n) {

if (n != null) {
while (n.left != null) {
n = n.left;
}
}
return n;
}

/**
* Finds the largest element in a BST
*
* @param n - root of the tree
* @return the largest node in the tree
*/
private Node largest(Node n) {

if (n != null) {
while (n.right != null) {
n = n.right;
}
}

return n;
}

/**
* Prints a tree in pre-order DFS.
*
* @param head - tree to print
*/
public void print(Node head) {

if (head == null) {
return;
}

System.out.println(head.data);
print(head.left);
print(head.right);
}

/**
* Executes the problem's solution
*
* @param input
*/
@Override
protected void run(String input) {

// Create the BST
String[] split = input.split("\\|");
int remove = Integer.parseInt(split[1].trim());
Node head = null;
for (int i : csvToIntArray(split[0])) {
if (head == null) {
head = new Node(i);
continue;
}

insert(i, head);
}

print(head);
remove(remove, head);
System.out.println();
print(head);
}

public static void main(String args[]) {
launch(new BST());
}
}
20 changes: 20 additions & 0 deletions challenge_11/java/zmiller91/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Binary Search Tree

This solution contains a BST implementation that can add and remove
elements from a tree; all functions run in O(log(n)) time.

## How To Run

```
javac BST.java
java BST
```

# Input

Input is accepted in the following format:

```
csv_of_elements | element_to_delete
1,2,3,4 | 3
```
49 changes: 49 additions & 0 deletions challenge_11/java/zmiller91/YOP.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import java.lang.Integer;
import java.lang.String;
import java.util.Scanner;

/**
* @author zmiller
*/
public abstract class YOP {

protected int[] csvToIntArray(String csv) {
String[] strings = csv.split(",");
int[] ints = new int[strings.length];
for (int i = 0; i < strings.length; i++) {
ints[i] = Integer.parseInt(strings[i].trim());
}

return ints;
}

/**
* Executes the life cycle of the solution.
*
* @param yop - class to execute
*/
protected static void launch(YOP yop) {

String input;
Scanner scan = new Scanner(System.in);

System.out.println("Type 'done' to terminate.\n");
while(true) {
System.out.print("Input: ");
input = scan.nextLine();
if(input.toLowerCase().equals("done")) {
return;
}

yop.run(input);
}
}

/**
* Executes the solution
*
* @param input - command line input
*/
protected abstract void run(String input);

}
Loading