Skip to content

Commit 9201623

Browse files
authored
Merge pull request eloiseh#16 from Qiujm/master
Merge pull request #1 from DonaldY/master
2 parents d73a6a5 + e05a0fb commit 9201623

File tree

14 files changed

+677
-0
lines changed

14 files changed

+677
-0
lines changed

group24/809203042/.classpath

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
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="output" path="bin"/>
6+
</classpath>

group24/809203042/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/bin/

group24/809203042/.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>809203042Learning</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: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package com.github.qq809203042.coding2017.basic.structures;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
* 根据api中arraylist的方法描述,实现一个自己的arraylist
7+
* 基于数组实现
8+
*
9+
*/
10+
public class MyArrayList implements MyList {
11+
// 定义一个私有数组,初始长度为10
12+
private Object[] elementData = new Object[10];
13+
// 定义变量记录ArrayList的长度
14+
private int size = 0;
15+
16+
// 获取指定索引上的元素的值
17+
@Override
18+
public Object get(int index) {
19+
if (index >= 0 && index < size) {
20+
return elementData[index];
21+
} else {
22+
throw new IndexOutOfBoundsException("查询的索引不存在");
23+
}
24+
}
25+
// 向列表尾部添加元素
26+
@Override
27+
public boolean add(Object obj) {
28+
if (size >= elementData.length) {// 若size大于等于现有数组长度,则将数组扩容后再进行操作
29+
elementData = Arrays.copyOf(elementData, elementData.length * 2);
30+
}
31+
elementData[size] = obj;
32+
size++;
33+
return true;
34+
// 什么时候会返回false,如何返回false?
35+
}
36+
// 向列表指定位置添加元素
37+
@Override
38+
public boolean add(Object obj,int index) {
39+
if (size >= elementData.length) {// 若size大于等于现有数组长度,则将数组扩容后再进行操作
40+
elementData = Arrays.copyOf(elementData, elementData.length * 2);
41+
}
42+
// 将从index开始的所有元素向后移动一位
43+
moveBackward(elementData,index,size-1,1);
44+
// 将元素添加到指定位置
45+
elementData[index] = obj;
46+
size++;
47+
return true;
48+
}
49+
50+
// 删除指定位置的元素
51+
@Override
52+
public Object remove(int index) {
53+
if(size == 0){
54+
throw new IndexOutOfBoundsException("列表为空");
55+
}
56+
if(index < 0 || index >= size){
57+
throw new IndexOutOfBoundsException("想要删除的元素索引不存在");
58+
}
59+
Object removeElement = elementData[index];
60+
moveForward(elementData,index+1,size-1,1);
61+
// 最后一位置为null;!!
62+
elementData[size-1] = null;
63+
size--;
64+
return removeElement;
65+
}
66+
67+
@Override
68+
public int size() {
69+
70+
return size;
71+
}
72+
// 判断列表是否为空
73+
@Override
74+
public boolean isEmpty() {
75+
76+
return size == 0;
77+
}
78+
// 将数组从startPos位置到endPos位置的元素向后移动step步长
79+
private void moveBackward(Object[] elementData, int startPos, int endPos, int step) {
80+
for(int i = endPos + step; i >= startPos + step; i--){
81+
elementData[i] = elementData[i-step];
82+
}
83+
84+
}
85+
// 将数组从startPos位置到endPos位置的元素向前移动step步长
86+
private void moveForward(Object[] elementData, int startPos, int endPos, int step) {
87+
for(int i = startPos - step; i <= endPos - step; i++){
88+
elementData[i] = elementData[i+step];
89+
}
90+
91+
}
92+
@Override
93+
public String toString() {
94+
// return Arrays.toString(elementData);
95+
Object[] myArrayList = Arrays.copyOf(elementData, size);
96+
return Arrays.toString(myArrayList);
97+
98+
}
99+
100+
101+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package com.github.qq809203042.coding2017.basic.structures;
2+
3+
4+
/*
5+
* 实现二叉树,
6+
* 只存储int类型
7+
* 内部类代表每个节点:每个节点含有一个键,一个值,一条左链接,一条右链接
8+
*
9+
* 实现插入值操作:当root为空时插在root上:确定某一节点上是否为空
10+
*
11+
*/
12+
public class MyBinaryTree {
13+
private BinaryTreeNode root ;
14+
private int size;
15+
16+
// 构造函数
17+
public MyBinaryTree(){
18+
19+
}
20+
21+
public boolean getValue(Integer key){
22+
return getValue(root,key);
23+
}
24+
25+
private boolean getValue(BinaryTreeNode node,Integer key){
26+
if(node == null){//如果为空,则为空,没有该元素
27+
return false;
28+
}
29+
Integer value = node.getValue();
30+
if(value == key){//找到
31+
return true;
32+
}
33+
else if(value.compareTo(key) > 0){//如果小于该节点对象,比较左节点
34+
return getValue(node.left,key);
35+
}else{
36+
return getValue(node.right,key);//如果小于该节点对象,比较右节点
37+
}
38+
39+
}
40+
41+
public void add(Integer key){
42+
root = add(root, key);
43+
44+
}
45+
46+
private BinaryTreeNode add(BinaryTreeNode node, Integer key) {
47+
if(node == null){//若没有该节点,则创建并添加数据至节点中
48+
node = new BinaryTreeNode(key);
49+
size++;
50+
return node;
51+
}
52+
Integer value = node.getValue();
53+
if(value.compareTo(key) > 0){
54+
node.setLeft(add(node.left,key));
55+
56+
}else if(value.compareTo(key) < 0){
57+
node.setRight(add(node.right,key));
58+
59+
}
60+
return node;
61+
}
62+
63+
public int size(){
64+
return size;
65+
}
66+
67+
68+
// 前序遍历
69+
public void preOrder(){
70+
preOrder(root);
71+
System.out.println();
72+
}
73+
// 中序遍历
74+
public void midOrder(){
75+
midOrder(root);
76+
System.out.println();
77+
}
78+
// 后序遍历
79+
public void aftOrder(){
80+
aftOrder(root);
81+
System.out.println();
82+
}
83+
84+
// 前序遍历
85+
private void preOrder(BinaryTreeNode node){
86+
if(node == null){
87+
return;
88+
}
89+
System.out.print(node.value + " ");
90+
preOrder(node.left);
91+
preOrder(node.right);
92+
93+
}
94+
// 中序遍历
95+
private void midOrder(BinaryTreeNode node){
96+
if(node == null){
97+
return;
98+
}
99+
midOrder(node.left);
100+
System.out.print(node.value + " ");
101+
midOrder(node.right);
102+
103+
}
104+
// 后序遍历
105+
private void aftOrder(BinaryTreeNode node){
106+
if(node == null){
107+
return;
108+
}
109+
aftOrder(node.left);
110+
aftOrder(node.right);
111+
System.out.print(node.value + " ");
112+
113+
}
114+
115+
116+
117+
// 二叉树的节点对象:每个节点含有一个键,一个值,一条左链接,一条右链接和一个节点计数器
118+
private static class BinaryTreeNode {
119+
private Integer value;
120+
121+
private BinaryTreeNode left;
122+
private BinaryTreeNode right;
123+
// 构造函数
124+
BinaryTreeNode(Integer value){
125+
this.value = value;
126+
}
127+
128+
129+
130+
public Integer getValue() {
131+
return value;
132+
}
133+
public void setValue(Integer value) {
134+
this.value = value;
135+
}
136+
137+
public BinaryTreeNode getLeft() {
138+
return left;
139+
}
140+
public void setLeft(BinaryTreeNode left) {
141+
this.left = left;
142+
}
143+
public BinaryTreeNode getRight() {
144+
return right;
145+
}
146+
public void setRight(BinaryTreeNode right) {
147+
this.right = right;
148+
}
149+
150+
public BinaryTreeNode insert(Integer value) {
151+
this.value = value;
152+
return null;
153+
}
154+
155+
}
156+
}

0 commit comments

Comments
 (0)