Created
March 11, 2020 18:27
-
-
Save feehe21/baf6e8966a36d108bfa6b88d92bb47ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class ActivitiesProblem | |
{ | |
public ActivitiesProblem(){ | |
int[] start = {1, 3, 0, 5, 8, 5}; | |
int[] finish = {2, 4, 6, 7, 9, 9}; | |
boolean more = true; | |
int next = -1; | |
int endTime = 0; | |
ArrayList<Integer> a = new ArrayList<Integer>(); | |
while(more){ | |
next = findLowest(start, endTime); | |
if(next == -1){ | |
more = false; | |
System.out.println(a); | |
}else{ | |
a.add(next); | |
endTime = finish[next]; | |
} | |
} | |
} | |
public int findLowest(int[] finishTimes, int min){//returns pos of first lowest | |
int pos = -1; | |
int val = Integer.MAX_VALUE; | |
for(int i = 0; i < finishTimes.length; i++){ | |
if(finishTimes[i] >= min && finishTimes[i] < val){ | |
val = finishTimes[i]; | |
pos = i; | |
return pos; | |
} | |
} | |
return pos; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class BinaryTree | |
{ | |
// Root of Binary Tree | |
treeNode root; | |
// Constructors | |
BinaryTree(int key) | |
{ | |
root = new treeNode(key); | |
} | |
BinaryTree() | |
{ | |
root = null; | |
} | |
public void insert(treeNode node, int value) { | |
if (value < node.data) { | |
if (node.left != null) { | |
insert(node.left, value); | |
} else { | |
System.out.println(" Inserted " + value + " to left of Node " + node.data); | |
node.left = new treeNode(value); | |
} | |
} else if (value > node.data) { | |
if (node.right != null) { | |
insert(node.right, value); | |
} else { | |
System.out.println(" Inserted " + value + " to right of Node " + node.data); | |
node.right = new treeNode(value); | |
} | |
} | |
} | |
public void showValues(treeNode node){ | |
if(node != null){ | |
System.out.println(node.data); | |
if(node.left != null){ | |
System.out.print("left: "); | |
showValues(node.left); | |
} | |
if(node.right != null){ | |
System.out.print("right: "); | |
showValues(node.right); | |
} | |
}else{ | |
System.out.println("null"); | |
} | |
} | |
public boolean search(int value){ | |
treeNode node = root; | |
while(node.left!=null || node.right!=null){ | |
if(node.data == value){ | |
return true; | |
} | |
if(value < node.data){ | |
if(node.left ==null){ | |
return false; | |
}else{ | |
node = node.left; | |
} | |
}else{ | |
if(node.right ==null){ | |
return false; | |
}else{ | |
node = node.right; | |
} | |
} | |
} | |
return false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
public class coinProblem | |
{ | |
treeRunner runner; | |
ArrayList<Integer> list; | |
treeNode last; | |
int worked; | |
public coinProblem(int value){ | |
int[] coins = {1,2,5,10,20,50,100,500,1000}; | |
list = new ArrayList<Integer>(); | |
runner = new treeRunner(coins); | |
worked = -1; | |
int hold = value; | |
int holder = 0; | |
while(hold > 0){ | |
worked = -1; | |
holder = findLargestSmallCoin(hold, runner.tree.root); | |
if(holder != -1){ | |
list.add(holder); | |
hold -= holder; | |
}else{ | |
System.out.println("wrong"); | |
} | |
} | |
System.out.println(list); | |
} | |
/* | |
20 | |
5 100 | |
2 10 50 500 | |
1 1000 | |
*/ | |
public int findLargestSmallCoin(int value, treeNode node){ | |
if(node == null){ | |
node = runner.tree.root; | |
} | |
if(node.data == value){ | |
return node.data; | |
} | |
if(node.data > value){ | |
if(node.left !=null){ | |
return findLargestSmallCoin(value, node.left); | |
}else{ | |
return worked; | |
} | |
}else{ | |
if(node.data > worked){ | |
worked = node.data; | |
} | |
if(node.right !=null){ | |
return findLargestSmallCoin(value, node.right); | |
}else{ | |
return worked; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class treeNode | |
{ | |
int data; | |
treeNode left; | |
treeNode right; | |
treeNode(int d){ | |
data = d; | |
left = null; | |
right = null; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class treeRunner | |
{ | |
BinaryTree tree; | |
treeNode root; | |
int midway; | |
/*public static void main(String[] args) { | |
BinaryTree tree = new BinaryTree(); | |
treeNode root = null; | |
root = tree.insert(root, 4); | |
tree.root = root; | |
tree.insert(root, 2); | |
tree.insert(root, 1); | |
tree.insert(root, 3); | |
tree.insert(root, 6); | |
tree.insert(root, 5); | |
tree.showValues(tree.root); | |
}*/ | |
public treeRunner(int[] a){ | |
tree = new BinaryTree(); | |
root = null; | |
for(int i = 0; i < a.length; i++){ | |
System.out.print(a[i]);} | |
a = sort(a); | |
for(int i = 0; i < a.length; i++){ | |
System.out.print(a[i]);} | |
//root = tree.insert(root, a[a.length/2]); | |
//tree.root = root; | |
//merge(a, 0, a.length-1); | |
//tree.showValues(tree.root); | |
//System.out.println(tree.search(0)); | |
midway = (a.length-1)/2; | |
treeNode root = new treeNode(a[midway]); | |
tree.root = root; | |
in(a, 0, a.length-1, root); | |
tree.showValues(tree.root); | |
} | |
public void merge(int[]a, int start, int end){ | |
int mid = (start+end)/2; | |
if(end-start > 1){ | |
if(mid!=a.length/2){ | |
tree.insert(root, a[mid]); | |
} | |
merge(a, start, mid-1); | |
merge(a, mid+1, end); | |
}else if(start == end){ | |
if(start!=a.length/2){ | |
tree.insert(root, a[start]); | |
} | |
}else{ | |
if(start!=a.length/2){ | |
tree.insert(root, a[start]); | |
} | |
if(end!=a.length/2){ | |
tree.insert(root, a[end]); | |
} | |
} | |
} | |
public void in(int[] a, int start, int end, treeNode node){ | |
int m = start + (end-start)/2; | |
if(m!=midway){ | |
//System.out.println("Insert"); | |
tree.insert(node, a[m]); | |
} | |
if(start<end){ | |
in(a, m+1, end, node); | |
in(a, start, m, node); | |
} | |
} | |
public int[] sort(int[]a){ | |
int spot = 0; | |
int hold = 0; | |
for(int i = 0; i < a.length-1; i++){//spot to swap to | |
spot = i; | |
for(int x = i; x < a.length; x++){//lowest | |
if(a[x] < a[spot]){ | |
spot = x; | |
} | |
} | |
hold = a[spot]; | |
a[spot] = a[i]; | |
a[i] = hold; | |
} | |
for(int i = 0; i < a.length; i++){ | |
//System.out.print(a[i]+" "); | |
} | |
return a; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment