forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from Qiujm/master
Merge pull request #1 from DonaldY/master
- Loading branch information
Showing
14 changed files
with
677 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<classpath> | ||
<classpathentry kind="src" path="src"/> | ||
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
<classpathentry kind="output" path="bin"/> | ||
</classpath> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<projectDescription> | ||
<name>809203042Learning</name> | ||
<comment></comment> | ||
<projects> | ||
</projects> | ||
<buildSpec> | ||
<buildCommand> | ||
<name>org.eclipse.jdt.core.javabuilder</name> | ||
<arguments> | ||
</arguments> | ||
</buildCommand> | ||
</buildSpec> | ||
<natures> | ||
<nature>org.eclipse.jdt.core.javanature</nature> | ||
</natures> | ||
</projectDescription> |
101 changes: 101 additions & 0 deletions
101
group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyArrayList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package com.github.qq809203042.coding2017.basic.structures; | ||
|
||
import java.util.Arrays; | ||
|
||
/* | ||
* 根据api中arraylist的方法描述,实现一个自己的arraylist | ||
* 基于数组实现 | ||
* | ||
*/ | ||
public class MyArrayList implements MyList { | ||
// 定义一个私有数组,初始长度为10 | ||
private Object[] elementData = new Object[10]; | ||
// 定义变量记录ArrayList的长度 | ||
private int size = 0; | ||
|
||
// 获取指定索引上的元素的值 | ||
@Override | ||
public Object get(int index) { | ||
if (index >= 0 && index < size) { | ||
return elementData[index]; | ||
} else { | ||
throw new IndexOutOfBoundsException("查询的索引不存在"); | ||
} | ||
} | ||
// 向列表尾部添加元素 | ||
@Override | ||
public boolean add(Object obj) { | ||
if (size >= elementData.length) {// 若size大于等于现有数组长度,则将数组扩容后再进行操作 | ||
elementData = Arrays.copyOf(elementData, elementData.length * 2); | ||
} | ||
elementData[size] = obj; | ||
size++; | ||
return true; | ||
// 什么时候会返回false,如何返回false? | ||
} | ||
// 向列表指定位置添加元素 | ||
@Override | ||
public boolean add(Object obj,int index) { | ||
if (size >= elementData.length) {// 若size大于等于现有数组长度,则将数组扩容后再进行操作 | ||
elementData = Arrays.copyOf(elementData, elementData.length * 2); | ||
} | ||
// 将从index开始的所有元素向后移动一位 | ||
moveBackward(elementData,index,size-1,1); | ||
// 将元素添加到指定位置 | ||
elementData[index] = obj; | ||
size++; | ||
return true; | ||
} | ||
|
||
// 删除指定位置的元素 | ||
@Override | ||
public Object remove(int index) { | ||
if(size == 0){ | ||
throw new IndexOutOfBoundsException("列表为空"); | ||
} | ||
if(index < 0 || index >= size){ | ||
throw new IndexOutOfBoundsException("想要删除的元素索引不存在"); | ||
} | ||
Object removeElement = elementData[index]; | ||
moveForward(elementData,index+1,size-1,1); | ||
// 最后一位置为null;!! | ||
elementData[size-1] = null; | ||
size--; | ||
return removeElement; | ||
} | ||
|
||
@Override | ||
public int size() { | ||
|
||
return size; | ||
} | ||
// 判断列表是否为空 | ||
@Override | ||
public boolean isEmpty() { | ||
|
||
return size == 0; | ||
} | ||
// 将数组从startPos位置到endPos位置的元素向后移动step步长 | ||
private void moveBackward(Object[] elementData, int startPos, int endPos, int step) { | ||
for(int i = endPos + step; i >= startPos + step; i--){ | ||
elementData[i] = elementData[i-step]; | ||
} | ||
|
||
} | ||
// 将数组从startPos位置到endPos位置的元素向前移动step步长 | ||
private void moveForward(Object[] elementData, int startPos, int endPos, int step) { | ||
for(int i = startPos - step; i <= endPos - step; i++){ | ||
elementData[i] = elementData[i+step]; | ||
} | ||
|
||
} | ||
@Override | ||
public String toString() { | ||
// return Arrays.toString(elementData); | ||
Object[] myArrayList = Arrays.copyOf(elementData, size); | ||
return Arrays.toString(myArrayList); | ||
|
||
} | ||
|
||
|
||
} |
156 changes: 156 additions & 0 deletions
156
group24/809203042/src/com/github/qq809203042/coding2017/basic/structures/MyBinaryTree.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
package com.github.qq809203042.coding2017.basic.structures; | ||
|
||
|
||
/* | ||
* 实现二叉树, | ||
* 只存储int类型 | ||
* 内部类代表每个节点:每个节点含有一个键,一个值,一条左链接,一条右链接 | ||
* | ||
* 实现插入值操作:当root为空时插在root上:确定某一节点上是否为空 | ||
* | ||
*/ | ||
public class MyBinaryTree { | ||
private BinaryTreeNode root ; | ||
private int size; | ||
|
||
// 构造函数 | ||
public MyBinaryTree(){ | ||
|
||
} | ||
|
||
public boolean getValue(Integer key){ | ||
return getValue(root,key); | ||
} | ||
|
||
private boolean getValue(BinaryTreeNode node,Integer key){ | ||
if(node == null){//如果为空,则为空,没有该元素 | ||
return false; | ||
} | ||
Integer value = node.getValue(); | ||
if(value == key){//找到 | ||
return true; | ||
} | ||
else if(value.compareTo(key) > 0){//如果小于该节点对象,比较左节点 | ||
return getValue(node.left,key); | ||
}else{ | ||
return getValue(node.right,key);//如果小于该节点对象,比较右节点 | ||
} | ||
|
||
} | ||
|
||
public void add(Integer key){ | ||
root = add(root, key); | ||
|
||
} | ||
|
||
private BinaryTreeNode add(BinaryTreeNode node, Integer key) { | ||
if(node == null){//若没有该节点,则创建并添加数据至节点中 | ||
node = new BinaryTreeNode(key); | ||
size++; | ||
return node; | ||
} | ||
Integer value = node.getValue(); | ||
if(value.compareTo(key) > 0){ | ||
node.setLeft(add(node.left,key)); | ||
|
||
}else if(value.compareTo(key) < 0){ | ||
node.setRight(add(node.right,key)); | ||
|
||
} | ||
return node; | ||
} | ||
|
||
public int size(){ | ||
return size; | ||
} | ||
|
||
|
||
// 前序遍历 | ||
public void preOrder(){ | ||
preOrder(root); | ||
System.out.println(); | ||
} | ||
// 中序遍历 | ||
public void midOrder(){ | ||
midOrder(root); | ||
System.out.println(); | ||
} | ||
// 后序遍历 | ||
public void aftOrder(){ | ||
aftOrder(root); | ||
System.out.println(); | ||
} | ||
|
||
// 前序遍历 | ||
private void preOrder(BinaryTreeNode node){ | ||
if(node == null){ | ||
return; | ||
} | ||
System.out.print(node.value + " "); | ||
preOrder(node.left); | ||
preOrder(node.right); | ||
|
||
} | ||
// 中序遍历 | ||
private void midOrder(BinaryTreeNode node){ | ||
if(node == null){ | ||
return; | ||
} | ||
midOrder(node.left); | ||
System.out.print(node.value + " "); | ||
midOrder(node.right); | ||
|
||
} | ||
// 后序遍历 | ||
private void aftOrder(BinaryTreeNode node){ | ||
if(node == null){ | ||
return; | ||
} | ||
aftOrder(node.left); | ||
aftOrder(node.right); | ||
System.out.print(node.value + " "); | ||
|
||
} | ||
|
||
|
||
|
||
// 二叉树的节点对象:每个节点含有一个键,一个值,一条左链接,一条右链接和一个节点计数器 | ||
private static class BinaryTreeNode { | ||
private Integer value; | ||
|
||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
// 构造函数 | ||
BinaryTreeNode(Integer value){ | ||
this.value = value; | ||
} | ||
|
||
|
||
|
||
public Integer getValue() { | ||
return value; | ||
} | ||
public void setValue(Integer value) { | ||
this.value = value; | ||
} | ||
|
||
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 value) { | ||
this.value = value; | ||
return null; | ||
} | ||
|
||
} | ||
} |
Oops, something went wrong.