Skip to content

Commit 551df02

Browse files
authored
Merge pull request onlyliuxin#6 from WANGCANCER/master
Merge WANGCANCER Commit
2 parents b3db14d + dd964fc commit 551df02

File tree

10 files changed

+513
-0
lines changed

10 files changed

+513
-0
lines changed

group11/996108220/.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>

group11/996108220/.gitignore

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

group11/996108220/.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>996108220Learning</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: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package com.coding.basic;
2+
3+
public class ArrayList implements List {
4+
5+
private int size = 0;
6+
private Object[] elementData = new Object[100];
7+
/**
8+
* 在队尾添加元素
9+
*/
10+
public void add(Object o){
11+
if(size+1>elementData.length)this.grow(elementData);
12+
else elementData[size++]=o;
13+
}
14+
/**
15+
* 在index处添加元素,index+1到size-1元素向后移动
16+
*/
17+
public void add(int index, Object o){
18+
if(index<0||index>size){
19+
System.out.println("数组越界"+index);
20+
return;
21+
}
22+
if(size+1>elementData.length)this.grow(elementData);
23+
else {
24+
for(int i=size;i>=index+1;)
25+
{
26+
elementData[i]=elementData[--i];
27+
}
28+
size++;
29+
elementData[index]=o;
30+
}
31+
32+
}
33+
/**
34+
* 获得index处的元素elementData[index]
35+
*/
36+
public Object get(int index){
37+
//TODO越界抛出异常
38+
if(index<0||index>size){
39+
System.out.println("数组越界"+index);
40+
return null;
41+
}
42+
else{
43+
return elementData[index];
44+
}
45+
}
46+
/**
47+
* 移除index处的元素,将index+1到size-1的元素向前移动
48+
*/
49+
public Object remove(int index){
50+
//TODO越界抛出异常
51+
if(index<0||index>=size){
52+
System.out.println("数组越界"+index);
53+
return null;
54+
}
55+
else{
56+
Object value=elementData[index];
57+
for(int i=index;i<size;)
58+
{
59+
elementData[i]=elementData[++i];
60+
}
61+
size--;
62+
return value;
63+
}
64+
}
65+
66+
public int size(){
67+
return size;
68+
}
69+
70+
public Iterator iterator(){
71+
return new Itr();
72+
}
73+
74+
private class Itr implements Iterator {
75+
int cursor; // index of next element to return
76+
Itr() {
77+
cursor=0;
78+
}
79+
public boolean hasNext() {
80+
return cursor != size;
81+
}
82+
83+
84+
public Object next() {
85+
86+
int i = cursor;
87+
if (i >= size)
88+
System.out.println("超过size");;
89+
Object[] elementData = ArrayList.this.elementData;
90+
if (i >= elementData.length)
91+
System.out.println("超过length");
92+
cursor = i + 1;
93+
return elementData[i];
94+
}
95+
96+
}
97+
public void grow(Object[] elementData2){
98+
int[] elementData=new int[elementData2.length+elementData2.length/2];
99+
System.arraycopy(elementData2,0,elementData,0,elementData2.length);
100+
}
101+
/**
102+
* 测试方法
103+
* @param args
104+
*/
105+
public static void main(String args[]){
106+
ArrayList list=new ArrayList();
107+
list.add(1);
108+
list.add(2);
109+
list.add(3);
110+
list.add(4);
111+
list.add(5);
112+
list.add(6);
113+
//list.add(2,0);
114+
//list.remove(list.size-1);
115+
System.out.println(list.size());
116+
Iterator itr=list.iterator();
117+
while (itr.hasNext()) {
118+
System.out.println(itr.next());
119+
120+
}
121+
}
122+
123+
}
124+
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package com.coding.basic;
2+
3+
import com.sun.corba.se.impl.orbutil.graph.Node;
4+
5+
public class BinaryTree <T extends Comparable>{
6+
7+
private BinaryTreeNode root=null;
8+
private int size=0;
9+
/**
10+
* 插入节点,保持二叉树的性质
11+
* @param o
12+
*/
13+
public void insert(T o){
14+
if (size==0) {
15+
root=new BinaryTreeNode<Comparable>(o);
16+
}
17+
else{
18+
insert(o,root);
19+
}
20+
size++;
21+
}
22+
23+
private void insert(T o, BinaryTreeNode<?> ptr) {
24+
if ((ptr.right==null&&(ptr.data.compareTo(o)<=0))){
25+
ptr.right=new BinaryTreeNode<Comparable>(o);
26+
}
27+
else if (ptr.left==null&&(ptr.data.compareTo(o)>0)) {
28+
ptr.left=new BinaryTreeNode<Comparable>(o);
29+
30+
}
31+
else if (ptr.left!=null&&(ptr.data.compareTo(o)>0)) {
32+
insert(o, ptr.left);
33+
}
34+
else {
35+
insert(o, ptr.right);
36+
}
37+
}
38+
private static class BinaryTreeNode <T extends Comparable>{
39+
private T data;
40+
private BinaryTreeNode left;
41+
private BinaryTreeNode right;
42+
private BinaryTreeNode(T o) {
43+
this.data=o;
44+
this.left=null;
45+
this.right=null;
46+
}
47+
private BinaryTreeNode() {
48+
49+
}
50+
}
51+
/**
52+
* 前序遍历
53+
*/
54+
public void preOrder(BinaryTreeNode Node)
55+
{
56+
if (Node != null)
57+
{
58+
System.out.print(Node.data + " ");
59+
preOrder(Node.left);
60+
preOrder(Node.right);
61+
}
62+
}
63+
64+
/**
65+
* 中序遍历
66+
*/
67+
public void midOrder(BinaryTreeNode Node)
68+
{
69+
if (Node != null)
70+
{
71+
midOrder(Node.left);
72+
System.out.print(Node.data + " ");
73+
midOrder(Node.right);
74+
}
75+
}
76+
77+
/**
78+
* 后序遍历
79+
*/
80+
public void posOrder(BinaryTreeNode Node)
81+
{
82+
if (Node != null)
83+
{
84+
posOrder(Node.left);
85+
posOrder(Node.right);
86+
System.out.print(Node.data + " ");
87+
}
88+
}
89+
/**
90+
* @param key查找元素
91+
* @param node
92+
* @return 返回date的node引用
93+
*/
94+
public BinaryTreeNode searchNode(T key,BinaryTreeNode node) {
95+
if (node!=null) {
96+
if (node.data.compareTo(key)==0) {
97+
return node;
98+
}
99+
else if (node.data.compareTo(key)>0) {
100+
return searchNode(key,node.left);
101+
}
102+
else {
103+
return searchNode(key,node.right);
104+
}
105+
}
106+
else{
107+
return null;
108+
}
109+
}
110+
111+
public static void main(String[] args) {
112+
BinaryTree tree=new BinaryTree();
113+
tree.insert(5);
114+
tree.insert(3);
115+
tree.insert(1);
116+
tree.insert(6);
117+
tree.insert(5);
118+
tree.insert(2);
119+
tree.preOrder(tree.root);
120+
System.out.println();
121+
tree.posOrder(tree.root);
122+
System.out.println();
123+
tree.midOrder(tree.root);
124+
System.out.println(tree.searchNode(1, tree.root).data);
125+
126+
127+
}
128+
129+
}
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)