forked from onlyliuxin/coding2017
-
Notifications
You must be signed in to change notification settings - Fork 5
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 #11 from wayss000/master
提交数据结构
- Loading branch information
Showing
7 changed files
with
270 additions
and
54 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
group08/1144989424/firstPractice/src/basic/BinaryTreeNode.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,49 @@ | ||
package basic; | ||
|
||
/** | ||
* 二叉树的数据结构 | ||
* @author Wayss | ||
* 2017-02-25 | ||
*/ | ||
|
||
public class BinaryTreeNode { | ||
|
||
private Object data; | ||
private BinaryTreeNode left; | ||
private BinaryTreeNode right; | ||
|
||
public BinaryTreeNode(){ | ||
|
||
} | ||
|
||
public BinaryTreeNode(Integer val){ | ||
data = val; | ||
left = null; | ||
right = null; | ||
} | ||
|
||
|
||
public Object getData() { | ||
return data; | ||
} | ||
public void setData(Object data) { | ||
this.data = data; | ||
} | ||
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(Object o){ | ||
return null; | ||
} | ||
|
||
} |
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
32 changes: 0 additions & 32 deletions
32
group08/1144989424/firstPractice/src/basic/MyBinaryTreeNode.java
This file was deleted.
Oops, something went wrong.
89 changes: 78 additions & 11 deletions
89
group08/1144989424/firstPractice/src/basic/MyLinkedList.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 |
---|---|---|
@@ -1,46 +1,113 @@ | ||
package basic; | ||
|
||
/** | ||
* 实现LinkedList基本功能 | ||
* @author Wayss | ||
* 2017-02-23 | ||
*/ | ||
|
||
public class MyLinkedList implements MyList { | ||
|
||
private Node head; | ||
private int size = 0; | ||
|
||
public void add(Object o){ | ||
|
||
Node n = new Node(o); | ||
head.next = n; | ||
size++; | ||
} | ||
public void add(int index , Object o){ | ||
|
||
//1.index校验 | ||
if(index < 0 || index > size){ | ||
throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); | ||
} | ||
//2. 查找index位置的前一个节点 | ||
//tempNode为当前链表的第一个节点 | ||
Node tempNode = head.next; | ||
for(int i = 0; i < index - 1 ; i++){ | ||
tempNode = tempNode.next; | ||
} | ||
Node behindNode = tempNode.next; | ||
Node insertNode = new Node(o); | ||
tempNode.next = insertNode; | ||
insertNode.next = behindNode; | ||
size++; | ||
} | ||
public Object get(int index){ | ||
return null; | ||
//1.index校验 | ||
if(index < 0 || index > size){ | ||
throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); | ||
} | ||
//2. 查找当前节点 | ||
Node tempNode = head.next; | ||
for(int i = 0; i < index; i++){ | ||
tempNode = tempNode.next; | ||
} | ||
return tempNode.data; | ||
} | ||
public Object remove(int index){ | ||
return null; | ||
//1.index校验 | ||
if(index < 0 || index > size){ | ||
throw new IndexOutOfBoundsException("插入的下标越界了:"+"插入的下标为:"+index+"集合大小为:"+size); | ||
} | ||
//2. 查找当前节点的上一个节点 | ||
Node tempNode = head.next; | ||
for(int i = 0; i < index - 1; i++){ | ||
tempNode = tempNode.next; | ||
} | ||
Node deleteNode = tempNode.next; | ||
Node behideNode = tempNode.next.next; | ||
tempNode.next = behideNode; | ||
size--; | ||
return deleteNode.data; | ||
} | ||
|
||
public int size(){ | ||
return -1; | ||
return size; | ||
} | ||
|
||
public void addFirst(Object o){ | ||
|
||
Node insertNode = new Node(o); | ||
insertNode.next = head.next; | ||
head.next = insertNode; | ||
size++; | ||
} | ||
public void addLast(Object o){ | ||
|
||
Node insertNode = new Node(o); | ||
Node tempNode = head.next; | ||
for(int i = 0; i < size; i++){ | ||
tempNode = tempNode.next; | ||
} | ||
tempNode.next = insertNode; | ||
size++; | ||
} | ||
public Object removeFirst(){ | ||
return null; | ||
Node firstNode = head.next; | ||
head = firstNode.next; | ||
size--; | ||
return firstNode; | ||
} | ||
public Object removeLast(){ | ||
return null; | ||
Node tempNode = head.next; | ||
//1.移除需要找到最后一个点的前一个点 | ||
for(int i = 0; i < size - 1; i++){ | ||
tempNode = tempNode.next; | ||
} | ||
Node deleteNode = tempNode.next; | ||
tempNode.next = null; | ||
size--; | ||
return deleteNode; | ||
} | ||
public MyIterator iterator(){ | ||
return null; | ||
} | ||
|
||
|
||
private static class Node{ | ||
private static class Node{ | ||
Object data; | ||
Node next; | ||
|
||
public Node(Object data){ | ||
this.data = data; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,19 +1,31 @@ | ||
package basic; | ||
|
||
/** | ||
* 实现队列 | ||
* @author Wayss | ||
* 2017-02-25 | ||
*/ | ||
|
||
public class MyQueue { | ||
|
||
public void enQueue(Object o){ | ||
MyLinkedList linkList = new MyLinkedList(); | ||
|
||
public void enQueue(Object o){ | ||
linkList.addLast(o); | ||
} | ||
|
||
public Object deQueue(){ | ||
return null; | ||
return linkList.removeFirst(); | ||
} | ||
|
||
public boolean isEmpty(){ | ||
if(linkList.size() == 0){ | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
public int size(){ | ||
return -1; | ||
return linkList.size(); | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
group08/1144989424/firstPractice/src/basic/MySortBinaryTree.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,81 @@ | ||
package basic; | ||
|
||
import java.util.Comparator; | ||
|
||
/** | ||
* 实现二叉排序树,使左节点的值,始终父节点小,右节点的值,始终比父节点大 | ||
* @author Wayss | ||
* 2017-02-25 | ||
*/ | ||
|
||
public class MySortBinaryTree { | ||
|
||
public BinaryTreeNode root = new BinaryTreeNode(); | ||
|
||
/** | ||
* 1. 添加时,先判断与root节点值的大小 | ||
* 2. insert的值小于root的值,则插入到root的左边。 | ||
* 3. insert的值大于root的值,则插入到root的右边。 | ||
* PS:目前只支持Integer类型的对象插入 | ||
* @param val | ||
*/ | ||
public void add(Integer val){ | ||
BinaryTreeNode treeNode = new BinaryTreeNode(val); | ||
insert(root, treeNode); | ||
} | ||
|
||
private void insert(BinaryTreeNode node, BinaryTreeNode insertNode){ | ||
|
||
int result = compare((Integer)insertNode.getData(),(Integer)node.getData()); | ||
|
||
if(0 == result){ | ||
//相等的话,当重复数据,不插了.呵呵 | ||
} | ||
//insert的值小于root的值,则插入到root的左边。 | ||
//如果左节点有值,则递归 | ||
if(-1 == result){ | ||
if(node.getLeft() != null){ | ||
insert(node.getLeft(), insertNode); | ||
}else{ | ||
node.setLeft(insertNode); | ||
} | ||
} | ||
//insert的值大于root的值,则插入到root的右边。 | ||
if(1 == result){ | ||
if(node.getRight() != null){ | ||
insert(node.getRight(), insertNode); | ||
}else{ | ||
node.setRight(insertNode); | ||
} | ||
} | ||
} | ||
|
||
public MyArrayList getValue(){ | ||
MyArrayList malist = new MyArrayList(); | ||
getTreeValue(root,malist); | ||
return malist; | ||
} | ||
|
||
private void getTreeValue(BinaryTreeNode node,MyArrayList list){ | ||
//遍历左子数 | ||
if(node.getLeft() != null){ | ||
getTreeValue(node.getLeft(),list); | ||
} | ||
list.add(node.getData()); | ||
//遍历右子数 | ||
if(node.getRight() != null){ | ||
getTreeValue(node.getRight(),list); | ||
} | ||
} | ||
|
||
public static int compare(Integer i1, Integer i2){ | ||
if(i1 < i2){ | ||
return -1; | ||
}else if(i1 == i2){ | ||
return 0; | ||
}else { | ||
return 1; | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.