Skip to content

Commit

Permalink
Merge pull request #8 from HarryHook/master
Browse files Browse the repository at this point in the history
Master
  • Loading branch information
Rong Huang authored Feb 26, 2017
2 parents b33c76f + 0275882 commit e62e923
Show file tree
Hide file tree
Showing 17 changed files with 1,163 additions and 6 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,22 @@ hs_err_pid*
#ide config
.metadata
.recommenders
/bin/
.idea/workspace.xml
.idea/dictionaries/myj.xml
.idea/
.DS_Store
*.classpath
*.project
.settings
.project
.target
.classpath
**/.settings
**/.classpath
**/.eclipse
**/target/
target/
bin/
.svn
*.iml
36 changes: 36 additions & 0 deletions group02/727171008/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/bin/
*.class

# Mobile Tools for Java (J2ME)
.mtj.tmp/

# Package Files #
*.jar
*.war
*.ear

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

#ide config
.metadata
.recommenders
/bin/
.idea/workspace.xml
.idea/dictionaries/myj.xml
.idea/
.DS_Store
*.classpath
*.project
.settings
.project
.target
.classpath
**/.settings
**/.classpath
**/.eclipse
**/target/
target/
bin/
.svn
*.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.HarryHook.coding2017.basic;

import org.junit.Before;
import com.github.HarryHook.coding2017.basic.MyArrayList;


public class ArrayListTest extends ListTest {

@Before
public void setUpArrayList()
{
aList = new MyArrayList();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*
* Created by Harry 2017-2-23 10:50:39
* 实现二叉树,并按二叉查找树插入节点
* 能实现基本的功能,但测试时存在问题,传进去的数据为空 2017-2-2523:49:43
*/
package com.github.HarryHook.coding2017.basic;

public class BinaryTreeNode
{
private Integer data;
private BinaryTreeNode left;
private BinaryTreeNode right;

//中序遍历二叉树
public void inOrder(BinaryTreeNode node)
{
if(node != null)
{
inOrder(node.left);
System.out.print(" " + node.data);
inOrder(node.right);
}
}

//获取给节点的值
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;
}

//在二叉树中插入一个节点,需要判断
public BinaryTreeNode insert(Integer obj)
{
// 新增节点
BinaryTreeNode newNode = new BinaryTreeNode();
// 当前节点,保留根的值
BinaryTreeNode current = this;
// 上个节点
BinaryTreeNode parent = null;
// 如果根节点为空
if (current.data == null)
{
newNode.setData(obj);
newNode.setLeft(null);
newNode.setRight(null);
return newNode;
}else
{
while (true)
{
parent = current;
if (obj < current.data)
{
current = current.left;
if (current == null)
{
newNode.setData(obj);
newNode.setLeft(null);
newNode.setRight(null);
parent.left = newNode;
return newNode;
}
} else
{
current = current.right;
if (current == null)
{
newNode.setData(obj);
newNode.setLeft(null);
newNode.setRight(null);
parent.right = newNode;
return newNode;
}
}
}
}
}

public static void main(String[] args)
{
BinaryTreeNode BTN = new BinaryTreeNode();

BTN = BTN.insert(5);
System.out.print(BTN.getData() + " ");
System.out.print(BTN.insert(2).getData() + " ");
System.out.print(BTN.insert(1).getData() + " ");
System.out.print(BTN.insert(4).getData() + " ");
System.out.print(BTN.insert(6).getData() + " ");
System.out.print(BTN.insert(8).getData() + " ");
System.out.println("");
System.out.println("中序遍历二叉树: ");
BTN.inOrder(BTN);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.github.HarryHook.coding2017.basic;

import static org.junit.Assert.assertEquals;

import org.junit.Before;
import org.junit.Test;

import com.github.HarryHook.coding2017.basic.BinaryTreeNode;

public class BinaryTreeNodeTest
{

BinaryTreeNode binaryTreeNode;

@Before
public void setUpBinaryTreeNode()
{
binaryTreeNode = new BinaryTreeNode();
}

@Test
public void testBinaryTreeNodeFunctional()
{
binaryTreeNode = binaryTreeNode.insert(4);
binaryTreeNode.insert(1);
binaryTreeNode.insert(3);
binaryTreeNode.insert(5);
binaryTreeNode.insert(2);

assertEquals(true, 4 == binaryTreeNode.getData());
assertEquals(true, 1 == binaryTreeNode.getLeft().getData());
assertEquals(true, 5 == binaryTreeNode.getRight().getData());
assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData());
assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData());

//节点为空 说明值没有插进去
binaryTreeNode.inOrder(binaryTreeNode);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.github.HarryHook.coding2017.basic;

public interface Iterator
{
public boolean hasNext();
public Object next();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package com.github.HarryHook.coding2017.basic;

import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import com.github.HarryHook.coding2017.basic.MyLinkedList;

public class LinkedListTest extends ListTest{

private MyLinkedList aLinkedList;

@Before
public void setUpLinkedList() {
aList = new MyLinkedList();
aLinkedList = new MyLinkedList();
}

@Test
public void testAddFirst() {
aLinkedList.addFirst(5);
assertEquals(5, aLinkedList.get(0));

aLinkedList.addFirst(6);
assertEquals(6, aLinkedList.get(0));
assertEquals(5, aLinkedList.get(1));
assertEquals(2, aLinkedList.size());
}

@Test
public void testAddLast() {
aLinkedList.addLast("hello");
assertEquals("hello", aLinkedList.get(0));

aLinkedList.addLast("world");
assertEquals("hello", aLinkedList.get(0));
assertEquals("world", aLinkedList.get(1));
assertEquals(2, aLinkedList.size());
}

@Test
public void testRemoveFirst() {
aLinkedList.addLast("hello");
aLinkedList.addLast("world");

aLinkedList.removeFirst();
assertEquals("world", aLinkedList.get(0));
assertEquals(1, aLinkedList.size());

aLinkedList.removeFirst();
assertEquals(0, aLinkedList.size());
}

@Test
public void testRemoveLast() {
aLinkedList.addFirst("world");
aLinkedList.addFirst("hello");

aLinkedList.removeLast();
assertEquals("hello", aLinkedList.get(0));
assertEquals(1, aLinkedList.size());

aLinkedList.removeLast();
assertEquals(0, aLinkedList.size());
}

@Test
public void testLinkedListFunctional() {
for (int i=1; i<4; i++) {
aLinkedList.add(i); // [1,2,3]
}
aLinkedList.remove(1); // [1,3]

aLinkedList.add(1, 0); // [1,0,3]
for (int i=4; i<6; i++) {
aLinkedList.addFirst(i); // [5, 4, 1, 0, 3]
}
assertEquals(5, aLinkedList.size());
assertEquals(5, aLinkedList.get(0));
assertEquals(1, aLinkedList.get(2));
assertEquals(0, aLinkedList.get(3));

aLinkedList.remove(3); // [5, 4, 1, 3]
assertEquals(3, aLinkedList.get(aLinkedList.size()-1));
aLinkedList.removeLast(); // [5, 4, 1]
assertEquals(1, aLinkedList.get(aLinkedList.size()-1));
aLinkedList.removeFirst(); // [4,1]

assertEquals(4, aLinkedList.get(0));
assertEquals(1, aLinkedList.get(1));
assertEquals(2, aLinkedList.size());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.github.HarryHook.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();

public Iterator iterator();
}

Loading

0 comments on commit e62e923

Please sign in to comment.