Add Initial Files
This commit is contained in:
Vendored
+7
@@ -0,0 +1,7 @@
|
||||
{
|
||||
"java.project.sourcePaths": ["src"],
|
||||
"java.project.outputPath": "bin",
|
||||
"java.project.referencedLibraries": [
|
||||
"lib/**/*.jar"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
## Getting Started
|
||||
|
||||
Welcome to the VS Code Java world. Here is a guideline to help you get started to write Java code in Visual Studio Code.
|
||||
|
||||
## Folder Structure
|
||||
|
||||
The workspace contains two folders by default, where:
|
||||
|
||||
- `src`: the folder to maintain sources
|
||||
- `lib`: the folder to maintain dependencies
|
||||
|
||||
Meanwhile, the compiled output files will be generated in the `bin` folder by default.
|
||||
|
||||
> If you want to customize the folder structure, open `.vscode/settings.json` and update the related settings there.
|
||||
|
||||
## Dependency Management
|
||||
|
||||
The `JAVA PROJECTS` view allows you to manage your dependencies. More details can be found [here](https://github.com/microsoft/vscode-java-dependency#manage-dependencies).
|
||||
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,5 @@
|
||||
public class MorseTester {
|
||||
public static void main (String args[]) {
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
public class MorseTree {
|
||||
|
||||
public static void main(String args[]) {
|
||||
// TODO: keep root in this class, transfer all other lines to tester
|
||||
// Make function to populate root? potentially. Prevents clutter in
|
||||
// Main tester
|
||||
TreeNode<String> root = new TreeNode<String>("_");
|
||||
TreeNode<String> E = new TreeNode<String>("E");
|
||||
TreeNode<String> T = new TreeNode<String>("T");
|
||||
root.setLeft(E); root.setRight(T);
|
||||
TreeNode<String> I = new TreeNode<String>("I");
|
||||
TreeNode<String> A = new TreeNode<String>("A");
|
||||
E.setLeft(I); E.setRight(A);
|
||||
TreeNode<String> S = new TreeNode<String>("S");
|
||||
TreeNode<String> U = new TreeNode<String>("U");
|
||||
I.setLeft(S); I.setRight(U);
|
||||
TreeNode<String> H = new TreeNode<String>("H");
|
||||
TreeNode<String> V = new TreeNode<String>("V");
|
||||
S.setLeft(H); S.setRight(V);
|
||||
TreeNode<String> c_5 = new TreeNode<String>("5");
|
||||
TreeNode<String> c_4 = new TreeNode<String>("4");
|
||||
H.setLeft(c_5); H.setRight(c_4);
|
||||
// leftmost column complete
|
||||
TreeNode<String> c_3 = new TreeNode<String>("3");
|
||||
V.setRight(c_3);
|
||||
// second from the left
|
||||
TreeNode<String> F = new TreeNode<String>("F");
|
||||
TreeNode<String> _Blank = new TreeNode<String>("_");
|
||||
U.setLeft(F); U.setRight(_Blank);
|
||||
TreeNode<String> c_2 = new TreeNode<String>("2");
|
||||
_Blank.setRight(c_2);
|
||||
// I column complete
|
||||
TreeNode<String> R = new TreeNode<String>("R");
|
||||
TreeNode<String> W = new TreeNode<String>("W");
|
||||
A.setLeft(R); A.setRight(W);
|
||||
TreeNode<String> L = new TreeNode<String>("L");
|
||||
R.setLeft(L);
|
||||
// left of A complete
|
||||
TreeNode<String> P = new TreeNode<String>("P");
|
||||
TreeNode<String> J = new TreeNode<String>("J");
|
||||
W.setLeft(P); W.setRight(J);
|
||||
TreeNode<String> c_1 = new TreeNode<String>("1");
|
||||
J.setRight(c_1);
|
||||
// E complete
|
||||
TreeNode<String> N = new TreeNode<String>("N");
|
||||
TreeNode<String> M = new TreeNode<String>("M");
|
||||
T.setLeft(N); T.setRight(M);
|
||||
TreeNode<String> D = new TreeNode<String>("D");
|
||||
TreeNode<String> K = new TreeNode<String>("K");
|
||||
N.setLeft(D); N.setRight(K);
|
||||
TreeNode<String> B = new TreeNode<String>("B");
|
||||
TreeNode<String> X = new TreeNode<String>("X");
|
||||
D.setLeft(B); D.setRight(X);
|
||||
TreeNode<String> c_6 = new TreeNode<String>("6");
|
||||
B.setLeft(c_6);
|
||||
// end of first column on right side
|
||||
TreeNode<String> C = new TreeNode<String>("C");
|
||||
TreeNode<String> Y = new TreeNode<String>("Y");
|
||||
K.setLeft(C); K.setRight(Y);
|
||||
// send second column on right side
|
||||
TreeNode<String> G = new TreeNode<String>("G");
|
||||
TreeNode<String> O = new TreeNode<String>("O");
|
||||
M.setLeft(G); M.setRight(O);
|
||||
TreeNode<String> Z = new TreeNode<String>("Z");
|
||||
TreeNode<String> Q = new TreeNode<String>("Q");
|
||||
G.setLeft(Z); G.setRight(Q);
|
||||
TreeNode<String> c_7 = new TreeNode<String>("7");
|
||||
Z.setLeft(c_7);
|
||||
TreeNode<String> _Blank1 = new TreeNode<String>("_");
|
||||
TreeNode<String> _Blank2 = new TreeNode<String>("_");
|
||||
O.setLeft(_Blank1); O.setRight(_Blank2);
|
||||
TreeNode<String> c_8 = new TreeNode<String>("8");
|
||||
_Blank1.setLeft(c_8);
|
||||
TreeNode<String> c_9 = new TreeNode<String>("9");
|
||||
TreeNode<String> c_0 = new TreeNode<String>("0");
|
||||
_Blank2.setLeft(c_9); _Blank2.setRight(c_0);
|
||||
// complete
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,257 @@
|
||||
|
||||
public class TreeNode<T> {
|
||||
private T element;
|
||||
private TreeNode<T> left;
|
||||
private TreeNode<T> right;
|
||||
|
||||
public TreeNode(T element){
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public T getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public void setElement(T element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public TreeNode<T> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(TreeNode<T> left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public TreeNode<T> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(TreeNode<T> right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public boolean isLeaf(){
|
||||
return (left == right) && (left == null);
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return element.toString();
|
||||
}
|
||||
|
||||
//O(n)
|
||||
public String preorder(){
|
||||
if (isLeaf()){
|
||||
return element.toString();
|
||||
}
|
||||
else if ((left != null) && (right == null)){
|
||||
return element.toString() + " " + left.preorder();
|
||||
}
|
||||
else if ((left == null) && (right != null)){
|
||||
return element.toString() + " " + right.preorder();
|
||||
}
|
||||
else{
|
||||
return element.toString() + " " + left.preorder() + " " + right.preorder();
|
||||
}
|
||||
}
|
||||
|
||||
//O(n)
|
||||
public String postorder(){
|
||||
if (isLeaf()){
|
||||
return element.toString();
|
||||
}
|
||||
else if ((left != null) && (right == null)){
|
||||
return left.postorder() + " " + element.toString();
|
||||
}
|
||||
else if ((left == null) && (right != null)){
|
||||
return right.postorder() + " " + element.toString();
|
||||
}
|
||||
else{
|
||||
return left.postorder() + " " + right.postorder() + " " + element.toString();
|
||||
}
|
||||
}
|
||||
|
||||
//O(n)
|
||||
public int size(){
|
||||
if (isLeaf()){
|
||||
return 1;
|
||||
}
|
||||
else if ((left != null) && (right == null)){
|
||||
return 1 + left.size();
|
||||
}
|
||||
else if ((left == null) && (right != null)){
|
||||
return 1 + right.size();
|
||||
}
|
||||
else{
|
||||
return 1 + left.size() + right.size();
|
||||
}
|
||||
}
|
||||
|
||||
//O(n)
|
||||
public int height(){
|
||||
if (isLeaf()){
|
||||
return 0;
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
return left.height() + 1;
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
return right.height() + 1;
|
||||
}
|
||||
else{
|
||||
return Math.max(left.height(), right.height()) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
//O(1)
|
||||
public void insertLeft(T element){
|
||||
if (left == null){
|
||||
left = new TreeNode<T>(element);
|
||||
}
|
||||
else{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//O(1)
|
||||
public void insertRight(T element){
|
||||
if (right == null){
|
||||
right = new TreeNode<T>(element);
|
||||
}
|
||||
else{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//O(n log n)
|
||||
public void insert(T element){
|
||||
if (isLeaf()){
|
||||
this.insertLeft(element);
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
this.insertLeft(element);
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
this.insertRight(element);
|
||||
}
|
||||
else{
|
||||
if (left.height() <= right.height()){
|
||||
left.insert(element);
|
||||
}
|
||||
else if (left.height() > right.height()){
|
||||
right.insert(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//O(n^2)
|
||||
public boolean isBalanced(){
|
||||
if (isLeaf()){
|
||||
return true;
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
return Math.abs(left.height() - -1) <= 1;
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
return Math.abs(right.height() - -1) <= 1;
|
||||
}
|
||||
else{
|
||||
return Math.abs(left.height() - right.height()) <= 1;
|
||||
}
|
||||
}
|
||||
|
||||
// //O(log n)
|
||||
// public void insertBalanced(T element){
|
||||
// if (element <= this.getElement()){
|
||||
// if (left == null){
|
||||
// insertLeft(element);
|
||||
// }
|
||||
// else{
|
||||
// left.insertBalanced(element);
|
||||
// }
|
||||
// }
|
||||
// else{
|
||||
// if (right == null){
|
||||
// insertRight(element);
|
||||
// }
|
||||
// else{
|
||||
// right.insertBalanced(element);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
//Best case: O(1)
|
||||
//Worst case: O(n)
|
||||
public boolean search(T element){
|
||||
if (this.element == element){
|
||||
return true;
|
||||
}
|
||||
else if (left == null && right == null){
|
||||
return false;
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
return left.search(element);
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
return right.search(element);
|
||||
}
|
||||
else{
|
||||
return left.search(element) || right.search(element);
|
||||
}
|
||||
}
|
||||
|
||||
//Best case: O(1)
|
||||
//Worst case: O(n)
|
||||
public void remove(T element){
|
||||
if (this.element == element){
|
||||
//This can never happen/work
|
||||
}
|
||||
else if (left == null && right == null){
|
||||
return;
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
if (left.element == element){
|
||||
left = left.getLeft();
|
||||
//left = null;
|
||||
}
|
||||
else{
|
||||
left.remove(element);
|
||||
}
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
if (right.element == element){
|
||||
right = right.getRight();
|
||||
}
|
||||
else{
|
||||
right.remove(element);
|
||||
}
|
||||
}
|
||||
else{
|
||||
if (left.element == element){
|
||||
left = left.getLeft();
|
||||
}
|
||||
else{
|
||||
left.remove(element);
|
||||
}
|
||||
if (right.element == element){
|
||||
right = right.getRight();
|
||||
}
|
||||
else{
|
||||
right.remove(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
+257
@@ -0,0 +1,257 @@
|
||||
|
||||
public class TreeNode<T> {
|
||||
private T element;
|
||||
private TreeNode<T> left;
|
||||
private TreeNode<T> right;
|
||||
|
||||
public TreeNode(T element){
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public T getElement() {
|
||||
return element;
|
||||
}
|
||||
|
||||
public void setElement(T element) {
|
||||
this.element = element;
|
||||
}
|
||||
|
||||
public TreeNode<T> getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(TreeNode<T> left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
public TreeNode<T> getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(TreeNode<T> right) {
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
public boolean isLeaf(){
|
||||
return (left == right) && (left == null);
|
||||
}
|
||||
|
||||
public String toString(){
|
||||
return element.toString();
|
||||
}
|
||||
|
||||
//O(n)
|
||||
public String preorder(){
|
||||
if (isLeaf()){
|
||||
return element.toString();
|
||||
}
|
||||
else if ((left != null) && (right == null)){
|
||||
return element.toString() + " " + left.preorder();
|
||||
}
|
||||
else if ((left == null) && (right != null)){
|
||||
return element.toString() + " " + right.preorder();
|
||||
}
|
||||
else{
|
||||
return element.toString() + " " + left.preorder() + " " + right.preorder();
|
||||
}
|
||||
}
|
||||
|
||||
//O(n)
|
||||
public String postorder(){
|
||||
if (isLeaf()){
|
||||
return element.toString();
|
||||
}
|
||||
else if ((left != null) && (right == null)){
|
||||
return left.postorder() + " " + element.toString();
|
||||
}
|
||||
else if ((left == null) && (right != null)){
|
||||
return right.postorder() + " " + element.toString();
|
||||
}
|
||||
else{
|
||||
return left.postorder() + " " + right.postorder() + " " + element.toString();
|
||||
}
|
||||
}
|
||||
|
||||
//O(n)
|
||||
public int size(){
|
||||
if (isLeaf()){
|
||||
return 1;
|
||||
}
|
||||
else if ((left != null) && (right == null)){
|
||||
return 1 + left.size();
|
||||
}
|
||||
else if ((left == null) && (right != null)){
|
||||
return 1 + right.size();
|
||||
}
|
||||
else{
|
||||
return 1 + left.size() + right.size();
|
||||
}
|
||||
}
|
||||
|
||||
//O(n)
|
||||
public int height(){
|
||||
if (isLeaf()){
|
||||
return 0;
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
return left.height() + 1;
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
return right.height() + 1;
|
||||
}
|
||||
else{
|
||||
return Math.max(left.height(), right.height()) + 1;
|
||||
}
|
||||
}
|
||||
|
||||
//O(1)
|
||||
public void insertLeft(T element){
|
||||
if (left == null){
|
||||
left = new TreeNode<T>(element);
|
||||
}
|
||||
else{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//O(1)
|
||||
public void insertRight(T element){
|
||||
if (right == null){
|
||||
right = new TreeNode<T>(element);
|
||||
}
|
||||
else{
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
//O(n log n)
|
||||
public void insert(T element){
|
||||
if (isLeaf()){
|
||||
this.insertLeft(element);
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
this.insertLeft(element);
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
this.insertRight(element);
|
||||
}
|
||||
else{
|
||||
if (left.height() <= right.height()){
|
||||
left.insert(element);
|
||||
}
|
||||
else if (left.height() > right.height()){
|
||||
right.insert(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//O(n^2)
|
||||
public boolean isBalanced(){
|
||||
if (isLeaf()){
|
||||
return true;
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
return Math.abs(left.height() - -1) <= 1;
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
return Math.abs(right.height() - -1) <= 1;
|
||||
}
|
||||
else{
|
||||
return Math.abs(left.height() - right.height()) <= 1;
|
||||
}
|
||||
}
|
||||
|
||||
// //O(log n)
|
||||
// public void insertBalanced(T element){
|
||||
// if (element <= this.getElement()){
|
||||
// if (left == null){
|
||||
// insertLeft(element);
|
||||
// }
|
||||
// else{
|
||||
// left.insertBalanced(element);
|
||||
// }
|
||||
// }
|
||||
// else{
|
||||
// if (right == null){
|
||||
// insertRight(element);
|
||||
// }
|
||||
// else{
|
||||
// right.insertBalanced(element);
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
//Best case: O(1)
|
||||
//Worst case: O(n)
|
||||
public boolean search(T element){
|
||||
if (this.element == element){
|
||||
return true;
|
||||
}
|
||||
else if (left == null && right == null){
|
||||
return false;
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
return left.search(element);
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
return right.search(element);
|
||||
}
|
||||
else{
|
||||
return left.search(element) || right.search(element);
|
||||
}
|
||||
}
|
||||
|
||||
//Best case: O(1)
|
||||
//Worst case: O(n)
|
||||
public void remove(T element){
|
||||
if (this.element == element){
|
||||
//This can never happen/work
|
||||
}
|
||||
else if (left == null && right == null){
|
||||
return;
|
||||
}
|
||||
else if (left != null && right == null){
|
||||
if (left.element == element){
|
||||
left = left.getLeft();
|
||||
//left = null;
|
||||
}
|
||||
else{
|
||||
left.remove(element);
|
||||
}
|
||||
}
|
||||
else if (left == null && right != null){
|
||||
if (right.element == element){
|
||||
right = right.getRight();
|
||||
}
|
||||
else{
|
||||
right.remove(element);
|
||||
}
|
||||
}
|
||||
else{
|
||||
if (left.element == element){
|
||||
left = left.getLeft();
|
||||
}
|
||||
else{
|
||||
left.remove(element);
|
||||
}
|
||||
if (right.element == element){
|
||||
right = right.getRight();
|
||||
}
|
||||
else{
|
||||
right.remove(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user