forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
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 #1 from thlcly/master
recommit
- Loading branch information
Showing
29 changed files
with
1,834 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
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,28 @@ | ||
# Created by .ignore support plugin (hsz.mobi) | ||
### Java template | ||
*.class | ||
|
||
# Log file | ||
*.log | ||
|
||
# BlueJ files | ||
*.ctxt | ||
|
||
# Mobile Tools for Java (J2ME) | ||
.mtj.tmp/ | ||
|
||
# Package Files # | ||
*.jar | ||
*.war | ||
*.ear | ||
*.zip | ||
*.tar.gz | ||
*.rar | ||
|
||
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml | ||
hs_err_pid* | ||
|
||
# Idea Files # | ||
.idea | ||
*.iml | ||
|
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,24 @@ | ||
<?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>com.aaront.execrise</groupId> | ||
<artifactId>coding2017</artifactId> | ||
<version>1.0.0-SNAPSHOT</version> | ||
<packaging>jar</packaging> | ||
|
||
|
||
<properties> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
<java.version>1.8</java.version> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>4.12</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
90 changes: 90 additions & 0 deletions
90
...4958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/ArrayList.java
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,90 @@ | ||
package com.aaront.exercise.basic; | ||
|
||
import java.util.Arrays; | ||
|
||
public class ArrayList implements List { | ||
|
||
private int size = 0; | ||
|
||
private static final double factor = 0.75; | ||
|
||
private Object[] elementData = new Object[100]; | ||
|
||
public void add(Object o) { | ||
_ensureCapacityEnough(); | ||
elementData[size++] = o; | ||
} | ||
|
||
public void add(int index, Object o) { | ||
if (index < 0 || index > size) throw new IndexOutOfBoundsException("index超出边界"); | ||
_ensureCapacityEnough(); | ||
int i = size; | ||
for (; i > index; i--) { | ||
elementData[i] = elementData[i - 1]; | ||
} | ||
elementData[i] = o; | ||
size++; | ||
} | ||
|
||
private void _ensureCapacityEnough() { | ||
if (size >= elementData.length) { | ||
dilatancy(); | ||
} | ||
} | ||
|
||
private void dilatancy() { | ||
int newLength = elementData.length + (int) (elementData.length * factor); | ||
elementData = Arrays.copyOf(elementData, newLength); | ||
} | ||
|
||
public Object get(int index) { | ||
if(index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); | ||
return elementData[index]; | ||
} | ||
|
||
public Object remove(int index) { | ||
if (index < 0 || index >= size) throw new IndexOutOfBoundsException("index超出边界"); | ||
Object element = elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, size - 1 - index); | ||
size--; | ||
return element; | ||
|
||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
public Iterator iterator() { | ||
return new ArrayListIterator(this); | ||
} | ||
|
||
public Object[] toArray() { | ||
Object[] objects = new Object[size]; | ||
System.arraycopy(elementData, 0, objects, 0, size); | ||
return objects; | ||
} | ||
|
||
private static class ArrayListIterator implements Iterator { | ||
|
||
private ArrayList arrayList; | ||
private int pos = 0; | ||
|
||
private ArrayListIterator(ArrayList arrayList) { | ||
this.arrayList = arrayList; | ||
} | ||
|
||
public boolean hasNext() { | ||
return pos < arrayList.size(); | ||
} | ||
|
||
public Object next() { | ||
return arrayList.elementData[pos++]; | ||
} | ||
|
||
public void remove() { | ||
arrayList.remove(pos - 1); | ||
pos--; | ||
} | ||
} | ||
} |
235 changes: 235 additions & 0 deletions
235
...958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/BinaryTree.java
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,235 @@ | ||
package com.aaront.exercise.basic; | ||
|
||
public class BinaryTree { | ||
|
||
private BinaryTreeNode head = new BinaryTreeNode(null); | ||
private BinaryTreeNode root; | ||
private int size; | ||
private int index = 0; | ||
|
||
public static final int PREORDER = 0; | ||
public static final int INORDER = 1; | ||
public static final int POSTORDER = 2; | ||
public static final int HIERARCHICAL = 3; | ||
|
||
public static final int RECURSION = 10; | ||
public static final int ITERATION = 11; | ||
|
||
public void add(Integer o) { | ||
BinaryTreeNode node = new BinaryTreeNode(o); | ||
if (root == null) { | ||
root = node; | ||
head.setLeft(root); | ||
} else { | ||
insert(root, node); | ||
} | ||
size++; | ||
} | ||
|
||
private void insert(BinaryTreeNode node, BinaryTreeNode newNode) { | ||
// 要插入的节点插入当前节点的左子树 | ||
if (node.getData() > newNode.getData()) { | ||
if (node.getLeft() == null) { | ||
node.setLeft(newNode); | ||
} else { | ||
insert(node.left, newNode); | ||
} | ||
} else { // 要插入的节点插入当前节点的右子树 | ||
if (node.getRight() == null) { | ||
node.setRight(newNode); | ||
} else { | ||
insert(node.right, newNode); | ||
} | ||
} | ||
} | ||
|
||
public BinaryTreeNode search(int data) { | ||
return search(data, ITERATION); | ||
} | ||
|
||
public BinaryTreeNode search(int data, int method) { | ||
switch (method) { | ||
case RECURSION: | ||
return findNodeRecursion(root, data); | ||
case ITERATION: | ||
return findNodeIteration(data); | ||
default: | ||
throw new IllegalArgumentException("不支持的查找方法"); | ||
} | ||
} | ||
|
||
private BinaryTreeNode findNodeRecursion(BinaryTreeNode node, int data) { | ||
if (node == null) return null; | ||
if (node.getData() == data) return node; | ||
if (node.getData() > data) return findNodeRecursion(node.getLeft(), data); | ||
return findNodeRecursion(node.getRight(), data); | ||
} | ||
|
||
private BinaryTreeNode findNodeIteration(int data) { | ||
BinaryTreeNode currentNode = root; | ||
while (currentNode != null) { | ||
if (currentNode.getData() == data) { | ||
return currentNode; | ||
} | ||
if (currentNode.getData() > data) { | ||
currentNode = currentNode.getLeft(); | ||
} else { | ||
currentNode = currentNode.getRight(); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
public BinaryTreeNode min() { | ||
return findMin(root); | ||
} | ||
|
||
private BinaryTreeNode findMin(BinaryTreeNode node) { | ||
if (node == null) return null; | ||
if (node.getLeft() == null) return node; | ||
return findMin(node.getLeft()); | ||
} | ||
|
||
public BinaryTreeNode max() { | ||
return findMax(root); | ||
} | ||
|
||
private BinaryTreeNode findMax(BinaryTreeNode node) { | ||
if (node == null) return null; | ||
if (node.getRight() == null) return node; | ||
return findMax(node.getRight()); | ||
} | ||
|
||
public void delete(Integer data) { | ||
BinaryTreeNode node = search(data); | ||
if (node == null) return; | ||
BinaryTreeNode parentNode = searchParentNode(node); | ||
if (parentNode == null) return; | ||
// 删除叶子节点 | ||
if (node.getLeft() == null && node.getRight() == null) { | ||
if (parentNode.getLeft() == node) parentNode.setLeft(null); | ||
else parentNode.setRight(null); | ||
} else if (node.getLeft() != null && node.getRight() == null) { // 删除只有左子树的节点 | ||
if (parentNode.getLeft() == node) parentNode.setLeft(node.getLeft()); | ||
else parentNode.setRight(node.getLeft()); | ||
} else if (node.getRight() != null && node.getLeft() == null) { // 删除只有右子树的节点 | ||
if (parentNode.getLeft() == node) parentNode.setLeft(node.getRight()); | ||
else parentNode.setRight(node.getRight()); | ||
} else { // 删除有两个子树的节点 | ||
BinaryTreeNode replace = findMin(node.getRight()); | ||
BinaryTreeNode replaceParentNode = searchParentNode(replace); | ||
replaceParentNode.setLeft(replace.getRight()); | ||
node.setData(replace.getData()); | ||
replace.setLeft(null); | ||
replace.setRight(null); | ||
} | ||
size--; | ||
} | ||
|
||
private BinaryTreeNode searchParentNode(BinaryTreeNode node) { | ||
if (node == null) return null; | ||
if (node == root) return head; | ||
BinaryTreeNode current = root; | ||
while (current != null) { | ||
if (current.getLeft() == node || current.getRight() == node) return current; | ||
if (current.getData().compareTo(node.getData()) > 0) current = current.getLeft(); | ||
else current = current.getRight(); | ||
} | ||
return null; | ||
} | ||
|
||
public int[] traversal() { | ||
return traversal(PREORDER); | ||
} | ||
|
||
public int[] traversal(int order) { | ||
int[] datas = new int[size]; | ||
if (order == PREORDER) { | ||
preorderTraversal(root, datas); | ||
} else if (order == INORDER) { | ||
inorderTraversal(root, datas); | ||
} else if (order == POSTORDER) { | ||
postorderTraversal(root, datas); | ||
} else { | ||
hierarchicalTraversal(root, datas); | ||
} | ||
index = 0; | ||
return datas; | ||
} | ||
|
||
private void preorderTraversal(BinaryTreeNode node, int[] datas) { | ||
if (node == null) { | ||
return; | ||
} | ||
|
||
datas[index++] = node.getData(); | ||
preorderTraversal(node.getLeft(), datas); | ||
preorderTraversal(node.getRight(), datas); | ||
} | ||
|
||
private void inorderTraversal(BinaryTreeNode node, int[] datas) { | ||
if (node == null) { | ||
return; | ||
} | ||
|
||
inorderTraversal(node.getLeft(), datas); | ||
datas[index++] = node.getData(); | ||
inorderTraversal(node.getRight(), datas); | ||
} | ||
|
||
private void postorderTraversal(BinaryTreeNode node, int[] datas) { | ||
if (node == null) { | ||
return; | ||
} | ||
|
||
postorderTraversal(node.getLeft(), datas); | ||
postorderTraversal(node.getRight(), datas); | ||
datas[index++] = node.getData(); | ||
} | ||
|
||
private void hierarchicalTraversal(BinaryTreeNode node, int[] datas) { | ||
if (node == null) return; | ||
Queue queue = new Queue(); | ||
queue.enQueue(node); | ||
while (!queue.isEmpty()) { | ||
BinaryTreeNode tmp = (BinaryTreeNode) queue.deQueue(); | ||
datas[index++] = tmp.getData(); | ||
if (tmp.getLeft() != null) queue.enQueue(tmp.getLeft()); | ||
if (tmp.getRight() != null) queue.enQueue(tmp.getRight()); | ||
} | ||
} | ||
|
||
public class BinaryTreeNode { | ||
private Integer data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode(Integer data) { | ||
this.data = data; | ||
} | ||
|
||
public Integer getData() { | ||
return data; | ||
} | ||
|
||
public void setData(Integer 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; | ||
} | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...54958168/class01/BasicDataStructure/src/main/java/com/aaront/exercise/basic/Iterator.java
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,9 @@ | ||
package com.aaront.exercise.basic; | ||
|
||
public interface Iterator { | ||
boolean hasNext(); | ||
|
||
Object next(); | ||
|
||
void remove(); | ||
} |
Oops, something went wrong.