diff --git a/group17/785396327/binarytree/BinaryTree.java b/group17/785396327/binarytree/BinaryTree.java new file mode 100644 index 0000000000..bf5a6b639c --- /dev/null +++ b/group17/785396327/binarytree/BinaryTree.java @@ -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 type) { + type.printByType(this.root); + } +} diff --git a/group17/785396327/binarytree/DLRSearchType.java b/group17/785396327/binarytree/DLRSearchType.java new file mode 100644 index 0000000000..3d1adbbdc7 --- /dev/null +++ b/group17/785396327/binarytree/DLRSearchType.java @@ -0,0 +1,16 @@ +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class DLRSearchType implements SearchType { + + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + System.out.print(root.getData()+" "); + printByType(root.getLeft()); + printByType(root.getRight()); + } + } +} diff --git a/group17/785396327/binarytree/LDRSearchType.java b/group17/785396327/binarytree/LDRSearchType.java new file mode 100644 index 0000000000..da18dbc1b9 --- /dev/null +++ b/group17/785396327/binarytree/LDRSearchType.java @@ -0,0 +1,15 @@ +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class LDRSearchType implements SearchType { + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + printByType(root.getLeft()); + System.out.print(root.getData() + " "); + printByType(root.getRight()); + } + } +} diff --git a/group17/785396327/binarytree/LFSearchType.java b/group17/785396327/binarytree/LFSearchType.java new file mode 100644 index 0000000000..d983c50ff9 --- /dev/null +++ b/group17/785396327/binarytree/LFSearchType.java @@ -0,0 +1,27 @@ +package binarytree; + +import java.util.LinkedList; + +/** + * Created by william on 2017/2/18. + */ +public class LFSearchType implements SearchType { + private LinkedList 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()); + } + + } + +} diff --git a/group17/785396327/binarytree/LRDSearchType.java b/group17/785396327/binarytree/LRDSearchType.java new file mode 100644 index 0000000000..250645e6df --- /dev/null +++ b/group17/785396327/binarytree/LRDSearchType.java @@ -0,0 +1,15 @@ +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public class LRDSearchType implements SearchType { + @Override + public void printByType(BinaryTree.Node root) { + if (root != null) { + printByType(root.getLeft()); + printByType(root.getRight()); + System.out.print(root.getData() + " "); + } + } +} diff --git a/group17/785396327/binarytree/SearchType.java b/group17/785396327/binarytree/SearchType.java new file mode 100644 index 0000000000..606124a781 --- /dev/null +++ b/group17/785396327/binarytree/SearchType.java @@ -0,0 +1,9 @@ +package binarytree; + +/** + * Created by william on 2017/2/18. + */ +public interface SearchType { + + void printByType(T root); +} diff --git a/group17/785396327/list/ArrayList.java b/group17/785396327/list/ArrayList.java new file mode 100644 index 0000000000..9d79bd2013 --- /dev/null +++ b/group17/785396327/list/ArrayList.java @@ -0,0 +1,135 @@ +package list; + +import java.util.Arrays; +import java.util.NoSuchElementException; + +/** + * Created by william on 2017/2/25. + */ +public class ArrayList implements List { + 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 iterator() { + return new Itr(); + } + + private class Itr implements Iterator { + 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(); + } +} diff --git a/group17/785396327/list/Iterator.java b/group17/785396327/list/Iterator.java new file mode 100644 index 0000000000..0df87c6cf1 --- /dev/null +++ b/group17/785396327/list/Iterator.java @@ -0,0 +1,13 @@ +package list; + +/** + * Created by IBM on 2017/2/25. + */ +public interface Iterator { + + boolean hasNext(); + + T next(); + + void remove(); +} diff --git a/group17/785396327/list/LinkedList.java b/group17/785396327/list/LinkedList.java new file mode 100644 index 0000000000..1b1fa0fccb --- /dev/null +++ b/group17/785396327/list/LinkedList.java @@ -0,0 +1,163 @@ +package list; + +/** + * Created by william on 2017/2/25. + */ +public class LinkedList implements List { + private int size; + private Node first; + private Node last; + + private static class Node { + Node next; + Node prev; + T data; + + Node(Node prev, Node next, T data) { + this.prev = prev; + this.next = next; + this.data = data; + } + } + + @Override + public int size() { + return this.size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public boolean contains(T ele) { + Node head = first; + while (head != null) { + if ((ele == null && head.data == null) || (ele.equals(head.data))) + return true; + head = head.next; + } + return false; + } + + @Override + public boolean add(T ele) { + if (first == null) + first = last = new Node(null, null, ele); + else { + //新添加节点的上一个节点是原来链表的最后一个节点 + Node addNode = new Node(last, null, ele); + //原来链表的最后一个节点的下一个节点需要指向新添加的节点 + last.next = addNode; + //更新最后一个节点为新添加的节点 + last = addNode; + } + size++; + return true; + } + + @Override + public boolean add(int index, T ele) { + checkBounds(index, true); + if (index == size) add(ele); + else { + Node head = first; + for (int i = 0; i < size; i++) { + if (i == index - 1)//得到要插入位置的前一个节点 + head.next = new Node(head, head.next, ele); + else + head = head.next; + } + } + size++; + return true; + } + + @Override + public boolean remove(T ele) { + if (!contains(ele)) + return false; + Node head = first; + Node prev = head.prev; + while (head != null) { + if ((ele == null && ele == head.data) || ele.equals(head.data)) { + prev.next = head.next; + size--; + return true; + } + prev = head; + head = head.next; + } + return false; + } + + @Override + public T remove(int index) { + checkBounds(index, false); + T removeEle = get(index); + remove(removeEle); + return removeEle; + } + + @Override + public T get(int index) { + checkBounds(index, false); + if (index > (size >> 1)) { + //索引位置大于1/2size,从后往前 + Node tail = last; + for (int i = size - 1; i >= 0; i--) { + if (i == index) + return (T) tail.data; + else + tail = tail.prev; + } + } else { + //从前往后 + Node head = first; + for (int i = 0; i < size; i++) { + if (i == index) + return (T) head.data; + else + head = head.next; + } + } + return null; + } + + @Override + public int indexOf(T ele) { + if (first == null) return -1; + Node head = first; + for (int i = 0; i < size; i++) { + if ((ele == null && ele == head.data) || ele.equals(head.data)) + return i; + head = head.next; + } + return -1; + } + + /** + * 指定位置查找元素和插入元素到指定位置IndexOutofBounds的判断标准不一样 + * + * @param index + * @param isInsert + */ + private void checkBounds(int index, boolean isInsert) { + if (isInsert && (index < 0 || index > size))//允许插入到最后一个元素之后,不能排除= + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + if (index < 0 || index >= size)//查询从0 --- size-1 + throw new IndexOutOfBoundsException("index : " + index + ", size : [ 0 - " + size + " ]"); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("[ "); + Node head = first; + while (head != null) { + sb.append(head.data + " "); + head = head.next; + } + return sb.append("]").toString(); + } +} diff --git a/group17/785396327/list/List.java b/group17/785396327/list/List.java new file mode 100644 index 0000000000..706f9f29bb --- /dev/null +++ b/group17/785396327/list/List.java @@ -0,0 +1,25 @@ +package list; + +/** + * Created by william on 2017/2/25. + */ +public interface List { + + int size(); + + boolean isEmpty(); + + boolean contains(T ele); + + boolean add(T ele); + + boolean add(int index, T ele); + + boolean remove(T ele); + + T remove(int index); + + T get(int index); + + int indexOf(T ele); +} diff --git a/group17/785396327/queue/Queue.java b/group17/785396327/queue/Queue.java new file mode 100644 index 0000000000..68bea83b77 --- /dev/null +++ b/group17/785396327/queue/Queue.java @@ -0,0 +1,41 @@ +package queue; + +import java.util.NoSuchElementException; + +/** + * Created by william on 2017/2/25. + */ +public class Queue extends LinkedList { + + public boolean add(T ele) { + return add(ele); + } + + public T element() { + if (size() == 0) + throw new NoSuchElementException("队列中没有元素!"); + return get(0); + } + + public boolean offer(T ele) { + return add(ele); + } + + public T peek() { + if (size() == 0) + return null; + return get(0); + } + + public T poll() { + if (size() == 0) + return null; + return remove(0); + } + + public T remove() { + if (size() == 0) + throw new NoSuchElementException("队列中没有元素!"); + return remove(0); + } +} diff --git a/group17/785396327/stack/Stack.java b/group17/785396327/stack/Stack.java new file mode 100644 index 0000000000..c917809cc3 --- /dev/null +++ b/group17/785396327/stack/Stack.java @@ -0,0 +1,29 @@ +package stack; + +import java.util.EmptyStackException; + +/** + * Created by william on 2017/2/25. + */ +public class Stack extends ArrayList { + + public boolean empty() { + return isEmpty(); + } + + public T peek() { + if (size() == 0) + throw new EmptyStackException(); + return (T) get(0); + } + + public T pop() { + if (size() == 0) + throw new EmptyStackException(); + return (T) remove(0); + } + + public void push(T ele) { + add(0, ele); + } +}