Skip to content

Commit

Permalink
base structure
Browse files Browse the repository at this point in the history
  • Loading branch information
ljfirst committed Aug 22, 2018
1 parent bf4ff2a commit cf1743b
Show file tree
Hide file tree
Showing 7 changed files with 213 additions and 0 deletions.
Binary file added bin/link_list/RangList.class
Binary file not shown.
Binary file modified bin/link_list/SinglyLinkedList.class
Binary file not shown.
Binary file added bin/tree/Tree.class
Binary file not shown.
Binary file added bin/tree/binary_tree.class
Binary file not shown.
105 changes: 105 additions & 0 deletions data_structure/link_list/RangList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package link_list;
/**
*@author liujun
*@date: 2018-8-22 下午02:06:33
*@author—Email:ljfirst@mail.ustc.edu.cn
*@description:
*1. 判断单聊表是否有环
*2. 找出带环单链表的入口
*@version 1.0
*/
public class RangList extends SinglyLinkedList{

public RangList(int value) {
super(value);
// TODO Auto-generated constructor stub
}

@Override
//尾插法改写为构造有环链表
public SinglyLinkedList insertTail(int[] list) {
System.out.println("尾插法改写为构造有环链表");
SinglyLinkedList point = null;
SinglyLinkedList first = point;
//环入口结点
SinglyLinkedList temp = new SinglyLinkedList();
//尾插法,循环赋值
for (int i = 0; i < list.length; i++) {
SinglyLinkedList link = new SinglyLinkedList(list[i]);
//首次判断
if (first == null) {
first = point = link;
}
//环入口结点
if(i == list.length/2){
temp = link;
System.out.println("插入点:"+temp.value);
}
point.next = link;
point = point.next;
}
//为节点构造环
point.next = temp;
return first;
}

//判断单链表是否有环
public boolean FindListTail(SinglyLinkedList first) {
// TODO Auto-generated method stub
SinglyLinkedList slow = first;
SinglyLinkedList fast = first;

while (fast.next != null && fast != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
return true;
}
}
return false;
}

//返回单链表环的相遇点
public SinglyLinkedList ListMeet(SinglyLinkedList first) {

SinglyLinkedList slow = first;
SinglyLinkedList fast = first;

while (fast != null && fast.next != null) {
fast = fast.next.next;
slow = slow.next;
if (fast == slow) {
return fast;
}
}
return null;
}

//找出带环单链表的入口
public SinglyLinkedList findLoopPort(SinglyLinkedList first) {
// TODO Auto-generated method stub

SinglyLinkedList point_meet = ListMeet(first);
while(first != null && point_meet != null){
if(first == point_meet){
return point_meet;
}
first = first.next;
point_meet = point_meet.next;
}
return null;
}

//主方法测试
public static void main(String[] args) {

//构造有环单链表
int[] array = {1,2,3,4,5,6,7,8,9,10};
RangList rl = new RangList(0);
SinglyLinkedList first= rl.insertTail(array);

//测试单链表是否有环
System.out.println(rl.FindListTail(first));
System.out.println(rl.ListMeet(first).value);
}
}
6 changes: 6 additions & 0 deletions data_structure/link_list/SinglyLinkedList.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ public class SinglyLinkedList {
int value;
SinglyLinkedList next = null;

public SinglyLinkedList() {
// TODO Auto-generated constructor stub
this.value = 0;
}

public SinglyLinkedList(int value) {
// TODO Auto-generated constructor stub
this.value = value;
Expand Down Expand Up @@ -130,6 +135,7 @@ public void printLink(SinglyLinkedList first) {
//主方法测试
public static void main(String[] args) {
// Map s = new HashMap<Integer, Integer>(12);
//first是表头结点(含值0,后续被替换)
SinglyLinkedList first = new SinglyLinkedList(0);
int[] array = {1,2,3,4,5,6,7,8,9,10};
first = first.insertTail(array);
Expand Down
102 changes: 102 additions & 0 deletions data_structure/tree/Tree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package tree;

import java.util.LinkedList;
import java.util.Queue;

/**
*@author liujun
*@date: 2018-7-21 下午01:13:08
*@author—Email:ljfirst@mail.ustc.edu.cn
*@description:树、二叉树、先序遍历赋值二叉树、
*中序遍历赋值二叉树,后序遍历赋值二叉树
*其中二叉树包含:二叉树的数据结构、二叉树的三种遍历赋值、二叉树的层次遍历、二叉树的按行打印
*@version 1.0
*/
public class Tree {

public static void main(String[] args) {
binary_tree root = new binary_tree();
int [] tree_num ={1,5,7,-1,-1,-1,8,6,3,-1,-1,2,-1,-1,9,-1,-1};

//先序构建二叉树
root = root.create_preOrder(root, tree_num, 0);
//二叉树层次遍历
root.levelTraverse(root);
}
}

//二叉树的数据结构
class binary_tree{

binary_tree left; //左孩子
binary_tree right; //右孩子
int value = 0; //默认二叉树的值为0
public static int count = 0;//定义一个全局静态计数变量

public binary_tree(int value) {
// TODO Auto-generated constructor stub
this.value = value;
}

public binary_tree() {
// TODO Auto-generated constructor stub
}

//二叉树的三种遍历赋值
//将数组先序遍历赋值二叉树,约定-1是空指针
public binary_tree create_preOrder(binary_tree root, int [] tree_num ,int i){

if(i < tree_num.length){
if(tree_num[i] == -1){
root = null;
}else{
//new root's lchild and rchild
binary_tree lchild = new binary_tree();
binary_tree rchild = new binary_tree();
//preOrder
root.value = tree_num[i];
//不用++count,构造的时候或出错
root.left = create_preOrder(lchild, tree_num, ++count);
root.right = create_preOrder(rchild, tree_num, ++count);
}
}

return root;
}

//将数组中序遍历赋值二叉树
public binary_tree create_inOrder(binary_tree root){

return root;
}

//将数组后序遍历赋值二叉树
public binary_tree create_postOrder(binary_tree root){

return root;
}

//清零操作
public void clear(){
count = 0;
}
//二叉树的层次遍历,使用栈来辅助实现
public void levelTraverse(binary_tree root){

// Queue is just an interface, LinkedList is Realization
Queue<binary_tree> queue = new LinkedList<binary_tree>();
queue.offer(root);
while(!queue.isEmpty()){
binary_tree note = queue.poll();
System.out.print(note.value + " ");
if(note.left != null){
levelTraverse(note.left);
}
if(note.right != null){
levelTraverse(note.right);
}
}
}

// 二叉树的按行打印
}

0 comments on commit cf1743b

Please sign in to comment.