-
Notifications
You must be signed in to change notification settings - Fork 641
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from nusubmarine01/master
第三组第一次作业
- Loading branch information
Showing
151 changed files
with
7,717 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package com.byhieg.coding2017; | ||
|
||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
|
||
public void add(Object o) { | ||
isCapacityEnough(size + 1); | ||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
checkForAdd(index); | ||
isCapacityEnough(size + 1); | ||
System.arraycopy(elementData,index,elementData,index + 1,size - index); | ||
elementData[index] = o; | ||
size++; | ||
} | ||
|
||
private void checkForAdd(int index){ | ||
if (index < 0 || index > size) { | ||
throw new IndexOutOfBoundsException("index不在指定范围内"); | ||
} | ||
|
||
} | ||
private void isCapacityEnough(int size) { | ||
if (size > 100) { | ||
explicitCapacity(size); | ||
} | ||
if (size < 0) { | ||
throw new OutOfMemoryError(); | ||
} | ||
} | ||
|
||
private final static int MAX_ARRAY_LENGTH = Integer.MAX_VALUE - 8; | ||
|
||
public void explicitCapacity(int size) { | ||
int newLength = elementData.length * 2; | ||
if (newLength > (MAX_ARRAY_LENGTH)){ | ||
newLength = (size > MAX_ARRAY_LENGTH ? Integer.MAX_VALUE : MAX_ARRAY_LENGTH); | ||
} | ||
elementData = Arrays.copyOf(elementData, newLength); | ||
|
||
} | ||
|
||
|
||
public Object get(int index) { | ||
checkRange(index); | ||
return elementData[index]; | ||
} | ||
|
||
private void checkRange(int index){ | ||
if (index < 0 || index >= size) { | ||
throw new IndexOutOfBoundsException("index不在范围内"); | ||
} | ||
} | ||
|
||
public Object remove(int index) { | ||
Object o = get(index); | ||
//要保证后面的 index + 1是有效的 | ||
int moveSize = size - index - 1; | ||
if (moveSize > 0) { | ||
System.arraycopy(elementData,index + 1,elementData,index, size - index); | ||
} | ||
elementData[--size] = null; | ||
return o; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new MyIterator(); | ||
} | ||
|
||
private class MyIterator implements Iterator { | ||
|
||
private int cursor = 0; | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor != size; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
if (cursor >= size) { | ||
throw new NoSuchElementException(); | ||
} | ||
return elementData[cursor++]; | ||
} | ||
} | ||
|
||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package com.byhieg.coding2017; | ||
|
||
/** | ||
* Created by byhieg on 17/2/22. | ||
* Mail to [email protected] | ||
*/ | ||
|
||
public class BinaryTree { | ||
|
||
private BinaryTreeNode root = new BinaryTreeNode(); | ||
|
||
public BinaryTree(Object rootData){ | ||
root = root.insert(rootData); | ||
} | ||
|
||
|
||
//左边的值小于等于父节点的值,右边的值大于父节点的值 | ||
private void insertNode(BinaryTreeNode root, BinaryTreeNode node) { | ||
int value = (int)node.getData(); | ||
int rootValue = (int)root.getData(); | ||
if (value <= rootValue){ | ||
insertLeft(root,node); | ||
}else { | ||
insertRight(root,node); | ||
} | ||
} | ||
|
||
|
||
public void insert(Object o) { | ||
BinaryTreeNode node = new BinaryTreeNode(); | ||
node = node.insert(o); | ||
insertNode(root,node); | ||
} | ||
|
||
private void insertLeft(BinaryTreeNode father, BinaryTreeNode node) { | ||
if (father.getLeft() == null) { | ||
father.setLeft(node); | ||
}else{ | ||
insertNode(father.getLeft(),node); | ||
} | ||
} | ||
|
||
private void insertRight(BinaryTreeNode father, BinaryTreeNode node) { | ||
if (father.getRight() == null) { | ||
father.setRight(node); | ||
} else { | ||
insertNode(father.getRight(),node); | ||
} | ||
} | ||
|
||
//前序遍历输出书 | ||
private void preOrder(BinaryTreeNode node) { | ||
if (node != null) { | ||
System.out.println(node.getData()); | ||
preOrder(node.getLeft()); | ||
preOrder(node.getRight()); | ||
} | ||
} | ||
|
||
|
||
//打印树 | ||
public void printTree(){ | ||
preOrder(root); | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.byhieg.coding2017; | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public Object getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
|
||
public BinaryTreeNode getLeft() { | ||
return left; | ||
} | ||
|
||
public void setLeft(BinaryTreeNode left) { | ||
this.left = left; | ||
} | ||
|
||
public BinaryTreeNode getRight() { | ||
return right; | ||
} | ||
|
||
public void setRight(BinaryTreeNode right) { | ||
this.right = right; | ||
} | ||
|
||
public BinaryTreeNode insert(Object o) { | ||
BinaryTreeNode node = new BinaryTreeNode(); | ||
int value = (int)o; | ||
node.setData(value); | ||
node.setRight(null); | ||
node.setLeft(null); | ||
return node; | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.byhieg.coding2017; | ||
|
||
public interface Iterator { | ||
public boolean hasNext(); | ||
public Object next(); | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,152 @@ | ||
package com.byhieg.coding2017; | ||
|
||
import javax.swing.text.html.HTMLDocument; | ||
|
||
public class LinkedList implements List { | ||
|
||
private Node head; | ||
int size = 0; | ||
|
||
public void add(Object o) { | ||
addLast(o); | ||
} | ||
|
||
public void add(int index, Object o) { | ||
checkRangeForAdd(index); | ||
if (index == size) { | ||
addLast(o); | ||
} | ||
Node nextNode = node(index); | ||
Node newNode = new Node(o, nextNode); | ||
|
||
Node prevNode; | ||
if (index == 0) { | ||
prevNode = null; | ||
} else { | ||
prevNode = node(index); | ||
} | ||
|
||
|
||
if (prevNode == null) { | ||
head = newNode; | ||
}else{ | ||
prevNode.next = newNode; | ||
} | ||
|
||
size++; | ||
} | ||
|
||
|
||
private Node node(int index) { | ||
Node cursor = head; | ||
for (int i = 0; i < index; i++) { | ||
cursor = cursor.next; | ||
} | ||
return cursor; | ||
} | ||
|
||
private void checkRangeForAdd(int index) { | ||
if (index > size || index < 0) { | ||
throw new IndexOutOfBoundsException("指定的index超过界限"); | ||
} | ||
} | ||
|
||
public Object get(int index) { | ||
checkRange(index); | ||
return node(index).data; | ||
} | ||
|
||
private void checkRange(int index) { | ||
if (index >= size || index < 0) { | ||
throw new IndexOutOfBoundsException("指定index超过界限"); | ||
} | ||
} | ||
|
||
public Object remove(int index) { | ||
checkRange(index); | ||
Node targetNode = node(index); | ||
Object o = targetNode.data; | ||
Node prevNode ; | ||
Node nextNode = targetNode.next; | ||
|
||
if (index == 0) { | ||
prevNode = null; | ||
}else{ | ||
prevNode = node(index - 1); | ||
} | ||
if (prevNode == null) { | ||
head = nextNode; | ||
targetNode.next = null; | ||
}else { | ||
prevNode.next = nextNode; | ||
targetNode.next = null; | ||
} | ||
|
||
targetNode.data = null; | ||
size --; | ||
return o; | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o) { | ||
Node nextNode = head; | ||
Node newNode = new Node(o, nextNode); | ||
head = newNode; | ||
size++; | ||
} | ||
|
||
public void addLast(Object o) { | ||
Node newNode = new Node(o, null); | ||
if (size == 0) { | ||
head = newNode; | ||
}else{ | ||
Node lastNode = node(size - 1); | ||
lastNode.next = newNode; | ||
} | ||
size++; | ||
} | ||
|
||
public Object removeFirst() { | ||
return remove(0); | ||
} | ||
|
||
public Object removeLast() { | ||
return remove(size() - 1); | ||
} | ||
|
||
public Iterator iterator() { | ||
|
||
return new MyIterator(); | ||
} | ||
|
||
private class MyIterator implements Iterator { | ||
|
||
public Node cursor = head; | ||
@Override | ||
public boolean hasNext() { | ||
return cursor != null; | ||
} | ||
|
||
@Override | ||
public Object next() { | ||
Object o = cursor.data; | ||
cursor = cursor.next; | ||
return o; | ||
} | ||
} | ||
|
||
|
||
|
||
private static class Node { | ||
Object data; | ||
Node next; | ||
|
||
public Node(Object data, Node next) { | ||
this.data = data; | ||
this.next = next; | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.byhieg.coding2017; | ||
|
||
public interface List { | ||
public void add(Object o); | ||
|
||
public void add(int index, Object o); | ||
|
||
public Object get(int index); | ||
|
||
public Object remove(int index); | ||
|
||
public int size(); | ||
} |
Oops, something went wrong.