From 8d011fa3948e4aa6e4a2f1b3003a83e53aec719a Mon Sep 17 00:00:00 2001 From: lirenxn Date: Sun, 26 Feb 2017 11:51:09 +1100 Subject: [PATCH 1/2] Finish Except Tree --- group02/609990377/DataStructure/.gitignore | 3 + .../coding2017/basic/ArrayList.java | 113 ++++++++++++ .../coding2017/basic/BinaryTreeNode.java | 32 ++++ .../coding2017/basic/Iterator.java | 8 + .../coding2017/basic/LinkedList.java | 163 ++++++++++++++++++ .../coding2017/basic/List.java | 9 + .../coding2017/basic/Queue.java | 30 ++++ .../coding2017/basic/Stack.java | 28 +++ 8 files changed, 386 insertions(+) create mode 100644 group02/609990377/DataStructure/.gitignore create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java create mode 100644 group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java diff --git a/group02/609990377/DataStructure/.gitignore b/group02/609990377/DataStructure/.gitignore new file mode 100644 index 0000000000..fa968c2f2b --- /dev/null +++ b/group02/609990377/DataStructure/.gitignore @@ -0,0 +1,3 @@ +/bin/ +.classpath +.project diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java new file mode 100644 index 0000000000..cfdc5fefe1 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/ArrayList.java @@ -0,0 +1,113 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.Arrays; +import java.util.InputMismatchException; +import java.util.NoSuchElementException; + +public class ArrayList implements List { + + private int size = 0; + + private Object[] elementData = new Object[10]; + + public void add(Object o){ + + //check size limit + if(size + 1 > elementData.length){ + int newlength = elementData.length * 3 / 2 + 1; + elementData = Arrays.copyOf(elementData, newlength); + } + + elementData[size++] = o; + } + + public void add(int index, Object o){ + //Check Object class + if(size >= 1){ + if(o.getClass() != elementData[0].getClass()) + throw new InputMismatchException("Index:"+index+" Size:"+size); + } + + //index Check + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + //check size limit + if(size + 1 > elementData.length){ + Object oldData[] = elementData; + int newlength = elementData.length * 3 / 2 + 1; + elementData = Arrays.copyOf(elementData, newlength); + } + + for(int i = size-1; i >= index; i-- ){ + elementData[i] = elementData[i-1]; + } + + elementData[index] = o; + + } + + public Object get(int index){ + //index Check + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + return elementData[index]; + } + + public Object remove(int index){ + //index Check + if(index >= size || index < 0){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + Object old = elementData[index]; + for(int i = index; i < size-1 ; i++ ){ + elementData[i] = elementData[i+1]; + } + elementData[--size] = null; + return old; + } + + public int size(){ + return size; + } + + public Iterator iterator(){ + return new Itr(); + } + + private class Itr implements Iterator{ + //index for next element to visit + private int cursor = 0; + + @Override + public boolean hasNext() { + return cursor != size; + } + + @Override + public Object next() { + if(cursor >= size){ + throw new NoSuchElementException(); + } + return elementData[cursor++]; + } + + @Override + public void remove() { + //Check bound + if(cursor == 0){ + throw new NoSuchElementException(); + } + + ArrayList.this.remove(--cursor); + + } + + + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java new file mode 100644 index 0000000000..74d7ae6f16 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java @@ -0,0 +1,32 @@ +package com.github.congcongcong250.coding2017.basic; + +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){ + return null; + } + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java new file mode 100644 index 0000000000..9033ad33be --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Iterator.java @@ -0,0 +1,8 @@ +package com.github.congcongcong250.coding2017.basic; + +public interface Iterator { + public boolean hasNext(); + public Object next(); + public void remove(); + +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java new file mode 100644 index 0000000000..840bb14f7c --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/LinkedList.java @@ -0,0 +1,163 @@ +package com.github.congcongcong250.coding2017.basic; + +import java.util.NoSuchElementException; + +public class LinkedList implements List { + + private Node head; + private int size; + + public LinkedList(){ + head.next = head; + head.previous = head; + size = 0; + } + + public void add(Object o){ + addLast(o); + } + + public void add(int index , Object o){ + //Check bound + if(index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + + Node nx = this.find(index); + Node pr = nx.previous; + Node in = new Node(o,pr,nx); + nx.previous = in; + pr.next = in; + size++; + } + + public Object get(int index){ + //Check bound + if(index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + return this.find(index); + } + + public Object remove(int index){ + //Check bound + if(index >= size){ + throw new IndexOutOfBoundsException("Index:"+index+" Size:"+size); + } + Node rem = this.find(index); + + Node pr = rem.previous; + Node nx = rem.next; + pr.next = nx; + nx.previous = pr; + + Object ret = rem.data; + rem.previous = null; + rem.next = null; + rem.data = null; + size--; + return ret; + } + + public int size(){ + return size; + } + + public void addFirst(Object o){ + Node nx = head.next; + Node in = new Node(o,head, nx); + head.next = in; + nx.previous = in; + size++; + } + + public void addLast(Object o){ + Node last = head.previous; + Node in = new Node(o,last,head); + last.next = in; + head.previous = in; + + size++; + } + public Object removeFirst(){ + return remove(0); + } + public Object removeLast(){ + return remove(size-1); + } + public Iterator iterator(){ + return new ListItr(); + } + + private Node find(int index){ + Node tra = head; + + //If index < size/2 + if( index < (size >> 1)){ + for(int i = 0; i <= index; i++){ + tra = tra.next; + } + }else{ + for(int i = size; i >= index; i--){ + tra = tra.previous; + } + } + return tra; + } + + private static class Node{ + Object data; + Node next; + Node previous; + + public Node(Object obj,Node pre, Node nx){ + data = obj; + next = nx; + previous = pre; + } + + + } + + private class ListItr implements Iterator{ + //Point to next node + Node cursor; + int nextIndex; + + public ListItr(){ + cursor = head.next; + nextIndex = 0; + } + + @Override + public boolean hasNext() { + return nextIndex < size; + } + + @Override + public Object next() { + Node re = cursor; + cursor = cursor.next; + nextIndex++; + return re; + } + + public Object previous() { + Node re = cursor.previous.previous; + cursor = cursor.previous; + nextIndex--; + return re; + } + + @Override + public void remove() { + //Check bound + if(nextIndex > size){ + throw new NoSuchElementException("Iterates to the end"); + } + LinkedList.this.remove(--nextIndex); + + } + + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java new file mode 100644 index 0000000000..fe05e60f37 --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/List.java @@ -0,0 +1,9 @@ +package com.github.congcongcong250.coding2017.basic; + +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(); +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java new file mode 100644 index 0000000000..2cdd329b0b --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Queue.java @@ -0,0 +1,30 @@ +package com.github.congcongcong250.coding2017.basic; + +public class Queue { + private LinkedList elementData; + + public Queue(){ + elementData = new LinkedList(); + } + + public void enQueue(Object o){ + elementData.addFirst(o); + } + + public Object deQueue(){ + Object ret = elementData.removeLast(); + return ret; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + + public boolean isEmpty(){ + return (elementData.size() == 0); + } + + public int size(){ + return elementData.size(); + } +} diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java new file mode 100644 index 0000000000..e3024202da --- /dev/null +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/Stack.java @@ -0,0 +1,28 @@ +package com.github.congcongcong250.coding2017.basic; + +public class Stack { + private LinkedList elementData = new LinkedList(); + + public Stack(){ + elementData = new LinkedList(); + } + + public void push(Object o){ + elementData.addLast(o); + } + + public Object pop(){ + Object ret = elementData.removeLast(); + return ret; + } + + public Object peek(){ + return elementData.get(elementData.size()-1); + } + public boolean isEmpty(){ + return (elementData.size() == 0); + } + public int size(){ + return elementData.size(); + } +} From 6d3967a597cb2074b80f81753ba57988b3b40dbd Mon Sep 17 00:00:00 2001 From: lirenxn Date: Sun, 26 Feb 2017 13:23:33 +1100 Subject: [PATCH 2/2] BinaryTree --- .../coding2017/basic/BinaryTreeNode.java | 44 ++++++++++++++++++- 1 file changed, 42 insertions(+), 2 deletions(-) diff --git a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java index 74d7ae6f16..fb71311dc4 100644 --- a/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java +++ b/group02/609990377/DataStructure/src/com/github/congcongcong250/coding2017/basic/BinaryTreeNode.java @@ -1,11 +1,23 @@ package com.github.congcongcong250.coding2017.basic; -public class BinaryTreeNode { +public class BinaryTreeNode >{ private Object data; private BinaryTreeNode left; private BinaryTreeNode right; + public BinaryTreeNode(){ + data = null; + left = null; + right = null; + } + + public BinaryTreeNode(Object obj){ + data = obj; + left = null; + right = null; + } + public Object getData() { return data; } @@ -26,7 +38,35 @@ public void setRight(BinaryTreeNode right) { } public BinaryTreeNode insert(Object o){ - return null; + //If is empty root + if(data == null){ + data = o; + return this; + } + + //If it is a normal root + BinaryTreeNode in; + + if(o.compareTo(data) <= 0){ + if(left == null){ + in = new BinaryTreeNode(o); + left = in; + }else{ + in = left.insert(o); + } + }else{ + if(right == null){ + in = new BinaryTreeNode(o); + right = in; + }else{ + in = right.insert(o); + } + } + + assert (in == null):"Insert error"; + return in; } + + }