Skip to content

Commit e62c2fc

Browse files
authored
Merge pull request onlyliuxin#22 from txp-reps/master
基础数据结构作业
2 parents 59783e0 + cefbf6f commit e62c2fc

File tree

12 files changed

+622
-0
lines changed

12 files changed

+622
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
2+
# Created by https://www.gitignore.io/api/eclipse,intellij,java
3+
4+
### Eclipse ###
5+
target/
6+
.metadata
7+
bin/
8+
tmp/
9+
*.tmp
10+
*.bak
11+
*.swp
12+
*~.nib
13+
local.properties
14+
.settings/
15+
.loadpath
16+
.recommenders
17+
18+
# Eclipse Core
19+
.project
20+
21+
# External tool builders
22+
.externalToolBuilders/
23+
24+
# Locally stored "Eclipse launch configurations"
25+
*.launch
26+
27+
# PyDev specific (Python IDE for Eclipse)
28+
*.pydevproject
29+
30+
# CDT-specific (C/C++ Development Tooling)
31+
.cproject
32+
33+
# JDT-specific (Eclipse Java Development Tools)
34+
.classpath
35+
36+
# Java annotation processor (APT)
37+
.factorypath
38+
39+
# PDT-specific (PHP Development Tools)
40+
.buildpath
41+
42+
# sbteclipse plugin
43+
.target
44+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.sunline</groupId>
6+
<artifactId>project_basic_001</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>project_basic_001</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>junit</groupId>
20+
<artifactId>junit</artifactId>
21+
<version>4.10</version>
22+
<scope>test</scope>
23+
</dependency>
24+
</dependencies>
25+
</project>
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package com.coding.basic;
2+
3+
/**
4+
* @ClassName: ArrayList
5+
* @Description: 自增长数组
6+
* @author: tangxp
7+
* @date: 2017年2月23日 下午10:43:03
8+
*/
9+
public class ArrayList implements List {
10+
11+
private final int step = 10;
12+
private Object elementData[] = new Object[100];
13+
private int size = 0 ;
14+
15+
16+
/**
17+
* @Title: add
18+
* @Description: TODO
19+
* @param o , elements of this ArrayList
20+
* @see com.coding.basic.List#add(java.lang.Object)
21+
*/
22+
public void add(Object o) {
23+
add(size,o);
24+
}
25+
26+
27+
/**
28+
* @Title: add
29+
* @Description: TODO
30+
* @param index
31+
* @param o
32+
* @see com.coding.basic.List#add(int, java.lang.Object)
33+
*/
34+
public void add(int index, Object o) {
35+
if(index < 0 || index> size) {
36+
throw new IllegalArgumentException("下标越界");
37+
}
38+
39+
if(null == o) {
40+
throw new IllegalArgumentException("元素不能为空");
41+
}
42+
43+
if(this.checkOutOfBounds()) {
44+
this.autoGrow(this.step);
45+
}
46+
47+
int i = size;
48+
while(i>index) {
49+
elementData[i] = elementData[--i];
50+
}
51+
addDriect(i, o);
52+
}
53+
54+
55+
public Object get(int index) {
56+
if(index < 0 || index>= size) {
57+
throw new IllegalArgumentException("下标越界");
58+
}
59+
60+
return elementData[index];
61+
}
62+
63+
public Object remove(int index) {
64+
if(index < 0 || index>= size) {
65+
throw new IllegalArgumentException("下标越界");
66+
}
67+
68+
Object o = elementData[index];
69+
while (index<size-1) {
70+
elementData[index] = elementData[++index];
71+
}
72+
elementData[size] = null;
73+
this.size--;
74+
return o;
75+
}
76+
77+
public int size() {
78+
return this.size;
79+
}
80+
81+
82+
/**
83+
* @param growSize
84+
* 扩展elementData数组growSize大小
85+
*/
86+
private void autoGrow(int growSize) {
87+
if (elementData.length>this.size) {
88+
return;
89+
}
90+
Object newElementData[] = new Object[elementData.length+growSize];
91+
System.arraycopy(elementData, 0, newElementData, 0, elementData.length);
92+
elementData = newElementData;
93+
return;
94+
}
95+
96+
private void addDriect(int index, Object o) {
97+
elementData[index] = o;
98+
this.size++;
99+
}
100+
101+
@Override
102+
public String toString() {
103+
StringBuilder sb = new StringBuilder("[");
104+
for(int i =0;i<this.size;i++) {
105+
sb.append(elementData[i].toString());
106+
sb.append(",");
107+
}
108+
sb.replace(sb.length()-1, sb.length(), "]");
109+
return sb.toString();
110+
}
111+
112+
private boolean checkOutOfBounds() {
113+
if (elementData.length>this.size) {
114+
return false;
115+
}else {
116+
return true;
117+
}
118+
}
119+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.coding.basic;
2+
3+
4+
public class BinaryTreeNode <T extends Comparable<T>> {
5+
6+
private T data;
7+
private BinaryTreeNode<T> left;
8+
private BinaryTreeNode <T> right;
9+
10+
public T getData() {
11+
return data;
12+
}
13+
public void setData(T data) {
14+
this.data = data;
15+
}
16+
public BinaryTreeNode<T> getLeft() {
17+
return left;
18+
}
19+
public void setLeft(BinaryTreeNode<T> left) {
20+
this.left = left;
21+
}
22+
public BinaryTreeNode<T> getRight() {
23+
return right;
24+
}
25+
public void setRight(BinaryTreeNode<T> right) {
26+
this.right = right;
27+
}
28+
29+
public BinaryTreeNode<T> insert(T o){
30+
return insert(this,o);
31+
}
32+
33+
34+
public BinaryTreeNode<T> insert(BinaryTreeNode<T> bt, T o) {
35+
BinaryTreeNode<T> insertedNode = null;
36+
if(null == this.data) {
37+
this.data = o;
38+
return this;
39+
}
40+
41+
if(-1 == o.compareTo(bt.data)) {
42+
if(null == bt.left) {
43+
insertedNode = new BinaryTreeNode<T>();
44+
insertedNode.data = o;
45+
bt.left = insertedNode;
46+
return insertedNode;
47+
}
48+
return insert(bt.left,o);
49+
}else {
50+
if(null == bt.right) {
51+
insertedNode = new BinaryTreeNode<T>();
52+
insertedNode.data = o;
53+
bt.right = insertedNode;
54+
return insertedNode;
55+
}
56+
57+
return insert(bt.right,o);
58+
}
59+
}
60+
61+
@Override
62+
public String toString() {
63+
StringBuilder space = new StringBuilder("");
64+
return getString(this,space,0).toString();
65+
}
66+
67+
private StringBuilder getString(BinaryTreeNode<T> head,StringBuilder space,int deepth) {
68+
StringBuilder spaceTemp = new StringBuilder(space.toString());
69+
if (null == head) {
70+
return new StringBuilder(" null , ");
71+
} else {
72+
StringBuilder tempStr = new StringBuilder(" {").append(spaceTemp.append(head.data.toString())
73+
.append(new StringBuilder(space)).append("} "));
74+
deepth++;
75+
return getString(head.left, space, deepth).append(deepth)
76+
.append(tempStr).append(deepth).append("\n")
77+
.append(getString(head.right, space, deepth));
78+
}
79+
80+
}
81+
82+
83+
84+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.coding.basic;
2+
3+
public interface Iterator {
4+
public boolean hasNext();
5+
public Object next();
6+
7+
}

0 commit comments

Comments
 (0)