diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java new file mode 100644 index 0000000000..b74dbe85a2 --- /dev/null +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinarySearchTree.java @@ -0,0 +1,154 @@ +package com.coding.basic; + +/** + * BST 二叉排序树 实现 第14小组 296933284 + * + * @author Tonnyson + * + */ +public class BinarySearchTree implements Comparable { + + private Object data; + private BinarySearchTree leftChild; + private BinarySearchTree rightChild; + + public BinarySearchTree() { + super(); + this.data = null; + this.leftChild = null; + this.rightChild = null; + } + + public BinarySearchTree(Object data) { + this(); + this.data = data; + } + + public Object getData() { + return data; + } + + public void setData(Object data) { + this.data = data; + } + + public BinarySearchTree getLeftChild() { + return leftChild; + } + + public void setLeftChild(BinarySearchTree leftChild) { + this.leftChild = leftChild; + } + + public BinarySearchTree getRightChild() { + return rightChild; + } + + public void setRightChild(BinarySearchTree rightChild) { + this.rightChild = rightChild; + } + + /** + * 向树中插入节点 + * + * @param obj + * 节点值 + */ + public void insert(Object obj) { + insert(obj, this); + } + + private boolean insert(Object obj, BinarySearchTree node) { + + BinarySearchTree bstNode = new BinarySearchTree(obj); + + if (node == null) { + node = bstNode; + return true; + } else if (node.compareTo(obj) == 0) { + return true; + } else if (node.compareTo(obj) > 0) { + + if (node.getLeftChild() != null) { + return insert(obj, node.getLeftChild()); + } + + node.leftChild = bstNode; + + } else if (node.compareTo(obj) < 0) { + + if (node.getRightChild() != null) { + return insert(obj, node.getRightChild()); + } + + node.rightChild = bstNode; + } + + return false; + + } + + /** + * 中序遍历 BST 的节点,使之有序输出 + */ + public void inOrder(BinarySearchTree node) { + + if (node != null) { + inOrder(node.getLeftChild()); + visit(node); + inOrder(node.getRightChild()); + } + + } + + /** + * 层序遍历 BST 的节点值 + */ + public void levelOrder(BinarySearchTree node) { + Queue queue = new Queue(); + BinarySearchTree bstNode = null; + queue.enQueue(node); + + while (!queue.isEmpty()) { + bstNode = (BinarySearchTree) queue.deQueue(); + visit(bstNode); + + if (bstNode.getLeftChild() != null) { + queue.enQueue(bstNode.getLeftChild()); + } + + if (bstNode.getRightChild() != null) { + queue.enQueue(bstNode.getRightChild()); + } + } + } + + /** + * 访问指定节点值 + * + * @param node + */ + public void visit(BinarySearchTree node) { + System.out.println(node.getData()); + } + + /** + * 比较 BST 节点值大小 + */ + @Override + public int compareTo(Object obj) { + int result = 0; + + if (obj instanceof Integer) { + Integer value = (Integer) obj; + Integer thisValue = (Integer) this.data; + result = thisValue.compareTo(value); + } else { + String value = obj.toString(); + result = this.data.toString().compareTo(value); + } + + return result; + } + +} diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java deleted file mode 100644 index d01667aaa8..0000000000 --- a/group14/296933284/DataStructuresTest/src/com/coding/basic/BinaryTreeNode.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.coding.basic; - -public class BinaryTreeNode implements Comparable { - - private Object data; - private BinaryTreeNode leftChild; - private BinaryTreeNode rightChild; - - public BinaryTreeNode() { - super(); - this.data = null; - this.leftChild = null; - this.rightChild = null; - } - - public BinaryTreeNode(Object data) { - super(); - this.data = data; - } - - public Object getData() { - return data; - } - - public void setData(Object data) { - this.data = data; - } - - public BinaryTreeNode getLeftChild() { - return leftChild; - } - - public void setLeftChild(BinaryTreeNode leftChild) { - this.leftChild = leftChild; - } - - public BinaryTreeNode getRightChild() { - return rightChild; - } - - public void setRightChild(BinaryTreeNode rightChild) { - this.rightChild = rightChild; - } - - /** - * 向树中插入节点 - * - * @param i - */ - public void insert(int i) { - - } - - // ... - @Override - public int compareTo(Object obj) { - - return 0; - } - - -} - - - - - - - - - - - - - - - diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java index bc0878cb50..8c4cab01b6 100644 --- a/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/JavaTest.java @@ -3,51 +3,64 @@ import org.junit.Test; public class JavaTest { - + @Test - public void binaryTreeNodeTest() { -// BinaryTreeNode tree = new BinaryTreeNode(5); -// -// System.out.println(tree.getData()); + public void binarySearchTreeTest() { + BinarySearchTree bSTree = new BinarySearchTree(5); + + System.out.println(bSTree.getData()); + + // insert + bSTree.insert(1); + bSTree.insert(2); + bSTree.insert(4); + bSTree.insert(6); + bSTree.insert(7); + bSTree.insert(8); + System.out.println("-----------------"); + // inOrder + bSTree.inOrder(bSTree); + + System.out.println("-----------------"); + // levelOrder + bSTree.levelOrder(bSTree); } - - + @Test public void queueTest() { Queue queue = new Queue(); - + // enQueue() for (int i = 0; i < 10; i++) { queue.enQueue("hello: " + i); } - + // size() System.out.println(queue.size()); // 10 // isEmpty System.out.println(queue.isEmpty()); - + // deQueue() for (int i = 0; i < 10; i++) { System.out.println(queue.deQueue()); } - + // size() System.out.println(queue.size()); // 0 - + // isEmpty System.out.println(queue.isEmpty()); } - - + @Test public void stackTest() { Stack stack = new Stack(); - + // push() for (int i = 0; i < 10; i++) { stack.push("hello: " + i); } - + // size() System.out.println(stack.size()); @@ -59,127 +72,105 @@ public void stackTest() { // isEmpty() System.out.println(stack.isEmpty()); System.out.println(stack.size()); - + stack.push("hello world 1"); stack.push("hello world 2"); stack.push("hello world 3"); stack.push("hello world 4"); - + // peek() System.out.println(stack.peek()); - + // isEmpty() System.out.println(stack.isEmpty()); } - + @Test public void linkedListTest() { LinkedList linkedList = new LinkedList(); - // add(obj) - for (int i = 0; i < 10; i++) { + // add() addLast() + for (int i = 0; i < 5; i++) { linkedList.add("hello: " + i); } - // size() - System.out.println(linkedList.size()); // 10 + // iterator() get() getPreNode() + Iterator iter = linkedList.iterator(); - // get(index) - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("-->" + linkedList.get(i)); + while (iter.hasNext()) { + System.out.println(iter.next()); } - -// System.out.println("---->" + linkedList.get(10)); // java.lang.NullPointerException - - // remove() - System.out.println("---->" + linkedList.remove(9)); // 9 - System.out.println("---->" + linkedList.remove(5)); // 5 - System.out.println(linkedList.size()); // 8 - - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("-->" + linkedList.get(i)); + System.out.println("-----------------------"); + LinkedList linkedList1 = new LinkedList(); + + // addFirst() + for (int i = 0; i < 5; i++) { + linkedList1.addFirst("hello: " + i); } - // removeFirst() - System.out.println("---->" + linkedList.removeFirst()); // 0 - // removeLast - System.out.println("---->" + linkedList.removeLast()); // 8 + Iterator iter1 = linkedList1.iterator(); - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("-->" + linkedList.get(i)); - } - - // add(index, obj) - linkedList.add(4, "^_^"); - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("&&&&" + linkedList.get(i)); + while (iter1.hasNext()) { + System.out.println(iter1.next()); } - // addFirst() - linkedList.addFirst("***first***"); - // addLast() - linkedList.addLast("---last---"); - System.out.println(linkedList.get(0)); // ***first*** - System.out.println(linkedList.get(linkedList.size() - 1)); // ---last--- - System.err.println(linkedList.size()); // 9 + System.out.println("-----------------------"); + // remove() + System.out.println(linkedList1.remove(0)); // hello: 4 - for (int i = 0; i < linkedList.size(); i++) { - System.out.println("&&&&" + linkedList.get(i)); - } + System.out.println("-----------------------"); + // removeFirst() removeLast() + System.out.println(linkedList1.removeFirst()); // hello: 3 + System.out.println(linkedList1.removeLast()); // hello: 0 - System.out.println("-------------------------------"); - // iterator - Iterator iter = linkedList.iterator(); - while (iter.hasNext()) { - System.out.println(iter.next()); - } - - System.out.println("-------------------------------"); + System.out.println("-----------------------"); + // size() + System.out.println(linkedList.size()); // 5 } - + @Test public void arrayListTest() { ArrayList arrayList = new ArrayList(); - + // add(obj) for (int i = 0; i < 10; i++) { arrayList.add("hello: " + i); } - + // get(index) for (int i = 0; i < arrayList.size(); i++) { System.out.println("-->" + arrayList.get(i)); } - + // add(index, obj) arrayList.add(5, "Tennyson"); - + for (int i = 0; i < arrayList.size(); i++) { System.out.println("++>" + arrayList.get(i)); } - + // size() System.out.println("size: " + arrayList.size()); System.out.println("index 5: " + arrayList.get(5)); - + // remove() Object value = arrayList.remove(5); System.out.println("index 5: " + value); System.out.println("size: " + arrayList.size()); System.out.println("index5: " + arrayList.get(5)); - + for (int i = 0; i < arrayList.size(); i++) { System.out.println("index " + i + " : " + arrayList.get(i)); } - + System.out.println("-------------------------------"); // iterator Iterator iter = arrayList.iterator(); while (iter.hasNext()) { System.out.println(iter.next()); } - + System.out.println("-------------------------------"); - + } } diff --git a/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java index 72b4ba8a90..2a397e9bec 100644 --- a/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java +++ b/group14/296933284/DataStructuresTest/src/com/coding/basic/LinkedList.java @@ -1,7 +1,11 @@ package com.coding.basic; +import java.util.Collection; + +import org.junit.Test; + /** - * LinkedList 实现 第14小组 296933284 + * LinkedList (带头结点的单链表) 实现 第14小组 296933284 * * @author Tonnyson * @@ -10,144 +14,150 @@ public class LinkedList implements List { private Node head; private int size; - + public LinkedList() { super(); this.head = new Node(); this.size = 0; } - /** - * 尾插法在链表末尾插入节点 - */ public void add(Object obj) { + addLast(obj); + } - if (head.data == null) { - head.data = obj; - head.next = null; + public void add(int index, Object obj) { + + if (index == size + 1) { + addLast(obj); } else { - - Node r = head; - while (r.next != null) - r = r.next; - + Node r = getPreNode(index); Node node = new Node(); node.data = obj; - node.next = null; - + node.next = r.next; r.next = node; + size++; } - - size++; - } + + /** - * 在指定索引位置插入节点 + * 单链表首部插入节点 + * + * @param obj 所插入节点的节点值 + * */ - public void add(int index, Object obj) { - - if (index > size) - throw new IndexOutOfBoundsException(); - - Node r = head; - for (int i = 0; i < index - 1; i++) - r = r.next; - + public void addFirst(Object obj) { Node node = new Node(); node.data = obj; - node.next = null; - node.next = r.next; - r.next = node; + + node.next = head.next; + head.next = node; size++; } /** - * 返回指定位置的值 + * 单链表尾部插入节点 + * + * @param obj 所插入节点的节点值 + * */ - public Object get(int index) { - if (index > size) - throw new IndexOutOfBoundsException(); - + public void addLast(Object obj) { + + Node node = new Node(); + node.data = obj; + node.next = null; + Node r = head; - for (int i = 0; i < index; i++) - r = r.next; + while (r.next != null) r = r.next; + + r.next = node; + + size++; - return r.data; } /** - * 删除指定位置节点并返回其值 + * 将集合中所有元素按顺序插入单链表 + * + * @param c 要插入单链表的所有元素的集合 + * */ - public Object remove(int index) { - if (index > size) - throw new IndexOutOfBoundsException(); + public void addAll(Collection c) { - Node node = new Node(); + Iterator iter = (Iterator) c.iterator(); - Node r = head; - for (int i = 0; i < index - 1; i++) - r = r.next; - - node = r.next; - r.next = node.next; - node.next = null; - size--; - return node.data; - } - - /** - * 返回链表的长度 - */ - public int size() { - return size; + while (iter.hasNext()) { + addLast(iter.next()); + } } /** - * 在链表的起始位置插入节点 - * - * @param obj + * 获取指定位置的节点值 */ - public void addFirst(Object obj) { - Node node = new Node(); - node.data = obj; - node.next = head; - head = node; - size++; + public Object get(int index) { + // rangCheck(index); + + return getPreNode(index).next.data; } /** - * 在链表尾部插入节点 - * - * @param obj + * 删除指定位置节点,并返回节点值 */ - public void addLast(Object obj) { - add(obj); + public Object remove(int index) { + rangCheck(index); + + Node r = getPreNode(index); + + Object result = r.next.data; + + r.next = r.next.next; + size--; + return result; } - + /** - * 删除链表的第一个节点 + * 删除单链表第一个节点,并返回节点值 * - * @return 所删除节点的值 + * @return 第一个节点的值 */ public Object removeFirst() { - Node node = new Node(); - node = head; - head = head.next; - size--; - - return node.data; + return remove(0); } - + /** - * 删除链表的最后一个节点 + * 删除单链表最后一个节点,并返回节点值 * - * @return 所删除节点的值 + * @return 最后一个节点的值 */ public Object removeLast() { return remove(size - 1); } + // 获取指定位置的前驱结点并返回 + private Node getPreNode(int index) { + rangCheck(index); + + if (index == 0) { + return head; + } else { + Node r = head; + + for (int i = 0; i < index; i++) + r = r.next; + + return r; + } + + } + + /** + * 返回单链表的长度 + */ + public int size() { + return size; + } + /** * 迭代器 * @@ -159,7 +169,7 @@ public Iterator iterator() { // 迭代器内部类 private class Iter implements Iterator { - int current; + int current = 0; @Override public boolean hasNext() { @@ -170,8 +180,7 @@ public boolean hasNext() { public Object next() { int i = current; - if (i >= size) - throw new IndexOutOfBoundsException(); + rangCheck(i); current++; @@ -179,10 +188,27 @@ public Object next() { } } + + /** + * 检查是否越界 + * + * @param index + */ + private void rangCheck(int index) { + if (index > size || index < 0) + throw new IndexOutOfBoundsException(); + } - // 节点类定义 private static class Node { Object data; Node next; + + public Node() { + super(); + this.data = null; + this.next = null; + } + + } -} +} \ No newline at end of file diff --git a/group14/296933284/Note/README.md b/group14/296933284/Note/README.md new file mode 100644 index 0000000000..3b92cb5667 --- /dev/null +++ b/group14/296933284/Note/README.md @@ -0,0 +1,4 @@ +锘# 2017缂栫▼鎻愰珮(Java)瀛︿範绯诲垪绗旇閾炬帴 +--- + +1. [婕皥璁$畻鏈虹粍鎴 -- 寰瀷璁$畻鏈虹殑纭欢缁勬垚 2017-02-26](http://tennyson.ren/2017/02/25/%E6%BC%AB%E8%B0%88%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%B3%BB%E7%BB%9F%E7%BB%84%E6%88%90%20--%20%E5%BE%AE%E5%9E%8B%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%9A%84%E7%A1%AC%E4%BB%B6%E7%BB%84%E6%88%90/) \ No newline at end of file diff --git a/group14/296933284/Note/test.txt b/group14/296933284/Note/test.txt new file mode 100644 index 0000000000..95d09f2b10 --- /dev/null +++ b/group14/296933284/Note/test.txt @@ -0,0 +1 @@ +hello world \ No newline at end of file diff --git a/group14/296933284/README.md b/group14/296933284/README.md new file mode 100644 index 0000000000..023abf1e46 --- /dev/null +++ b/group14/296933284/README.md @@ -0,0 +1,4 @@ +锘# 2017骞寸紪绋嬫彁楂橈紙Java锛 浣滀笟銆佺粌涔犮佹荤粨 + +[DataStructuresTest --- 鍩烘湰鏁版嵁缁撴瀯Java瀹炵幇](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/DataStructuresTest) +[Note --- 2017缂栫▼鎻愰珮(Java)瀛︿範绯诲垪绗旇閾炬帴](https://github.com/Tennysons/coding2017/tree/master/group14/296933284/Note)