forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
gongxun
committed
Feb 26, 2017
1 parent
683079a
commit f83c004
Showing
12 changed files
with
543 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,55 @@ | ||
package binarytree; | ||
|
||
/** | ||
* Created by william on 2017/2/16. | ||
*/ | ||
public class BinaryTree { | ||
private Node root; | ||
|
||
class Node { | ||
private Node left; | ||
private Node right; | ||
private Comparable data; | ||
|
||
public Node(Node left, Node right, Comparable data) { | ||
this.left = left; | ||
this.right = right; | ||
this.data = data; | ||
} | ||
|
||
private void add(Comparable data) { | ||
if (this.data.compareTo(data) >= 0) | ||
if (this.left == null) | ||
this.left = new Node(null, null, data); | ||
else | ||
left.add(data); | ||
else if (this.data.compareTo(data) < 0) | ||
if (this.right == null) | ||
this.right = new Node(null, null, data); | ||
else | ||
this.right.add(data); | ||
} | ||
|
||
public Comparable getData() { | ||
return this.data; | ||
} | ||
|
||
public Node getLeft() { | ||
return this.left; | ||
} | ||
|
||
public Node getRight() { | ||
return this.right; | ||
} | ||
} | ||
|
||
public void add(Comparable data) { | ||
if (this.root == null) | ||
root = new Node(null, null, data); | ||
else this.root.add(data); | ||
} | ||
|
||
public void printByType(SearchType<Node> type) { | ||
type.printByType(this.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,16 @@ | ||
package binarytree; | ||
|
||
/** | ||
* Created by william on 2017/2/18. | ||
*/ | ||
public class DLRSearchType implements SearchType<BinaryTree.Node> { | ||
|
||
@Override | ||
public void printByType(BinaryTree.Node root) { | ||
if (root != null) { | ||
System.out.print(root.getData()+" "); | ||
printByType(root.getLeft()); | ||
printByType(root.getRight()); | ||
} | ||
} | ||
} |
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,15 @@ | ||
package binarytree; | ||
|
||
/** | ||
* Created by william on 2017/2/18. | ||
*/ | ||
public class LDRSearchType implements SearchType<BinaryTree.Node> { | ||
@Override | ||
public void printByType(BinaryTree.Node root) { | ||
if (root != null) { | ||
printByType(root.getLeft()); | ||
System.out.print(root.getData() + " "); | ||
printByType(root.getRight()); | ||
} | ||
} | ||
} |
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,27 @@ | ||
package binarytree; | ||
|
||
import java.util.LinkedList; | ||
|
||
/** | ||
* Created by william on 2017/2/18. | ||
*/ | ||
public class LFSearchType implements SearchType<BinaryTree.Node> { | ||
private LinkedList<BinaryTree.Node> queue = new LinkedList<>(); | ||
|
||
@Override | ||
public void printByType(BinaryTree.Node root) { | ||
if (root == null) | ||
return; | ||
queue.offer(root); | ||
while (!queue.isEmpty()) { | ||
BinaryTree.Node curNode = queue.poll(); | ||
System.out.print(curNode.getData() + " "); | ||
if (curNode.getLeft() != null) | ||
queue.offer(curNode.getLeft()); | ||
if (curNode.getRight() != null) | ||
queue.offer(curNode.getRight()); | ||
} | ||
|
||
} | ||
|
||
} |
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,15 @@ | ||
package binarytree; | ||
|
||
/** | ||
* Created by william on 2017/2/18. | ||
*/ | ||
public class LRDSearchType implements SearchType<BinaryTree.Node> { | ||
@Override | ||
public void printByType(BinaryTree.Node root) { | ||
if (root != null) { | ||
printByType(root.getLeft()); | ||
printByType(root.getRight()); | ||
System.out.print(root.getData() + " "); | ||
} | ||
} | ||
} |
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 binarytree; | ||
|
||
/** | ||
* Created by william on 2017/2/18. | ||
*/ | ||
public interface SearchType<T> { | ||
|
||
void printByType(T 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,135 @@ | ||
package list; | ||
|
||
import java.util.Arrays; | ||
import java.util.NoSuchElementException; | ||
|
||
/** | ||
* Created by william on 2017/2/25. | ||
*/ | ||
public class ArrayList<T> implements List<T> { | ||
private static final int DEFAULT_CAPACITY = 10; | ||
private int size; | ||
private Object[] elementData; | ||
|
||
public ArrayList() { | ||
elementData = new Object[DEFAULT_CAPACITY]; | ||
} | ||
|
||
public ArrayList(int initialCapacity) { | ||
if (initialCapacity < 0) | ||
throw new RuntimeException("非法初始化大小参数!"); | ||
elementData = new Object[initialCapacity]; | ||
} | ||
|
||
public boolean add(T ele) { | ||
grow(); | ||
elementData[size] = ele; | ||
size++; | ||
return true; | ||
} | ||
|
||
public T get(int index) { | ||
checkBounds(index); | ||
return (T) elementData[index]; | ||
} | ||
|
||
public T remove(int index) { | ||
checkBounds(index); | ||
T removeEle = (T) elementData[index]; | ||
System.arraycopy(elementData, index + 1, elementData, index, size - index); | ||
size--; | ||
return removeEle; | ||
} | ||
|
||
public boolean add(int index, T ele) { | ||
checkBounds(index); | ||
size++;//有效元素内容先加,保证长度极限情况grow在添加前生效 | ||
grow(); | ||
//将原本数组从待插入的index截取,将原本index后的有效值,复制到原本数组index+1之后 | ||
System.arraycopy(elementData, index, elementData, index + 1, size - index); | ||
elementData[index] = ele; | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean remove(T ele) { | ||
int index; | ||
if ((index = indexOf(ele)) == -1) | ||
return false; | ||
remove(index); | ||
return true; | ||
} | ||
|
||
private void checkBounds(int index) { | ||
if (index < 0 || index >= size) | ||
throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); | ||
} | ||
|
||
public int size() { | ||
return size; | ||
} | ||
|
||
private void grow() { | ||
if (size >= elementData.length) { | ||
int curLen = elementData.length; | ||
int newLen = curLen + (curLen >> 1) > Integer.MAX_VALUE ? Integer.MAX_VALUE : curLen + (curLen >> 1); | ||
elementData = Arrays.copyOf(elementData, newLen); | ||
} | ||
} | ||
|
||
public boolean isEmpty() { | ||
return size == 0; | ||
} | ||
|
||
@Override | ||
public boolean contains(T ele) { | ||
return indexOf(ele) != -1; | ||
} | ||
|
||
public int indexOf(T ele) { | ||
for (int i = 0; i < size; i++) { | ||
if (ele == null) | ||
if (null == elementData[i]) | ||
return i; | ||
else if (ele.equals(elementData[i])) | ||
return i; | ||
} | ||
return -1; | ||
} | ||
|
||
public Iterator<T> iterator() { | ||
return new Itr<T>(); | ||
} | ||
|
||
private class Itr<T> implements Iterator<T> { | ||
int cursor;//待遍历元素的下标 | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return cursor != size; | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (cursor >= size) | ||
throw new NoSuchElementException(); | ||
return (T) elementData[cursor++]; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
if (cursor >= size) | ||
throw new NoSuchElementException(); | ||
ArrayList.this.remove(cursor--); | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
StringBuilder sb = new StringBuilder("[ "); | ||
for (Object ele : elementData) { | ||
sb.append(ele).append(" "); | ||
} | ||
return sb.append("]").toString(); | ||
} | ||
} |
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 list; | ||
|
||
/** | ||
* Created by IBM on 2017/2/25. | ||
*/ | ||
public interface Iterator<T> { | ||
|
||
boolean hasNext(); | ||
|
||
T next(); | ||
|
||
void remove(); | ||
} |
Oops, something went wrong.