Skip to content

Commit 9135105

Browse files
author
Rong Huang
authored
Merge pull request onlyliuxin#16 from HarryHook/master
Submit my first code
2 parents 564f89a + 6a0535d commit 9135105

16 files changed

+1027
-0
lines changed

group02/727171008/.classpath

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
6+
<classpathentry kind="output" path="bin"/>
7+
</classpath>

group02/727171008/.gitignore

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/bin/
2+
.idea/workspace.xml
3+
.idea/dictionaries/myj.xml
4+
.idea/
5+
.DS_Store
6+
*.classpath
7+
*.project
8+
.settings
9+
.project
10+
.target
11+
.classpath
12+
**/.settings
13+
**/.classpath
14+
**/.eclipse
15+
**/target/
16+
target/
17+
bin/
18+
.svn
19+
*.iml

group02/727171008/.project

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>727171008Learning</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.github.HarryHook.coding2017.basic;
2+
3+
import org.junit.Before;
4+
import com.github.HarryHook.coding2017.basic.MyArrayList;
5+
6+
7+
public class ArrayListTest extends ListTest {
8+
9+
@Before
10+
public void setUpArrayList()
11+
{
12+
aList = new MyArrayList();
13+
}
14+
15+
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*
2+
* Created by Harry 2017-2-23 10:50:39
3+
* 实现二叉树,并按二叉查找树插入节点
4+
*
5+
*/
6+
package com.github.HarryHook.coding2017.basic;
7+
8+
public class BinaryTreeNode
9+
{
10+
private Integer data;
11+
private BinaryTreeNode left;
12+
private BinaryTreeNode right;
13+
14+
//中序遍历二叉树
15+
public void inOrder(BinaryTreeNode node)
16+
{
17+
if(node != null)
18+
{
19+
inOrder(node.left);
20+
System.out.print(" " + node.data);
21+
inOrder(node.right);
22+
}
23+
}
24+
25+
//获取给节点的值
26+
public Integer getData()
27+
{
28+
return data;
29+
}
30+
//给一个节点赋值
31+
public void setData(Integer data)
32+
{
33+
this.data = data;
34+
}
35+
//获取左节点
36+
public BinaryTreeNode getLeft()
37+
{
38+
return left;
39+
}
40+
//指定左节点
41+
public void setLeft(BinaryTreeNode left)
42+
{
43+
this.left = left;
44+
}
45+
46+
//获取右节点
47+
public BinaryTreeNode getRight()
48+
{
49+
return right;
50+
}
51+
//指定右节点
52+
public void setRight(BinaryTreeNode right)
53+
{
54+
this.right = right;
55+
}
56+
57+
//在二叉树中插入一个节点,需要判断
58+
public BinaryTreeNode insert(Integer obj)
59+
{
60+
// 新增节点
61+
BinaryTreeNode newNode = new BinaryTreeNode();
62+
// 当前节点,保留根的值
63+
BinaryTreeNode current = this;
64+
// 上个节点
65+
BinaryTreeNode parent = null;
66+
// 如果根节点为空
67+
if (current.data == null)
68+
{
69+
newNode.setData(obj);
70+
newNode.setLeft(null);
71+
newNode.setRight(null);
72+
return newNode;
73+
}else
74+
{
75+
while (true)
76+
{
77+
parent = current;
78+
if (obj < current.data)
79+
{
80+
current = current.left;
81+
if (current == null)
82+
{
83+
newNode.setData(obj);
84+
newNode.setLeft(null);
85+
newNode.setRight(null);
86+
parent.left = newNode;
87+
return newNode;
88+
}
89+
} else
90+
{
91+
current = current.right;
92+
if (current == null)
93+
{
94+
newNode.setData(obj);
95+
newNode.setLeft(null);
96+
newNode.setRight(null);
97+
parent.right = newNode;
98+
return newNode;
99+
}
100+
}
101+
}
102+
}
103+
}
104+
105+
public static void main(String[] args)
106+
{
107+
BinaryTreeNode BTN = new BinaryTreeNode();
108+
109+
BTN = BTN.insert(5);
110+
System.out.print(BTN.getData() + " ");
111+
System.out.print(BTN.insert(2).getData() + " ");
112+
System.out.print(BTN.insert(1).getData() + " ");
113+
System.out.print(BTN.insert(4).getData() + " ");
114+
System.out.print(BTN.insert(6).getData() + " ");
115+
System.out.print(BTN.insert(8).getData() + " ");
116+
System.out.println("");
117+
System.out.println("中序遍历二叉树: ");
118+
BTN.inOrder(BTN);
119+
}
120+
121+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.github.HarryHook.coding2017.basic;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import org.junit.Before;
6+
import org.junit.Test;
7+
8+
import com.github.HarryHook.coding2017.basic.BinaryTreeNode;
9+
10+
public class BinaryTreeNodeTest
11+
{
12+
13+
BinaryTreeNode binaryTreeNode;
14+
15+
@Before
16+
public void setUpBinaryTreeNode()
17+
{
18+
binaryTreeNode = new BinaryTreeNode();
19+
}
20+
21+
@Test
22+
public void testBinaryTreeNodeFunctional()
23+
{
24+
binaryTreeNode = binaryTreeNode.insert(4);
25+
binaryTreeNode.insert(1);
26+
binaryTreeNode.insert(3);
27+
binaryTreeNode.insert(5);
28+
binaryTreeNode.insert(2);
29+
30+
assertEquals(true, 4 == binaryTreeNode.getData());
31+
assertEquals(true, 1 == binaryTreeNode.getLeft().getData());
32+
assertEquals(true, 5 == binaryTreeNode.getRight().getData());
33+
assertEquals(true, 3 == binaryTreeNode.getLeft().getRight().getData());
34+
assertEquals(true, 2 == binaryTreeNode.getLeft().getRight().getLeft().getData());
35+
36+
//节点为空 说明值没有插进去
37+
binaryTreeNode.inOrder(binaryTreeNode);
38+
}
39+
40+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.github.HarryHook.coding2017.basic;
2+
3+
public interface Iterator
4+
{
5+
public boolean hasNext();
6+
public Object next();
7+
}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
package com.github.HarryHook.coding2017.basic;
2+
3+
import static org.junit.Assert.*;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
import com.github.HarryHook.coding2017.basic.MyLinkedList;
7+
8+
public class LinkedListTest extends ListTest{
9+
10+
private MyLinkedList aLinkedList;
11+
12+
@Before
13+
public void setUpLinkedList() {
14+
aList = new MyLinkedList();
15+
aLinkedList = new MyLinkedList();
16+
}
17+
18+
@Test
19+
public void testAddFirst() {
20+
aLinkedList.addFirst(5);
21+
assertEquals(5, aLinkedList.get(0));
22+
23+
aLinkedList.addFirst(6);
24+
assertEquals(6, aLinkedList.get(0));
25+
assertEquals(5, aLinkedList.get(1));
26+
assertEquals(2, aLinkedList.size());
27+
}
28+
29+
@Test
30+
public void testAddLast() {
31+
aLinkedList.addLast("hello");
32+
assertEquals("hello", aLinkedList.get(0));
33+
34+
aLinkedList.addLast("world");
35+
assertEquals("hello", aLinkedList.get(0));
36+
assertEquals("world", aLinkedList.get(1));
37+
assertEquals(2, aLinkedList.size());
38+
}
39+
40+
@Test
41+
public void testRemoveFirst() {
42+
aLinkedList.addLast("hello");
43+
aLinkedList.addLast("world");
44+
45+
aLinkedList.removeFirst();
46+
assertEquals("world", aLinkedList.get(0));
47+
assertEquals(1, aLinkedList.size());
48+
49+
aLinkedList.removeFirst();
50+
assertEquals(0, aLinkedList.size());
51+
}
52+
53+
@Test
54+
public void testRemoveLast() {
55+
aLinkedList.addFirst("world");
56+
aLinkedList.addFirst("hello");
57+
58+
aLinkedList.removeLast();
59+
assertEquals("hello", aLinkedList.get(0));
60+
assertEquals(1, aLinkedList.size());
61+
62+
aLinkedList.removeLast();
63+
assertEquals(0, aLinkedList.size());
64+
}
65+
66+
@Test
67+
public void testLinkedListFunctional() {
68+
for (int i=1; i<4; i++) {
69+
aLinkedList.add(i); // [1,2,3]
70+
}
71+
aLinkedList.remove(1); // [1,3]
72+
73+
aLinkedList.add(1, 0); // [1,0,3]
74+
for (int i=4; i<6; i++) {
75+
aLinkedList.addFirst(i); // [5, 4, 1, 0, 3]
76+
}
77+
assertEquals(5, aLinkedList.size());
78+
assertEquals(5, aLinkedList.get(0));
79+
assertEquals(1, aLinkedList.get(2));
80+
assertEquals(0, aLinkedList.get(3));
81+
82+
aLinkedList.remove(3); // [5, 4, 1, 3]
83+
assertEquals(3, aLinkedList.get(aLinkedList.size()-1));
84+
aLinkedList.removeLast(); // [5, 4, 1]
85+
assertEquals(1, aLinkedList.get(aLinkedList.size()-1));
86+
aLinkedList.removeFirst(); // [4,1]
87+
88+
assertEquals(4, aLinkedList.get(0));
89+
assertEquals(1, aLinkedList.get(1));
90+
assertEquals(2, aLinkedList.size());
91+
}
92+
93+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.github.HarryHook.coding2017.basic;
2+
3+
4+
public interface List
5+
{
6+
public void add(Object o);
7+
public void add(int index, Object o);
8+
public Object get(int index);
9+
public Object remove(int index);
10+
public int size();
11+
12+
public Iterator iterator();
13+
}
14+

0 commit comments

Comments
 (0)