├── .gitignore
├── README.md
├── pom.xml
└── src
├── .gitignore
├── main
└── java
│ ├── Graph
│ ├── Dijkstra
│ │ └── DijkstraShortPath.java
│ ├── Floyd
│ │ └── FolydShortPath.java
│ ├── Kruskal
│ │ ├── Kruskal.java
│ │ └── kGraph.java
│ ├── PrimTree
│ │ ├── Prim.java
│ │ └── PrimSeparate.java
│ ├── SPFA
│ │ └── SPFA.java
│ ├── Topological
│ │ ├── KeyPath.java
│ │ └── TopologicalSort.java
│ └── basic
│ │ ├── Edge.java
│ │ ├── EdgePoint.java
│ │ ├── Graph.java
│ │ ├── GraphList.java
│ │ └── GraphPoint.java
│ ├── List
│ └── MyList
│ │ ├── DoubleList.java
│ │ ├── MyList.java
│ │ ├── Node.java
│ │ └── SingleList.java
│ ├── Search
│ ├── BinarySearch
│ │ ├── BinarySearch.java
│ │ └── tempHS.java
│ └── Hash
│ │ ├── HashList.java
│ │ └── ValuePoint.java
│ ├── Sort
│ ├── RadixSort
│ │ ├── MyList.java
│ │ ├── Node.java
│ │ └── Solution.java
│ ├── base
│ │ └── SqlList.java
│ ├── exchange
│ │ ├── BubbleSort.java
│ │ └── QuickSort.java
│ ├── insert
│ │ ├── InsertSort.java
│ │ └── ShellSort.java
│ ├── other
│ │ ├── MergeSort.java
│ │ ├── MergeSortStack.java
│ │ └── RadixSort.java
│ └── select
│ │ ├── HeapSort.java
│ │ └── SelectSort.java
│ ├── String
│ └── KMP.java
│ └── Trees
│ ├── AVL
│ ├── AVLTree.java
│ └── AvlPoint.java
│ ├── Huffman
│ └── HuffmanTreePriority.java
│ ├── ReadBalckTree
│ ├── RBNode.java
│ └── RBTree.java
│ ├── ThreadTree
│ └── threadTree.java
│ ├── basic
│ ├── HuffmanNode.java
│ ├── Node.java
│ └── ThreadNode.java
│ └── traverse
│ └── Tree.java
└── test
└── java
├── Graph
├── Dijkstra
│ └── DijkstraShortPathTest.java
├── Floyd
│ └── FolydShortPathTest.java
├── Kruskal
│ └── KruskalTest.java
├── PrimTree
│ └── PrimTest.java
└── SPFA
│ └── SPFATest.java
├── List
└── Test
│ ├── DoubleListTest.java
│ └── ListTest.java
├── Sort
└── RadixSort
│ └── SolutionTest.java
├── String
└── kmptestTest.java
└── Trees
└── Huffman
└── HuffmanTreePriorityTest.java
/.gitignore:
--------------------------------------------------------------------------------
1 | *.class
2 | .classpath
3 | .project
4 | bin/*
5 | bin
6 | .idea/*
7 | target/*
8 | data_structure.iml
9 | *.png
10 | !test
11 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | 大话数据结构JAVA版本源代码,优化无止境,掌握基础才能起飞
2 |
3 | # Configuration
4 | * 开发IDE : Intellij
5 | * JDK: 1.8
6 | * 项目管理工具: Maven
7 |
8 |
9 | # Install
10 | Install Maven, http://maven.apache.org/download.cgi
11 |
12 | # Run
13 | mvn package 然后就能补齐所有的包
14 | 可以导入IDE 能直接运行
15 |
16 |
17 | # Test
18 | 应用代码在 test 文件夹下
19 |
20 |
21 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | groupId
8 | data_structure
9 | 1.0-SNAPSHOT
10 |
11 |
12 | junit
13 | junit
14 | [4.13.1,)
15 |
16 |
17 |
--------------------------------------------------------------------------------
/src/.gitignore:
--------------------------------------------------------------------------------
1 | !test
2 |
--------------------------------------------------------------------------------
/src/main/java/Graph/Dijkstra/DijkstraShortPath.java:
--------------------------------------------------------------------------------
1 | package Graph.Dijkstra;
2 |
3 | import Graph.basic.Graph;
4 |
5 | /**
6 | * @author chenxi
7 | * Dijkstra 是单源最短路劲算法,也就是指定起点,然后求其他各个点到他的距离
8 | * 这部分代码,求解完最短距离之后,放到了 dis 数组中,并且产生 以最短距离为边的 一个新图
9 | *
10 | * 需要的额外数据,Dis,Visited,Pre(用于建立图,方便回溯输出路径)
11 | * 算法流程:
12 | * 1. k点开始,设置 Visited,(点k表示最新更新的点)
13 | * 2. 更新k点周围的所有的没有被访问过的点(访问过的结点肯定具有更短的长度)
14 | * 3. 选取最小的点min,k,min 加入新图,min作为新的k点 回到步骤1
15 | *
16 | */
17 | public class DijkstraShortPath {
18 | int []dis;
19 | public Graph shortestPath(int source,Graph g)
20 | {
21 | Graph rt = new Graph(g.count);
22 | int [] dis = new int [g.count] ;
23 |
24 | int [] visited = new int[g.count];
25 | int [] pre = new int[g.count];
26 | int i,j;
27 | for(i = 0;idis[k]+g.AdjacentMatrix[k][j]){
46 | dis[j] = dis[k]+g.AdjacentMatrix[k][j];
47 | pre [j] = k;
48 | }
49 |
50 | }
51 | //选最小
52 | int min =Integer.MAX_VALUE;
53 | for(j=0;j dis[j])
56 | {
57 | min = dis[j];
58 | k = j;
59 | }
60 | }
61 |
62 | if(min Dis[i][k]+Dis[k][j])
61 | {
62 | Dis[i][j] = Dis[i][k]+Dis[k][j];
63 | Path[i][j] = Path[k][j];
64 | }
65 | }
66 |
67 | }
68 | }
69 |
70 | }
71 | public Graph getNewGraph(int source, Graph g)
72 | {
73 | Graph rt = new Graph(Dis.length);
74 | for(int i = 0;i getPQ(kGraph g)
35 | {
36 | PriorityQueue edges = new PriorityQueue(11, new Comparator() {
37 | public int compare(Edge h1,Edge h2)
38 | {
39 | return h1.weight-h2.weight;
40 | }
41 | });
42 | for(Edge edge: g.edges) edges.offer(edge);
43 | return edges;
44 | }
45 |
46 | public kGraph runKruskal(kGraph g)
47 | {
48 | kGraph rt = new kGraph(g.count);
49 | int [] parent = new int [g.count];
50 | for(int i =0 ;i edges = this.getPQ(g);
53 | Edge e =null;
54 | int i=0;
55 | int edgeCount =0;
56 | while(!edges.isEmpty()){
57 |
58 | if(edgeCount++==g.count-1) break;
59 |
60 | e = edges.poll();
61 |
62 | if(this.Union(parent, e.s, e.e) ==false){
63 | continue;
64 | }
65 | rt.addEdge(e);
66 |
67 | }
68 | return rt;
69 | }
70 |
71 |
72 |
73 | }
74 |
--------------------------------------------------------------------------------
/src/main/java/Graph/Kruskal/kGraph.java:
--------------------------------------------------------------------------------
1 | package Graph.Kruskal;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Comparator;
5 | import java.util.Iterator;
6 | import java.util.PriorityQueue;
7 |
8 | import Graph.basic.Edge;
9 | import Graph.basic.Graph;
10 |
11 |
12 | public class kGraph extends Graph{
13 | ArrayList edges;
14 |
15 | public kGraph(int vertexNum) {
16 | super(vertexNum);
17 | // TODO Auto-generated constructor stub
18 |
19 | edges = new ArrayList();
20 | }
21 | public void addEdge(int a, int b, int v)
22 | {
23 | Edge edge= new Edge(a,b,v);
24 | edges.add(edge);
25 | }
26 | public void addEdge(Edge edge)
27 | {
28 | if(edge==null) return;
29 | edges.add(edge);
30 | }
31 | public void printEdges()
32 | {
33 | for(Iterator< Edge>i = edges.iterator();i.hasNext();)
34 | {
35 | Edge edge = i.next();
36 | System.out.println("Node "+ edge.s+" Node "+edge.e+" Edge " + edge.weight);
37 | }
38 |
39 | }
40 |
41 | }
42 |
--------------------------------------------------------------------------------
/src/main/java/Graph/PrimTree/Prim.java:
--------------------------------------------------------------------------------
1 | package Graph.PrimTree;
2 |
3 | import Graph.Dijkstra.DijkstraShortPath;
4 | import Graph.basic.Graph;
5 |
6 | /**
7 | * @author chenxi
8 | * 聚合类写法将距离更新和找寻最小值写在了一起了,时间复杂度比较与之前的,一个是N一个是 2N,下面这个更好一点
9 | * 其核心思是空集合A 和全是点的集合B, 不断从B中挑选符合条件的结点加入到A中,仍然是一种贪心算法
10 | * 算法流程:
11 | * 1. B中一个 p 点压入A(到A距离变为0)
12 | * 2. 更新点 所有与 p 相邻的点 到 A的距离, 以及这些点的前置点为 p
13 | * 3. 更新完成之后挑选最小的点 min,将 p,min 放入新图,min作为新的p点,回到步骤1
14 | *
15 | */
16 | public class Prim {
17 | public Graph runPrime(Graph g)
18 | {
19 | Graph rt = new Graph(g.count);
20 | int []distance = new int[g.count];
21 | int pre[] = new int[g.count];
22 | for(int i=0;i0)
31 | {
32 | if(g.AdjacentMatrix[i][cur]!=0 && distance[i]>g.AdjacentMatrix[i][cur] ){
33 | distance[i] = g.AdjacentMatrix[i][cur];
34 | pre[i] = cur;
35 | }
36 | if(min> distance[i])
37 | {
38 | min = distance[i];
39 | minI= i;
40 | }
41 | }
42 | }
43 |
44 | cur = minI;
45 | if(cur!=Integer.MAX_VALUE)
46 | rt.addEdge(cur, pre[cur], min);
47 |
48 | }while(cur!=Integer.MAX_VALUE);
49 | return rt;
50 | }
51 |
52 |
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/Graph/PrimTree/PrimSeparate.java:
--------------------------------------------------------------------------------
1 | package Graph.PrimTree;
2 |
3 | import Graph.Dijkstra.DijkstraShortPath;
4 | import Graph.basic.Graph;
5 |
6 | import java.util.*;
7 |
8 |
9 | /**
10 | * @author chenxi
11 | * Prim Tree 分步编程, 将Prime算法分开写成了三个步骤,思路较为清晰
12 | * 但是时间复杂度上变成了 2*N
13 | *
14 | */
15 | public class PrimSeparate {
16 | //put the selected node into the new graph, and set the dis to be 0
17 | public void add(int i ,int[] dis,int []pre, Graph rt )
18 | {
19 | //the first one
20 | if(i>0) rt.addEdge(i, pre[i],dis[i]);
21 | dis[i] = 0;
22 | }
23 |
24 | //uupdate the distance of related nodes and the reference node
25 | protected void update(int n, int []dis, Graph G, int []pre)
26 | {
27 | int foo ;
28 | for(int i =0;i foo)
35 | {
36 | dis[i] = foo;
37 | pre[i] = n;
38 | }
39 | }
40 | }
41 |
42 | //find the least distance
43 | protected int findMin(int []dis)
44 | {
45 | int min = Integer.MAX_VALUE;
46 | for(int i =0;idis[i])
53 | {
54 | min = i;
55 | }
56 | }
57 | return min;
58 | }
59 |
60 | public Graph runPrime(Graph G)
61 | {
62 | if(G==null || G.count==0)return null;
63 | if(G.count<3) return G;
64 |
65 | //Init Set and its value means the distance to set A;
66 | int [] dis = new int[G.count];
67 | //If the value is not -1 and it means the node absorb it into A with the minimum lenght
68 | int [] pre = new int[G.count];
69 | Graph rt = new Graph(G.count);
70 | for(int i=0;i0)
81 | {
82 |
83 | this.add(n, dis, pre,rt);
84 |
85 | this.update(n, dis, G, pre);
86 |
87 | n = this.findMin(dis);
88 |
89 | count--;
90 | }
91 | return rt;
92 | }
93 |
94 |
95 | }
96 |
--------------------------------------------------------------------------------
/src/main/java/Graph/SPFA/SPFA.java:
--------------------------------------------------------------------------------
1 | package Graph.SPFA;
2 |
3 | import java.util.LinkedList;
4 | import java.util.Queue;
5 |
6 | import Graph.basic.EdgePoint;
7 | import Graph.basic.GraphList;
8 |
9 | /**
10 | * @author chenxi
11 | * 中国人原创算法,屌炸天
12 | * http://en.wikipedia.org/wiki/Shortest_Path_Faster_Algorithm
13 | * 当然最关键的问题,他能够轻松解决两点之间多条边的问题,而不需要做过多的判断
14 | * 想法超级自然,利用邻接链表的结构来做
15 | *
16 | * 1. 源点入队列
17 | * 2. cur表示队首元素(dis是最新的值),弹出来
18 | * 3. 更新所有和 cur 相连接的点,如果被更新的点,切不在q中,压入q
19 | * 4. q不为空回到步骤2
20 | *
21 | */
22 | public class SPFA {
23 | public int [] spfa(int s, GraphList g )
24 | {
25 | int dis[] = new int[g.count];
26 | for(int i=0;i q = new LinkedList();
28 | q.offer(s);
29 | dis[s] = 0;
30 | while(!q.isEmpty())
31 | {
32 | int cur = q.poll();
33 | EdgePoint e = g.AdjecentList.get(cur).firstEdge;
34 | while(e!=null)
35 | {
36 | int alter = dis[cur] + e.weight;
37 | if(dis[e.index]> alter)
38 | {
39 | dis[e.index] = alter;
40 | if(!q.contains(e.index))
41 | q.offer(e.index);
42 | }
43 | e = e.next;
44 | }
45 | }
46 | return dis;
47 | }
48 | }
49 |
--------------------------------------------------------------------------------
/src/main/java/Graph/Topological/KeyPath.java:
--------------------------------------------------------------------------------
1 | package Graph.Topological;
2 |
3 | import java.util.ArrayList;
4 | import java.util.Iterator;
5 | import java.util.Stack;
6 |
7 | import Graph.basic.EdgePoint;
8 | import Graph.basic.GraphList;
9 | import Graph.basic.GraphPoint;
10 |
11 | public class KeyPath {
12 | int [] etv;
13 | int [] ltv;
14 |
15 | public static GraphList init()
16 | {
17 |
18 | GraphList rt =new GraphList(10);
19 |
20 | rt.addEdge(0, 1, 3);
21 | rt.addEdge(0, 2, 4);
22 | rt.addEdge(1, 4, 6);
23 | rt.addEdge(1, 3, 5);
24 |
25 | rt.addEdge(2, 3, 8);
26 | rt.addEdge(2, 5, 7);
27 |
28 | rt.addEdge(3, 4, 3);
29 | rt.addEdge(4, 6, 9);
30 | rt.addEdge(4, 7, 4);
31 |
32 | rt.addEdge(5, 7, 6);
33 | rt.addEdge(6, 9, 2);
34 | rt.addEdge(7, 8, 5);
35 | rt.addEdge(8, 9, 3);
36 | return rt;
37 |
38 | }
39 | public void initVariables(int count)
40 | {
41 | etv = new int [count];
42 | ltv = new int [count];
43 |
44 |
45 | }
46 |
47 | public Stack TopologicalSort(GraphList glf)
48 | {
49 | Stack s = new Stack();
50 | Stack rt = new Stack();
51 |
52 | Integer i;
53 | glf.findInZeros(s);
54 |
55 | while(!s.isEmpty())
56 | {
57 | i = s.pop();
58 |
59 | GraphPoint gp = glf.getPoint(i);
60 |
61 | rt.push(gp);
62 |
63 | //clear edge and updte etv
64 | EdgePoint e = gp.firstEdge;
65 | while (e!=null)
66 | {
67 | int k = e.index;
68 |
69 | if((--glf.getPoint(k).in)==0 )
70 | {
71 |
72 | s.push(k);
73 | }
74 |
75 | if(etv[k] < etv[gp.index] + e.weight )
76 | {
77 | etv[k] = etv[gp.index] + e.weight;
78 | }
79 |
80 | e = e.next;
81 | }
82 |
83 |
84 | };
85 |
86 | return rt;
87 | }
88 |
89 | public void getKeyPath(GraphList glf)
90 | {
91 |
92 | Stack s = TopologicalSort(glf);
93 | Integer i;
94 | for(i=0;i ltv[k] - e.weight )
108 | {
109 | ltv[gp.index] = ltv[k] - e.weight;
110 | }
111 |
112 | e = e.next;
113 | }
114 | }
115 |
116 |
117 |
118 | //构造边的时间s
119 | int ete,lte;
120 | for (i=0;i s = TopologicalSort(glf);
145 |
146 | Iterator i = s.iterator();
147 | while (i.hasNext())
148 | {
149 | GraphPoint gp = i.next();
150 | System.out.print(gp.index+"-->");
151 | }
152 | System.out.println();
153 |
154 | for(int j = 0;j");
54 | }
55 | public void show(ArrayList rt)
56 | {
57 | Iterator gp = rt.iterator();
58 | while(gp.hasNext())
59 | {
60 | show(gp.next().index);
61 | }
62 | }
63 | public ArrayList getTopologicalSort(GraphList glf)
64 | {
65 | Stack s = new Stack();
66 | ArrayList rt = new ArrayList();
67 | Integer i;
68 | glf.findInZeros(s);
69 | while(!s.isEmpty())
70 | {
71 | i = s.pop();
72 |
73 | GraphPoint gp = glf.getPoint(i);
74 |
75 | glf.clearEdge(i);
76 |
77 | rt.add(gp);
78 |
79 | glf.findInZeros(s);
80 | };
81 |
82 | return rt;
83 | }
84 |
85 |
86 | public static void main(String[] args) {
87 | // TODO Auto-generated method stub
88 | GraphList glf = TopologicalSort.init();
89 | TopologicalSort ts = new TopologicalSort();
90 | ts.show(ts.getTopologicalSort(glf));
91 |
92 | }
93 |
94 | }
95 |
--------------------------------------------------------------------------------
/src/main/java/Graph/basic/Edge.java:
--------------------------------------------------------------------------------
1 | package Graph.basic;
2 |
3 | public class Edge {
4 | public int s,e;
5 | public int weight;
6 | public Edge()
7 | {
8 | s=e=weight= 0;
9 | }
10 | public Edge(int s,int e, int weight)
11 | {
12 | this.s = s;
13 | this.e = e;
14 | this.weight = weight;
15 | }
16 |
17 |
18 | }
19 |
--------------------------------------------------------------------------------
/src/main/java/Graph/basic/EdgePoint.java:
--------------------------------------------------------------------------------
1 | package Graph.basic;
2 | //describe the weight of edge
3 | public class EdgePoint {
4 | public int index;
5 | public int weight;
6 | public EdgePoint next = null;
7 | }
8 |
--------------------------------------------------------------------------------
/src/main/java/Graph/basic/Graph.java:
--------------------------------------------------------------------------------
1 | package Graph.basic;
2 |
3 | /**
4 | * XI CHEN
5 | *
6 | */
7 | import java.util.LinkedList;
8 | import java.util.Queue;
9 | import java.util.Stack;
10 |
11 | public class Graph {
12 |
13 | public int[][]AdjacentMatrix =null;
14 | public int[] visited = null;
15 | public int count = 0;
16 | public Graph(int vertexNum)
17 | {
18 | AdjacentMatrix = new int[vertexNum][vertexNum];
19 | for(int i=0;i q= new LinkedList();
65 | q.offer(start);
66 | int i =0;
67 | while(!q.isEmpty() && i< count )
68 | {
69 | int cur = q.poll();
70 | this.visit(cur);
71 | int j=0;
72 | while(j s = new Stack();
114 | s.push(start);
115 | int i = 0;
116 | while (!s.isEmpty()&& i0)
139 | System.out.println("Node "+j +" Node "+i+" Edge " + this.AdjacentMatrix[i][j]);
140 | }
141 | }
142 | }
143 | public static void main(String[] args) {
144 | // TODO Auto-generated method stub
145 | Graph g =new Graph(5);
146 | g.addEdge(0, 1);
147 | // g.addEdge(1, 2);
148 | g.addEdge(0, 3);
149 | g.addEdge(3, 4);
150 | // g.addEdge(4, 2);
151 | System.out.println("BFS");
152 | g.BFS();
153 | System.out.println("DFS non");
154 | g.DFS('N');
155 |
156 | System.out.println("DFS R");
157 | g.DFS('R');
158 |
159 | System.out.println("Show Edges");
160 | g.printEdges();
161 |
162 | }
163 |
164 | }
165 |
--------------------------------------------------------------------------------
/src/main/java/Graph/basic/GraphList.java:
--------------------------------------------------------------------------------
1 |
2 | package Graph.basic;
3 |
4 | import java.util.ArrayList;
5 | import java.util.Iterator;
6 | import java.util.LinkedList;
7 | import java.util.Queue;
8 | import java.util.Stack;
9 |
10 | public class GraphList {
11 | //each element is just a GraphPoint
12 | //The linked list is a varable of GraphPoint
13 | public ArrayList AdjecentList=null;
14 | public int [] visited = null;
15 | public int count = 0;
16 |
17 | //public int edgeCount = 0;
18 | public GraphList(int num)
19 | {
20 | AdjecentList = new ArrayList(num);
21 | visited = new int [num];
22 | for(int i=0;i index = this.AdjecentList.iterator();index.hasNext();)
37 | {
38 |
39 | GraphPoint cur = index.next();
40 | cur.disabled = false;
41 | }
42 | }
43 | public GraphPoint getPoint(int i)
44 | {
45 | GraphPoint cur = this.AdjecentList.get(i);
46 | if(!cur.disabled) return cur;
47 | return null;
48 | //return this.AdjecentList.get(i);
49 | }
50 | public void removeNode(int i)
51 | {
52 | getPoint(i).disabled = true;
53 | }
54 |
55 | public void addEdge(int start, int end, int weight)
56 | {
57 | GraphPoint s = this.getPoint(start);
58 | s.addEdge(end, weight);
59 | GraphPoint e = this.getPoint(end);
60 | e.in++;
61 |
62 | }
63 | public void addEdge(int start, int end ,int weight, Boolean undir)
64 | {
65 | this.addEdge(start, end, weight);
66 | if(undir==true)
67 | {
68 | this.addEdge(end,start , weight);
69 | }
70 |
71 | }
72 | public void clearEdge(int start)
73 | {
74 | GraphPoint cur = this.getPoint(start);
75 |
76 | EdgePoint ep = cur.firstEdge;
77 |
78 | if (ep ==null )
79 | {
80 | this.removeNode(start);
81 | return;
82 | }
83 | while(ep!=null )
84 | {
85 | this.getPoint(ep.index).in--;
86 | ep = ep.next;
87 | }
88 | this.removeNode(start);
89 | }
90 | public void addEdge(int start, int end)
91 | {
92 | GraphPoint s = this.getPoint(start);
93 | s.addEdge(end, 1);
94 | GraphPoint e = this.getPoint(end);
95 | e.in++;
96 |
97 | }
98 | public void findInZeros(Stack s)
99 | {
100 | for(Iterator i = this.AdjecentList.iterator();i.hasNext();)
101 | {
102 |
103 | GraphPoint cur = i.next();
104 | if(!cur.disabled && cur.in==0 && visited[cur.index]==0)
105 | {
106 | visited[cur.index] = 1;
107 | s.push(cur.index);
108 | }
109 | }
110 | }
111 | void visit(int start)
112 | {
113 | System.out.println("Visit Node "+start);
114 | visited[start] = 1;
115 | }
116 |
117 | public void DFS_FROM(int start)
118 | {
119 | if(visited[start]==1) return ;
120 | visit(start);
121 | EdgePoint ep = AdjecentList.get(start).firstEdge;
122 | while (ep!=null)
123 | {
124 | if(visited[ep.index]==0)
125 | {
126 | DFS_FROM(ep.index);
127 | }
128 | ep = ep.next;
129 | }
130 | }
131 |
132 | public void DFS()
133 | {
134 | this.initVist();
135 | for(int i=0;i q = new LinkedList();
147 | q.offer(this.AdjecentList.get(i));
148 |
149 | while(!q.isEmpty())
150 | {
151 | GraphPoint gp = q.poll();
152 | visit(gp.index);
153 | EdgePoint e = gp.firstEdge;
154 | while(e!=null)
155 | {
156 | if(visited[e.index]==0)
157 | {
158 | q.offer(this.AdjecentList.get(e.index));
159 | }
160 | e = e.next;
161 | }
162 | }
163 | }
164 | }
165 | public void printEdges()
166 | {
167 | for(GraphPoint gp: AdjecentList)
168 | {
169 | EdgePoint ep = gp.firstEdge;
170 | while(ep!=null)
171 | {
172 | System.out.println("Node "+gp.index+" Node "+ep.index+" Edge "+ep.weight);
173 | ep = ep.next;
174 | }
175 | }
176 | }
177 | public static void main(String [] args)
178 | {
179 | GraphList gl = new GraphList(5);
180 | gl.addEdge(0, 1);
181 |
182 | gl.addEdge(0, 2, 5);
183 | gl.addEdge(3, 2);
184 | gl.addEdge(0, 4);
185 | gl.addEdge(2,3);
186 | gl.DFS();
187 |
188 | }
189 | }
190 |
--------------------------------------------------------------------------------
/src/main/java/Graph/basic/GraphPoint.java:
--------------------------------------------------------------------------------
1 | package Graph.basic;
2 |
3 | import java.util.ArrayList;
4 |
5 |
6 | public class GraphPoint {
7 |
8 | public int index;
9 | //入度
10 | public int in=0;
11 |
12 | public Boolean disabled = false;
13 |
14 | public EdgePoint firstEdge = null;
15 | public GraphPoint(int index)
16 | {
17 | this.index = index;
18 | }
19 |
20 | public void addEdge(int e,int weight)
21 | {
22 | EdgePoint temp = new EdgePoint();
23 | temp.index = e;
24 | temp.weight = weight;
25 | temp.next = null;
26 |
27 | if(firstEdge==null){
28 | firstEdge = temp;
29 | return;
30 | }
31 |
32 | EdgePoint cur = firstEdge;
33 | while (cur.next!=null)
34 | {
35 | cur = cur.next;
36 | }
37 | cur.next = temp;
38 | }
39 |
40 | public void delEdge(int e)
41 | {
42 | EdgePoint pre = firstEdge;
43 | if (pre ==null ) return;
44 | if(pre.index==e) firstEdge = null;
45 |
46 | EdgePoint ep = pre.next;
47 |
48 | while(ep.next!=null && ep.index!=e)
49 | {
50 | pre = pre.next;
51 | ep =ep.next;
52 | }
53 | pre.next = null;
54 | }
55 |
56 |
57 |
58 |
59 |
60 | }
61 |
--------------------------------------------------------------------------------
/src/main/java/List/MyList/DoubleList.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package List.MyList;
5 | import java.util.*;
6 | /**
7 | * @author chenxi
8 | * 双向链表,和之前的单向链表相同
9 | */
10 | public class DoubleList extends MyList{
11 |
12 | @Override
13 | public void insert(E value) {
14 | // TODO Auto-generated method stub
15 | Node temp = new Node (value);
16 | if(size==0){
17 | head = tail = temp;
18 | size++; return;
19 | }
20 | temp.pre = tail;
21 | tail.next = temp;
22 | tail = temp;
23 | size++;
24 | }
25 |
26 | @Override
27 | public void insertBeforeHead(E value) {
28 | // TODO Auto-generated method stub
29 | if(size==0) {insert(value);return;}
30 | Node temp = new Node (value);
31 | temp.next = head;
32 | head.pre = temp;
33 | head = temp;
34 | size++;
35 | }
36 |
37 | @Override
38 | public void insert(E value, int index) {
39 | // TODO Auto-generated method stub
40 | if(size==0 ||index>=size) insert(value);
41 | else if(index<=0) insertBeforeHead(value);
42 | else
43 | {
44 | int i=0;
45 | Nodepre =head, cur = head.next;
46 | while(cur!=tail && i temp = new Node (value);
51 | temp.pre = pre;
52 | pre.next = temp;
53 |
54 | temp.next = cur;
55 | cur.pre = temp;
56 | size++;
57 | }
58 | }
59 |
60 |
61 |
62 | @Override
63 | public void delete(int index) {
64 | // TODO Auto-generated method stub
65 | if(size==0 || index>=size) return ;
66 | if(index<0) return;
67 | if(index==0){
68 | head = head.next;
69 | size--;
70 | if(size==0) tail = head;
71 | else head.pre = null;
72 | return;
73 | }
74 |
75 | Nodepre=head, cur= head.next;
76 | int i=0;
77 | while(cur!=null&&i++pre=head, cur= head.next;
97 | while(cur!=null&&cur.Value!=obj){pre=cur;cur= cur.next;}
98 | if(cur!=null){
99 | if(cur==tail)
100 | {
101 | tail = pre;pre.next = cur.next;
102 | }
103 | else{
104 | pre.next = cur.next;
105 | cur.next.pre = pre;
106 | }
107 | size--;
108 | }
109 | }
110 |
111 |
112 | }
113 |
--------------------------------------------------------------------------------
/src/main/java/List/MyList/MyList.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package List.MyList;
5 | import java.util.*;
6 | /**
7 | * @author chenxi
8 | * 简单的链表操作,增,删,改,查,单向链表
9 | * 增:指定位置增加;头部增加,尾部增加 done
10 | * 删除,置顶位置删除
11 | * 查:按照下标来查找,按照value 来查找 done
12 | * 改,指定位置修改
13 | */
14 | public abstract class MyList {
15 | Node head, tail;
16 | int size;
17 |
18 | public MyList( )
19 | {
20 | head = tail = null;size = 0;
21 | }
22 | //Insert
23 | //insert from tail
24 | public abstract void insert(E value);
25 |
26 | public abstract void insertBeforeHead(E value);
27 |
28 | //insert before index
29 | public abstract void insert(E value, int index);
30 |
31 |
32 | //delete
33 | //delete by index
34 | public abstract void delete( int index );
35 |
36 | //delete by Object
37 | public abstract void delete(E obj);
38 |
39 | //Search
40 | public Node find(int index)
41 | {
42 | if(head ==null) return head;
43 | if(index>=size) return null;
44 | Node cur = head;
45 | int i=0;
46 | while(cur!=null && i find (E object)
54 | {
55 | if(head ==null) return head;
56 | Node cur = head;
57 | while(cur!=null && cur.Value.equals(object))
58 | {
59 | cur = cur.next;
60 | }
61 | return cur;
62 | }
63 |
64 | public Node get(int index)
65 | {
66 | return find(index);
67 | }
68 | //update value by index
69 | public void update(int index, E value) {
70 | // TODO Auto-generated method stub
71 | Node cur= get(index);
72 | if(cur!=null) cur.Value = value;
73 | }
74 |
75 | //输出检查
76 | public String toString()
77 | {
78 | String res ="[";
79 | Node cur = head;
80 | while(cur!=null){
81 | res+=cur.Value+",";
82 | cur = cur.next;
83 | }
84 | return res.substring(0,res.length()-1)+"]";
85 | }
86 | //for test
87 | public boolean equalsList(List values)
88 | {
89 |
90 | if(values.size()!=size) return false;
91 | Node cur = head;
92 |
93 | for(int i=0;i l)
105 | {
106 | if(l.size()!=size) return false;
107 | if(size==0) return true;
108 | Node cur = tail;
109 | for(int i=size-1;i>=0;i--,cur=cur.pre)
110 | {
111 | if(!l.get(i).equals(cur.Value)) return false;
112 | }
113 | return true;
114 | }
115 |
116 | public static void main(String[] args)
117 | {
118 | }
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/src/main/java/List/MyList/Node.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package List.MyList;
5 | import java.util.*;
6 | /**
7 | * @author chenxi
8 | *
9 | */
10 | public class Node {
11 | public E Value;
12 | Node next;
13 | Node pre;
14 | public Node (E v)
15 | {
16 | this.Value = v;
17 | next = null;
18 | pre = null;
19 | }
20 |
21 | }
22 |
--------------------------------------------------------------------------------
/src/main/java/List/MyList/SingleList.java:
--------------------------------------------------------------------------------
1 | /**
2 | *
3 | */
4 | package List.MyList;
5 | import java.util.*;
6 | /**
7 | * @author chenxi
8 | * 简单的链表操作,增,删,改,查,单向链表
9 | * 增:指定位置增加;头部增加,尾部增加 done
10 | * 删除,置顶位置删除
11 | * 查:按照下标来查找,按照value 来查找 done
12 | * 改,指定位置修改
13 | */
14 | public class SingleList extends MyList{
15 |
16 |
17 | public SingleList( )
18 | {
19 | super();
20 | head = tail = null;size = 0;
21 | }
22 | //Insert
23 | //insert from tail
24 | public void insert(E value)
25 | {
26 | if(head==null)
27 | {
28 | head = new Node(value);
29 | tail = head;
30 | size++;
31 | return ;
32 | }
33 | tail.next = new Node(value);
34 | tail = tail.next;
35 | size++;
36 | }
37 | public void insertBeforeHead(E value)
38 | {
39 | if(head==null)
40 | {
41 | head = new Node(value);
42 | tail = head;
43 | size++;
44 | return ;
45 | }
46 | Node n = new Node (value);
47 | n.next = head;
48 | head = n;
49 | size++;
50 | }
51 | //insert before index
52 | public void insert(E value, int index)
53 | {
54 | if (index>=size ||tail ==null) insert(value);
55 | else if(index<=0 || head ==null) insertBeforeHead(value);
56 | else {
57 | Node cur =head;
58 | int i =0;
59 | while(i++ temp = new Node(value);
61 | temp.next = cur.next;
62 | cur.next = temp;
63 |
64 | }
65 | size++;
66 | }
67 |
68 |
69 |
70 | //update value by index
71 | public void update(int index, E value)
72 | {
73 | Node n = get(index);
74 | if(n!=null) n.Value = value;
75 | }
76 |
77 | //delete
78 | //delete by index
79 | public void delete( int index )
80 | {
81 | if(index>=size) return ;
82 | if(index<0) return;
83 | //删除都结点
84 | if(index==0)
85 | {
86 | head =head.next;
87 | size--;
88 | if(size<=1) tail = head;
89 | return;
90 | }
91 | int i =0;
92 | Node cur = head;
93 | while(i++ temp = cur.next;
96 | //删除的是尾结点
97 | if(temp==tail) tail = cur;
98 |
99 | cur.next = temp.next;
100 | size--;
101 | Listl = new ArrayList();
102 | }
103 |
104 | //delete by Object
105 | public void delete(E obj)
106 | {
107 | if(head == null) return;
108 | if(head.Value==obj) {delete(0);return;}
109 | Node pre =head,cur = head.next;
110 | while(cur!=null && cur.Value !=obj){pre= cur;cur = cur.next;}
111 | if(cur!=null)
112 | {
113 | if(cur==tail) tail = pre;
114 | pre.next = cur.next;
115 | size--;
116 | }
117 |
118 | }
119 |
120 |
121 | }
122 |
--------------------------------------------------------------------------------
/src/main/java/Search/BinarySearch/BinarySearch.java:
--------------------------------------------------------------------------------
1 | package Search.BinarySearch;
2 |
3 | import java.util.Scanner;
4 |
5 | public class BinarySearch {
6 |
7 | int []values = null;
8 | public BinarySearch(int []a ) {
9 | values = a ;
10 | }
11 | void show(int i)
12 | {
13 | if(i==-1) System.out.println("not found");
14 | else
15 | System.out.println("find "+values[i]+ " in position "+i);
16 | }
17 | void show(int i, int times)
18 | {
19 | if(i==-1) System.out.println("not found");
20 | else
21 | System.out.println(times+" times search "+"find "+values[i]+ " in position "+i);
22 | }
23 | public void search(int[]a, int len, int v)
24 | {
25 |
26 | int s,e,mid;
27 | s=0;e=len-1;
28 | while(s<=e)
29 | {
30 | mid=(e+s)/2;
31 | if(a[mid] == v) { show(mid); return;}
32 | else if(a[mid]v){ e = mid-1;}
34 | }
35 | show(-1);
36 | }
37 |
38 |
39 |
40 |
41 | public void BSearch(int []a)
42 | {
43 | Scanner s = new Scanner(System.in);
44 | while(true){
45 | System.out.println("请输入要查找的数字");
46 | int v = s.nextInt();
47 | if(v<0) break;
48 |
49 | search (a,a.length,v);
50 | }
51 | }
52 |
53 | public static void main(String[] args) {
54 | // TODO Auto-generated method stub
55 |
56 | tempHS t =new tempHS();
57 | int [] va = t.init(10);
58 | t.sort(va);
59 | t.showList(va);
60 |
61 | BinarySearch bs = new BinarySearch(va);
62 | bs.BSearch(va);
63 |
64 |
65 | }
66 |
67 |
68 | }
69 |
--------------------------------------------------------------------------------
/src/main/java/Search/BinarySearch/tempHS.java:
--------------------------------------------------------------------------------
1 | package Search.BinarySearch;
2 |
3 | import java.util.Random;
4 |
5 | import Sort.base.SqlList;
6 |
7 | public class tempHS {
8 |
9 | void swap(int[]a,int i,int j )
10 | {
11 | a[i] = a[i] + a[j];
12 | a[j] = a[i]-a[j];
13 | a[i] = a[i]-a[j];
14 | }
15 | //adjust from s to e-1
16 | void heapAdjust(int[]a, int s, int e)
17 | {
18 | int cur = s;
19 | while(cur=e) break;
23 | if(temp+1a[temp]) temp++;
24 | if(a[cur] < a[temp]) swap(a,cur,temp);
25 | cur = temp;
26 | }
27 | }
28 | //sort 0 to len-1
29 | //最大堆排序
30 | public void sort(int []a )
31 | {
32 | for(int i =a.length/2;i>=0;i--)
33 | {
34 | heapAdjust(a, i, a.length);
35 | }
36 |
37 | for(int i =a.length-1;i>0;i--)
38 | {
39 | swap(a,0,i);
40 | heapAdjust(a, 0, i);
41 |
42 | }
43 |
44 | }
45 | public int[] init(int count)
46 | {
47 | int []a = new int[count];
48 | Random rand = new Random();
49 | for(int i=0;icapacity) k = index(k);
35 | return this.value[k].isEmpty();
36 | }
37 | int index(int key){return key%capacity;}
38 | int getEmptyIndex(int key)
39 | {
40 | if(isEmpty(index(key))) return index(key);
41 | return getEmptyIndex(index(key+1));
42 | }
43 |
44 | public void set(ValuePoint vp ){
45 | this.set(vp.key,vp.value);
46 | }
47 |
48 | public void set(int key ,int value)
49 | {
50 | int i = getIndex(key);
51 | if(isFull() && i ==-1) {
52 | System.out.println("Full Capacity "); return ;
53 | }
54 | if(i==-1) {
55 | this.value[getEmptyIndex(key)].set(key, value);
56 | count++;
57 | }
58 | else
59 | {
60 | this.value[i].set(key, value);
61 | }
62 |
63 | }
64 |
65 | int getIndex(int key)
66 | {
67 | if(this.value[index(key)].key== key )
68 | return index(key);
69 |
70 | for(int i =1 ;i < capacity;i++)
71 | {
72 |
73 | if(this.value[index(key+i)].key==key)
74 | return index(key+i);
75 | }
76 |
77 | return -1;
78 | }
79 |
80 | public ValuePoint get(int key )
81 | {
82 | int i = getIndex(key);
83 | if(i>-1) return this.value[i];
84 | System.out.println("not found");
85 | return null;
86 | }
87 |
88 | public void remove(int key)
89 | {
90 | int i = getIndex(key);
91 | if(i>-1) {
92 | this.value[i].reset();
93 | count--;
94 | return ;
95 | }
96 | System.out.println("not found");
97 |
98 | }
99 | public void Print()
100 | {
101 | System.out.println("Start"+ count);
102 | for(int i = 0;i=0;
22 | max = max< v? v:max;
23 | }
24 | int rt = 0;
25 | while(max>0)
26 | {
27 | max /=10;
28 | rt++;
29 | }
30 | return rt;
31 |
32 | }
33 | void sort(int[] values)
34 | {
35 | MyList[] lists = new MyList [10];
36 | for(int i =0;ii;j--)
13 | {
14 | if(sl.r[j]=mV && i=end || end>sl.length) return;
25 |
26 | int mid = this.parting(sl,start,end);
27 | this.qsort(sl, start, mid-1);
28 | this.qsort(sl, mid+1, end);
29 | }
30 | public void sort(SqlList sl)
31 | {
32 | this.qsort(sl, 0, sl.length-1);
33 | }
34 | public static void main(String[] args) {
35 | // TODO Auto-generated method stub
36 | SqlList sl = new SqlList();
37 | sl.init(7);
38 | sl.showList();
39 | QuickSort qs = new QuickSort();
40 | qs.sort(sl);
41 | sl.showList();
42 | }
43 |
44 | }
45 |
--------------------------------------------------------------------------------
/src/main/java/Sort/insert/InsertSort.java:
--------------------------------------------------------------------------------
1 | package Sort.insert;
2 | import Sort.base.SqlList;
3 | public class InsertSort {
4 | public static void sort(SqlList sl)
5 | {
6 | int temp;
7 | for (int i=1;i=0;j--)
15 | {
16 | if(sl.r[j]<=temp){break;}
17 | sl.r[j+1] = sl.r[j];
18 |
19 | }
20 | sl.r[j+1] = temp;
21 | }
22 | }
23 | }
24 |
25 | public static void main(String[] args)
26 | {
27 | SqlList sl = new SqlList();
28 | sl.init(10);
29 | sl.showList();
30 | sort(sl);
31 | sl.showList();
32 | }
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/Sort/insert/ShellSort.java:
--------------------------------------------------------------------------------
1 | package Sort.insert;
2 |
3 | import Sort.base.SqlList;
4 |
5 | public class ShellSort {
6 | public static void sort(SqlList sl)
7 | {
8 | int inc = sl.length;
9 | int j;
10 | while (inc>1)
11 | {
12 | inc = inc/3 +1;
13 | for(int i = inc;i=0&&sl.r[j] > temp;j-=inc)
20 | {
21 | sl.r[j+inc] = sl.r[j];
22 | }
23 | sl.r[j+inc] = temp;
24 | }
25 | }
26 | //if(inc ==1) break;
27 |
28 | }
29 | }
30 | public static void main(String[] args)
31 | {
32 | SqlList sl = new SqlList();
33 | sl.init(10);
34 | sl.showList();
35 | ShellSort.sort(sl);
36 | sl.showList();
37 | }
38 | }
39 |
--------------------------------------------------------------------------------
/src/main/java/Sort/other/MergeSort.java:
--------------------------------------------------------------------------------
1 | package Sort.other;
2 |
3 | import Sort.base.SqlList;
4 | import Sort.exchange.QuickSort;
5 |
6 | public class MergeSort {
7 | protected void mergeSort(int[] sl, int start, int end)
8 | {
9 | if (start>=end-1) return ;
10 | int mid = (end - start)/2+start;
11 | mergeSort(sl, start, mid);
12 | mergeSort(sl,mid,end);
13 | merge(sl,start,mid,end);
14 | }
15 |
16 | protected void merge(int[] sl,int start, int mid, int end)
17 | {
18 | int i=start;int j=mid;int z=0;
19 | int [] rtValue = new int[end-start];
20 | while(i=end-1) return ;
11 | int i=start,j=end;
12 | int mid = end/2;
13 | int k=1;
14 |
15 | while(k<=mid)
16 | {
17 | i= start;
18 | while(i<=end-2*k)
19 | {
20 | j=i+2*k;
21 | merge(sl,i,i+k,j);
22 | i=j;
23 | }
24 | if(i=end) break;
29 | if(j+1< end && sl.r[j+1]>sl.r[j]) j++;
30 | if(sl.r[i] >= sl.r[j]) break;
31 | sl.swap(i, j);
32 | i=j;
33 | }
34 |
35 | }
36 | public void input(SqlList sl)
37 | {
38 | for (int i = sl.length/2 ;i >=0 ; i--)
39 | {
40 | HeapAdjust(sl,i,sl.length);
41 | }
42 |
43 | }
44 | public static void main(String[] args) {
45 | // TODO Auto-generated method stub
46 | SqlList sl = new SqlList();
47 | sl.init(7);
48 | sl.showList();
49 | HeapSort hs = new HeapSort();
50 | hs.sort(sl);
51 | sl.showList();
52 | }
53 |
54 | }
55 |
--------------------------------------------------------------------------------
/src/main/java/Sort/select/SelectSort.java:
--------------------------------------------------------------------------------
1 | package Sort.select;
2 | import Sort.base.SqlList;
3 | public class SelectSort {
4 |
5 | public static void sort(SqlList sl)
6 | {
7 | int min;
8 | for (int i=0;i sl.r[j])
14 | {
15 | min = j;
16 | }
17 | }
18 | if (i!=min){
19 | sl.swap(i, min);
20 | }
21 | }
22 | }
23 | public static void main(String[] args)
24 | {
25 | SqlList sl = new SqlList();
26 | sl.init(10);
27 | sl.showList();
28 | sort(sl);
29 | sl.showList();
30 | }
31 |
32 | }
33 |
--------------------------------------------------------------------------------
/src/main/java/String/KMP.java:
--------------------------------------------------------------------------------
1 | package String;
2 |
3 | public class KMP {
4 |
5 | public void getIndex(String s, int []next)
6 | {
7 | next [0] = -1;
8 | int i=1;
9 | int j=-1;
10 |
11 | while (iv) return search(cur.l, v);
18 | return search(cur.r, v);
19 | }
20 |
21 | //找右子树的最左子树
22 | AvlPoint findDPointR(AvlPoint cur)
23 | {
24 | if(cur.l == null) return cur;
25 | AvlPoint rt = findDPointR(cur.l);
26 | if(rt.l==null) cur.l = rt.r;
27 | getBF(cur);
28 | return rt;
29 |
30 | }
31 |
32 | //找左子树的最右子树
33 | AvlPoint findDPointL(AvlPoint cur)
34 | {
35 | if(cur.r == null) return cur;
36 | AvlPoint rt = findDPointL(cur.r);
37 | if(cur.r==null) cur.r = rt.l;
38 | getBF(cur);
39 | return rt;
40 | }
41 |
42 | public AvlPoint delete(AvlPoint cur, int v)
43 | {
44 | if(cur == null ) return null;
45 | if(cur.value == v)
46 | {
47 | AvlPoint replacePoint=null;
48 |
49 | if(cur.r!=null) replacePoint = this.findDPointR(cur);
50 | else if(cur.l!=null)replacePoint = this.findDPointL(cur);
51 |
52 | if(replacePoint!=null){
53 | replacePoint.l = cur.l;
54 | replacePoint.r = cur.r;
55 | getBF(replacePoint);
56 | }
57 | return replacePoint;
58 | }
59 |
60 | if(cur.value>v)
61 | {
62 | AvlPoint replacePoint=delete(cur.l, v);
63 | cur.l = replacePoint;
64 | }
65 | else if(cur.value1)
73 | {
74 | if(cur.l.bf>=0) {temp = RoateR(cur);}
75 | else if (cur.l.bf<0) {
76 | temp = RoateLR(cur);
77 | }
78 | }
79 | else if(cur.bf<-1)
80 | {
81 | if(cur.r.bf<=0){
82 | temp = RoateL(cur);
83 | }
84 | else if(cur.r.bf>0)
85 | {
86 | temp = RoateRL(cur);
87 | }
88 | }
89 | if(temp!=cur&&root==cur) root =temp;
90 | return temp;
91 | }
92 |
93 | AvlPoint insert(AvlPoint cur , int v)
94 | {
95 | if(cur==null)
96 | {
97 | cur = new AvlPoint(v);
98 | if(root == null) root = cur;
99 | return cur;
100 | }
101 | else
102 | {
103 | if(cur.value==v) return cur;
104 | else if(cur.value>v)
105 | {
106 | cur.l = insert(cur.l,v);
107 | }
108 | else if(cur.value1)
116 | {
117 | if(cur.l.bf>=0) {temp = RoateR(cur);}
118 | else if (cur.l.bf<0) {
119 | temp = RoateLR(cur);
120 | }
121 | }
122 | else if(cur.bf<-1)
123 | {
124 | if(cur.r.bf<=0){temp = RoateL(cur);}
125 | else if(cur.r.bf>0)
126 | {
127 | temp = RoateRL(cur);
128 | }
129 | }
130 | if(temp!=cur&&root==cur) root =temp;
131 | return temp;
132 |
133 | }
134 |
135 |
136 | }
137 |
138 |
139 | public void getBF(AvlPoint A)
140 | {
141 | int r = A.r==null?0:A.r.height;
142 | int l = A.l==null?0:A.l.height;
143 | A.height = (r>l?r:l)+1;
144 | A.bf = l-r;
145 | }
146 | AvlPoint RoateR(AvlPoint A )
147 | {
148 | AvlPoint B = A.l;
149 | A.l = B.r;
150 | B.r = A;
151 | getBF(A);
152 | getBF(B);
153 | return B;
154 | }
155 |
156 | AvlPoint RoateL(AvlPoint A)
157 | {
158 | AvlPoint B = A.r;
159 | A.r = B.l;
160 | B.l = A;
161 | getBF(A);
162 | getBF(B);
163 | return B;
164 | }
165 | AvlPoint RoateRL(AvlPoint A)
166 | {
167 | RoateR(A.r);
168 | return RoateL(A);
169 | }
170 |
171 | AvlPoint RoateLR(AvlPoint A)
172 | {
173 | RoateL(A.l);
174 | return RoateR(A);
175 | }
176 |
177 | //this method is clear and works well
178 | //it take advantage of the Recursion as the parent
179 | public void createAVL(int[]a)
180 | {
181 | for(int i=0;i charList= new HashMap();
14 | HuffmanNode root;
15 | //统计每个字符出现的次数
16 | public HashMap converWord(String s)
17 | {
18 | HashMap h = new HashMap();
19 |
20 | for(int i=0;i init(String s)
36 | {
37 | PriorityQueue pq = new PriorityQueue(11,new Comparator() {
38 | public int compare(HuffmanNode h1,HuffmanNode h2)
39 | {
40 | return h1.count-h2.count;
41 | }
42 | });
43 | HashMap h = this.converWord(s);
44 | for(Iterator i = h.keySet().iterator();i.hasNext();)
45 | {
46 |
47 | Character key = i.next();
48 | int count = h.get(key);
49 | HuffmanNode hn = new HuffmanNode(Character.toString(key),count);
50 | pq.offer(hn);
51 | }
52 | return pq;
53 | }
54 | //拿到两个最小的合成新的结点,加入到队列中去
55 | public void createTree(PriorityQueue words)
56 | {
57 |
58 | if(words.size()==1) return;
59 | HuffmanNode f = words.poll();
60 | HuffmanNode s = words.poll();
61 | HuffmanNode n = new HuffmanNode("no", f.count+s.count);
62 | n.left = f;
63 | n.right = s;
64 | words.offer(n);
65 | createTree(words);
66 | }
67 | //获取每个字符的编码
68 | public void encodeChar(HuffmanNode root,String Code)
69 | {
70 | if(root.isLeaf())
71 | {
72 | assert(root.Value.length()==1);
73 | charList.put(root.Value.toCharArray()[0], Code);
74 | return;
75 | }
76 | //向左添加0
77 | encodeChar(root.left,Code+"0");
78 | //向右添加1
79 | encodeChar(root.right,Code+"1");
80 | }
81 | public void prepareEncode(String s)
82 | {
83 | //input
84 | PriorityQueue words = init(s);
85 | //树的创建,最后变为size为1的 队列,内容即为root
86 | createTree(words);
87 | HuffmanNode root = words.poll();
88 |
89 | //给每一个元素赋值
90 | encodeChar(root, "");
91 |
92 | this.root = root;
93 | }
94 | public String encode(String s)
95 | {
96 | if(charList.isEmpty()) this.prepareEncode(s);
97 | StringBuffer rt = new StringBuffer();
98 | for(int i=0;i i = charList.keySet().iterator();i.hasNext();)
132 | {
133 | Character key = i.next();
134 | String Value = charList.get(key);
135 | System.out.println(key+" "+Value);
136 | }
137 | }
138 | public static void main(String[] args) {
139 |
140 | }
141 | }
142 |
--------------------------------------------------------------------------------
/src/main/java/Trees/ReadBalckTree/RBNode.java:
--------------------------------------------------------------------------------
1 | package Trees.ReadBalckTree;
2 |
3 | public class RBNode {
4 | public Integer value;
5 | public int color;//1 means read and 0 means black
6 | public RBNode left,right, parent;
7 | public RBNode(RBNode parent)
8 | {
9 | value = null;
10 | this.parent = parent;
11 | left = right = null;
12 | color = 0;
13 | }
14 | public void changeColor()
15 | {
16 | color = color ^ 1;
17 | }
18 | public RBNode(int value) {
19 | // TODO Auto-generated constructor stub
20 | this.value = value;
21 | this.color = 1;
22 | parent = null;
23 | right = new RBNode(this);
24 | left = new RBNode(this);
25 |
26 | }
27 |
28 | public RBNode(int value, RBNode parent) {
29 | // TODO Auto-generated constructor stub
30 | this.value = value;
31 | this.color = 1;
32 | this.parent = parent;
33 | right = new RBNode(this);
34 | left = new RBNode(this);
35 | }
36 | public void setNewValue(int value)
37 | {
38 | this.value = value;
39 | this.color = 1;
40 | if(right==null) right = new RBNode(this);
41 | if(left ==null) left = new RBNode(this);
42 | }
43 | public boolean isNil()
44 | {
45 | return value==null;
46 | }
47 |
48 |
49 | }
50 |
--------------------------------------------------------------------------------
/src/main/java/Trees/ReadBalckTree/RBTree.java:
--------------------------------------------------------------------------------
1 | package Trees.ReadBalckTree;
2 |
3 | import Trees.AVL.AvlPoint;
4 |
5 | public class RBTree {
6 | RBNode root;
7 | RBNode lastInsert= null;
8 | //take advantage of Nil is also a node
9 | public RBNode insert(int value)
10 | {
11 | RBNode cur = root;
12 | if(root==null){return new RBNode(value);}
13 | while(!cur.isNil())
14 | {
15 | if(cur.value==value) return null;
16 | if(cur.value>value ) cur = cur.left;
17 | else if(cur.valuerb.value) return rb.parent.right;
34 | if(rb.parent.valuerb.value) return g.right;
44 | else if(g.valuevalue) cur = cur.left;
121 | else if(cur.valuecur.value) cur.parent.left = cur.left;
172 | else cur.parent.right = cur.left;
173 | }
174 | //cur is root
175 | //cur only may has left son
176 | void delete_case1(RBNode cur){
177 | if(cur.parent!=null)
178 | delete_case2(cur);
179 | }
180 |
181 | //n是黑色,Child是黑色,S是红色(P是黑色)
182 | //n一定存在兄弟结点
183 | void delete_case2(RBNode cur){
184 | if(cur.color==0 && cur.left.color==0 )
185 | {
186 | if(Sibling(cur).color==1)
187 | {
188 | Sibling(cur).changeColor();
189 | cur.parent.changeColor();
190 |
191 | if(cur == cur.parent.left)
192 | RoateLeft(cur.parent);
193 | else
194 | RoateRight(cur.parent);
195 | }
196 | delete_case3(cur);
197 | }
198 |
199 | }
200 | //n是黑色,Child是黑色,S是黑色,P是红色
201 | void delete_case3(RBNode cur){
202 | RBNode s = Sibling(cur);
203 | if(cur.parent.color==1 && s.color==0 && s.left.color==0 && s.right.color==0 )
204 | {
205 | cur.parent.changeColor();
206 | Sibling(cur).changeColor();
207 |
208 | }
209 | else delete_case4(cur);
210 | }
211 | //n是黑色,Child是黑色,S是黑色,P是黑色,SL和SR同为黑色i
212 | void delete_case4(RBNode cur){
213 | RBNode s= Sibling(cur);
214 | if(s.color==0 && s.left.color==0 && 0==s.right.color)
215 | {
216 | s.changeColor();
217 | s.parent.changeColor();
218 | delete_case1(cur.parent);
219 | }
220 | else delete_case5(cur);
221 | }
222 |
223 | //SL 和SR 颜色不同 与N方向相反的点为黑色,另一个为红色
224 | void delete_case5(RBNode cur){
225 | RBNode s = Sibling(cur);
226 | if(cur == cur.parent.left && s.left.color==1 && s.right.color==0)
227 | {
228 | s.changeColor();
229 | s.left.changeColor();
230 | RoateRight(s);
231 | }
232 | else if(cur==cur.parent.right && s.right.color==1 && s.left.color==0)
233 | {
234 | s.changeColor();
235 | s.right.changeColor();
236 | RoateLeft(s);
237 | }
238 | delete_case6(cur);
239 | }
240 | //与N方向相反的点为红,另一个为黑色或者红色
241 | void delete_case6(RBNode cur){
242 | RBNode s = Sibling(cur);
243 | if(cur == cur.parent.left && s.right.color==1)
244 | {
245 | s.right.changeColor();
246 | RoateLeft(cur.parent);
247 | }
248 | else if(cur==cur.parent.right && s.left.color==1)
249 | {
250 | s.left.changeColor();
251 | int temp = s.parent.color;
252 | s.parent.color = s.color;
253 | s.color = temp;
254 | RoateRight(cur.parent);
255 |
256 | }
257 | }
258 |
259 | void RoateLeft(RBNode cur)
260 | {
261 | RBNode b = cur.right;
262 | cur.right = b.left;
263 | b.left.parent = cur;
264 |
265 | b.left = cur;
266 | b.parent = cur.parent;
267 | if(cur.parent!=null){
268 | if(cur.parent.value>cur.value) cur.parent.left = b;
269 | else cur.parent.right = b;
270 | }
271 | if(cur.parent==null) root = b;
272 | cur.parent = b;
273 | }
274 |
275 | void RoateRight(RBNode cur)
276 | {
277 | RBNode b = cur.left;
278 | cur.left = b.right;
279 | b.right.parent = cur;
280 |
281 | b.right = cur;
282 | b.parent = cur.parent;
283 | if(cur.parent!=null){
284 |
285 | if(cur.parent.value>cur.value) cur.parent.left = b;
286 | else cur.parent.right = b;
287 |
288 | }
289 | if(cur.parent==null) root = b;
290 | cur.parent = b;
291 |
292 |
293 | }
294 | public void preOrder(RBNode root)
295 | {
296 | if(root.value ==null) return;
297 | System.out.println(root.value+" color "+root.color);
298 | preOrder(root.left);
299 | preOrder(root.right);
300 | }
301 |
302 | public void midOrder(RBNode root)
303 | {
304 | if(root.value ==null) return;
305 |
306 | midOrder(root.left);
307 | System.out.println(root.value+" color "+root.color);
308 | midOrder(root.right);
309 | }
310 | public void createTree(int []value)
311 | {
312 | for(int i=0;i q = new LinkedList();
20 | q.offer(root);
21 | //Node left,right;
22 | while(i q = new LinkedList();
19 | q.offer(root);
20 | //Node left,right;
21 | while(i q = new LinkedList();
47 | q.offer(root);
48 | Node cur ;
49 | while(!q.isEmpty())
50 | {
51 | cur = q.poll();
52 | visit(cur);
53 | if(cur.left!=null) q.offer(cur.left);
54 | if(cur.right!=null) q.offer(cur.right);
55 | }
56 |
57 | }
58 |
59 | public void preOrder(Node Root)
60 | {
61 | if(Root ==null) return;
62 | visit(Root);
63 | preOrder(Root.left);
64 | preOrder(Root.right);
65 | }
66 |
67 | public void preOrder2(Node Root)
68 | {
69 | if(Root ==null) return;
70 | Stack s = new Stack();
71 | s.push(Root);
72 | Node cur;
73 | while (!s.isEmpty())
74 | {
75 | cur = s.pop();
76 | visit(cur);
77 | if(cur.right!=null) s.push(cur.right);
78 | if(cur.left!=null) s.push(cur.left);
79 | }
80 | }
81 | public void midOrder(Node Root)
82 | {
83 | if(Root==null) return;
84 | midOrder(Root.left);
85 | visit(Root);
86 | midOrder(Root.right);
87 | }
88 | //注意栈内变化,模拟栈内变化
89 | public void midOrder3(Node Root)
90 | {
91 | if(Root==null) return;
92 | Stack s = new Stack();
93 | Node cur = Root;
94 | while(cur!=null||!s.isEmpty())
95 | {
96 | //左结点访问结束的标志是 pre为空
97 | while(cur!=null)
98 | {
99 | s.push(cur);
100 | cur = cur.left;
101 | }
102 | if(!s.isEmpty())
103 | {
104 | cur = s.pop();
105 | visit(cur);
106 | cur = cur.right;
107 | }
108 |
109 | }
110 | }
111 |
112 | public void postOrder(Node Root)
113 | {
114 | if(Root==null) return;
115 | postOrder(Root.left);
116 | postOrder(Root.right);
117 | visit(Root);
118 | }
119 |
120 | public void postOrder3(Node Root)
121 | {
122 | if(Root==null) return;
123 | this.recoverVisited(Root);
124 | Stack s = new Stack();
125 | Node cur = Root;
126 | while((cur!=null&& cur.visited==0)||!s.isEmpty())
127 | {
128 | //左结点访问结束的标志是 pre为空
129 | while(cur!=null && cur.visited==0)
130 | {
131 | s.push(cur);
132 | cur = cur.left;
133 |
134 | }
135 | if(!s.isEmpty())
136 | {
137 | cur = s.pop();
138 | if (cur.right!=null && cur.right.visited==0) {
139 | s.push(cur);
140 | cur = cur.right;
141 | }
142 | else visit(cur);
143 |
144 |
145 | }
146 |
147 | }
148 | }
149 |
150 | void recoverVisited(Node Root)
151 | {
152 | if(Root ==null) return;
153 | Root.visited = 0;
154 | recoverVisited(Root.left);
155 | recoverVisited(Root.right);
156 | }
157 |
158 | public void midOrder2(Node Root)
159 | {
160 | if(Root==null) return;
161 | recoverVisited(Root);
162 | Stack s = new Stack();
163 | s.push(Root);
164 | Node cur;
165 | Node last;
166 | while(!s.isEmpty())
167 | {
168 | cur = s.pop();
169 | if(cur.left==null|| cur.left.visited==1)
170 | visit(cur);
171 | else
172 | {
173 | if(cur.right!=null) s.push(cur.right);
174 | s.push(cur);
175 | s.push(cur.left);
176 | }
177 | }
178 | }
179 | boolean sonsVisited(Node root)
180 | {
181 | return (root.left==null||root.left.visited==1) && (root.right==null||root.right.visited==1);
182 | }
183 | public void postOrder2(Node Root)
184 | {
185 | if(Root==null) return;
186 | recoverVisited(Root);
187 | Stack s = new Stack();
188 | s.push(Root);
189 | Node cur;
190 | Node last;
191 | while(!s.isEmpty())
192 | {
193 | cur = s.pop();
194 | if((cur.left==null&& cur.right==null)||sonsVisited(cur))
195 | {
196 | if(cur.visited==0)
197 | visit(cur);
198 | }
199 | else
200 | {
201 | s.push(cur);
202 | if(cur.right!=null && cur.right.visited==0) s.push(cur.right);
203 | if(cur.left!=null && cur.left.visited ==0) {s.push(cur.left);}
204 | }
205 |
206 |
207 | }
208 | }
209 | public static void testOrders(Tree t)
210 | {
211 | System.out.println("宽度优先递归");
212 | t.BFS(t.root);
213 | System.out.println("\n前序递归");
214 | t.preOrder(t.root);
215 |
216 | System.out.println("\n前序非递归");
217 | t.preOrder2(t.root);
218 |
219 | System.out.println("\n中序递归");
220 | t.midOrder(t.root);
221 | System.out.println("\n中序非递归");
222 | t.midOrder2(t.root);
223 | System.out.println("\n中序非递归");
224 | t.midOrder3(t.root);
225 |
226 | System.out.println("\n后序递归");
227 | t.postOrder(t.root);
228 | System.out.println("\n后序非递归");
229 | t.postOrder2(t.root);
230 | System.out.println("\n后序非递归");
231 | t.postOrder3(t.root);
232 |
233 | System.out.println("");
234 | }
235 |
236 | public static void main(String[] args)
237 | {
238 | int [] a = new int[]{1,2,3,4,5,6,7,8,9,10};
239 | Tree t = new Tree();
240 | t.createTree(a);
241 | Tree.testOrders(t);
242 |
243 |
244 |
245 | }
246 |
247 | }
248 |
--------------------------------------------------------------------------------
/src/test/java/Graph/Dijkstra/DijkstraShortPathTest.java:
--------------------------------------------------------------------------------
1 | package Graph.Dijkstra;
2 |
3 | import Graph.basic.Graph;
4 | import junit.framework.TestCase;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import static org.junit.Assert.assertTrue;
9 |
10 | public class DijkstraShortPathTest extends TestCase {
11 | Graph g ;
12 | @Before
13 | public void setUp() throws Exception {
14 | int v= 9;
15 | g = new Graph(v);
16 | g.addEdge(0, 1, 1);
17 | g.addEdge(0, 2, 5);
18 | g.addEdge(1, 2, 3);
19 | g.addEdge(1, 3, 7);
20 | g.addEdge(1, 4, 5);
21 | g.addEdge(2, 4, 1);
22 | g.addEdge(2, 5, 7);
23 | g.addEdge(3, 6, 3);
24 | g.addEdge(3, 4, 2);
25 | g.addEdge(4, 5, 3);
26 | g.addEdge(4, 6, 6);
27 | g.addEdge(4, 7, 9);
28 | g.addEdge(5, 5, 5);
29 | g.addEdge(6, 7, 2);
30 | g.addEdge(6, 8, 7);
31 | g.addEdge(7, 8, 4);
32 | }
33 | boolean valid(Graph G)
34 | {
35 |
36 | return G.getEdgeWeight(1, 0) == 1&&
37 | G.getEdgeWeight(1, 2) == 3 &&
38 | G.getEdgeWeight(2,4) == 1 &&
39 | G.getEdgeWeight(3, 4)== 2 &&
40 | G.getEdgeWeight(3, 6) == 3 &&
41 | G.getEdgeWeight(7, 6) == 2 &&
42 | G.getEdgeWeight(7, 8) == 4 &&
43 | G.getEdgeWeight(4, 5) == 3;
44 |
45 | }
46 | @Test
47 | public void test() {
48 | DijkstraShortPath dsp = new DijkstraShortPath();
49 | Graph rt = dsp.shortestPath(0, g);
50 | rt.printEdges();
51 | for(int i:dsp.dis) System.out.print(i+" ");
52 | System.out.println();
53 | assertTrue(valid(rt));;
54 | }
55 | }
--------------------------------------------------------------------------------
/src/test/java/Graph/Floyd/FolydShortPathTest.java:
--------------------------------------------------------------------------------
1 | package Graph.Floyd;
2 |
3 | import Graph.basic.Graph;
4 | import junit.framework.TestCase;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import static org.junit.Assert.assertTrue;
9 |
10 | public class FolydShortPathTest extends TestCase {
11 |
12 | Graph g ;
13 | @Before
14 | public void setUp() throws Exception {
15 | int v= 9;
16 | g = new Graph(v);
17 | g.addEdge(0, 1, 1);
18 | g.addEdge(0, 2, 5);
19 | g.addEdge(1, 2, 3);
20 | g.addEdge(1, 3, 7);
21 | g.addEdge(1, 4, 5);
22 | g.addEdge(2, 4, 1);
23 | g.addEdge(2, 5, 7);
24 | g.addEdge(3, 6, 3);
25 | g.addEdge(3, 4, 2);
26 | g.addEdge(4, 5, 3);
27 | g.addEdge(4, 6, 6);
28 | g.addEdge(4, 7, 9);
29 | g.addEdge(5, 5, 5);
30 | g.addEdge(6, 7, 2);
31 | g.addEdge(6, 8, 7);
32 | g.addEdge(7, 8, 4);
33 | }
34 | boolean valid(Graph G)
35 | {
36 |
37 | return G.getEdgeWeight(1, 0) == 1&&
38 | G.getEdgeWeight(1, 2) == 3 &&
39 | G.getEdgeWeight(2,4) == 1 &&
40 | G.getEdgeWeight(3, 4)== 2 &&
41 | G.getEdgeWeight(3, 6) == 3 &&
42 | G.getEdgeWeight(7, 6) == 2 &&
43 | G.getEdgeWeight(7, 8) == 4 &&
44 | G.getEdgeWeight(4, 5) == 3;
45 | }
46 | @Test
47 | public void test() {
48 | FolydShortPath fsp = new FolydShortPath();
49 | fsp.runFolydShortPath(g);
50 | Graph rt = fsp.getNewGraph(0, g);
51 | assertTrue(valid(rt));;
52 | rt.printEdges();
53 | }
54 | }
--------------------------------------------------------------------------------
/src/test/java/Graph/Kruskal/KruskalTest.java:
--------------------------------------------------------------------------------
1 | package Graph.Kruskal;
2 |
3 | import Graph.basic.Edge;
4 | import junit.framework.TestCase;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import java.util.Iterator;
9 |
10 | import static org.junit.Assert.assertTrue;
11 |
12 | public class KruskalTest extends TestCase {
13 | kGraph G;
14 | @Before
15 | public void setUp() throws Exception {
16 | int vertexNum = 9;
17 | G = new kGraph(vertexNum);
18 | G.addEdge(0, 1,10);
19 | G.addEdge(0, 5, 11);
20 | G.addEdge(1, 2, 18);
21 | G.addEdge(1, 6, 16);
22 | G.addEdge(1, 8, 12);
23 | G.addEdge(2, 3, 22);
24 | G.addEdge(2, 8, 8);
25 | G.addEdge(3, 4, 20);
26 | G.addEdge(3, 7, 16);
27 | G.addEdge(3, 8, 21);
28 | G.addEdge(4, 5,26);
29 | G.addEdge(4, 7,7);
30 | G.addEdge(5, 6, 17);
31 | G.addEdge(6, 7, 19);
32 |
33 | }
34 | boolean validEdge(Edge ed, int s, int e, int w)
35 | {
36 | return ed.s == s && ed.e == e && ed.weight ==w;
37 | }
38 | public boolean Validate(kGraph g)
39 | {
40 |
41 | for(Iterator< Edge> i = g.edges.iterator(); i.hasNext();)
42 | {
43 | Edge edge = i.next();
44 | if( !validEdge(edge, 4, 7, 7)&&
45 | !validEdge(edge,2,8,8) &&
46 | !validEdge(edge, 0, 1, 10) &&
47 | !validEdge(edge, 0 ,5, 11) &&
48 | !validEdge(edge, 1, 8, 12) &&
49 | !validEdge(edge, 1, 6, 16) &&
50 | !validEdge(edge, 3, 7, 16) &&
51 | !validEdge(edge, 6, 7, 19) ) return false;
52 | }
53 | return true;
54 |
55 | }
56 | @Test
57 | public void test() {
58 | Kruskal k = new Kruskal();
59 | kGraph g = k.runKruskal(G);
60 | g.printEdges();
61 | assertTrue(Validate(g));
62 | System.out.println();
63 | }
64 | }
--------------------------------------------------------------------------------
/src/test/java/Graph/PrimTree/PrimTest.java:
--------------------------------------------------------------------------------
1 | package Graph.PrimTree;
2 |
3 | import Graph.basic.Graph;
4 | import junit.framework.TestCase;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import static org.junit.Assert.assertTrue;
9 |
10 | public class PrimTest extends TestCase {
11 | Graph G;
12 | @Before
13 | public void setUp() throws Exception {
14 | int vertexNum=9;
15 | G = new Graph(vertexNum);
16 |
17 | G.addEdge(0, 1,10);
18 | G.addEdge(0, 5, 11);
19 | G.addEdge(1, 2, 18);
20 | G.addEdge(1, 6, 16);
21 | G.addEdge(1, 8, 12);
22 |
23 | G.addEdge(2, 3, 22);
24 | G.addEdge(2, 8, 8);
25 |
26 | G.addEdge(3, 4, 20);
27 | G.addEdge(3, 7, 16);
28 | G.addEdge(3, 8, 21);
29 | G.addEdge(3, 6, 24);
30 |
31 | G.addEdge(4, 5,26);
32 | G.addEdge(4, 7,7);
33 | G.addEdge(5, 6, 17);
34 | G.addEdge(6, 7, 19);
35 | }
36 | boolean validate(Graph G)
37 | {
38 | return G.getEdgeWeight(1, 0) == 10&&
39 | G.getEdgeWeight(5, 0) == 11 &&
40 | G.getEdgeWeight(6,1) ==16 &&
41 | G.getEdgeWeight(8, 1)== 12 &&
42 | G.getEdgeWeight(8, 2) == 8 &&
43 | G.getEdgeWeight(7, 3) == 16 &&
44 | G.getEdgeWeight(7, 4) == 7 &&
45 | G.getEdgeWeight(7, 6) == 19
46 | ;
47 |
48 |
49 | }
50 | @Test
51 | public void test() {
52 | // TODO Auto-generated method stub
53 | Prim p2 = new Prim();
54 | Graph rt = p2.runPrime(G);
55 | //rt.printEdges();
56 | assertTrue(validate(rt));
57 |
58 | PrimSeparate p = new PrimSeparate();
59 | rt = p.runPrime(G);
60 | assertTrue(validate(rt));
61 | }
62 | }
--------------------------------------------------------------------------------
/src/test/java/Graph/SPFA/SPFATest.java:
--------------------------------------------------------------------------------
1 | package Graph.SPFA;
2 |
3 | import Graph.basic.GraphList;
4 | import junit.framework.TestCase;
5 | import org.junit.Before;
6 | import org.junit.Test;
7 |
8 | import static org.junit.Assert.assertTrue;
9 |
10 | public class SPFATest extends TestCase {
11 | GraphList g;
12 | @Before
13 | public void setUp() throws Exception {
14 | int v= 9;
15 | g = new GraphList(v);
16 | g.addEdge(0, 1, 1,true);
17 | g.addEdge(0, 2, 5,true);
18 | g.addEdge(1, 2, 3,true);
19 | g.addEdge(1, 3, 7,true);
20 | g.addEdge(1, 4, 5,true);
21 | g.addEdge(2, 4, 1,true);
22 | g.addEdge(2, 5, 7,true);
23 | g.addEdge(3, 6, 3,true);
24 | g.addEdge(3, 4, 2,true);
25 | g.addEdge(4, 5, 3,true);
26 | g.addEdge(4, 6, 6,true);
27 | g.addEdge(4, 7, 9,true);
28 | g.addEdge(5, 5, 5,true);
29 | g.addEdge(6, 7, 2,true);
30 | g.addEdge(6, 8, 7,true);
31 | g.addEdge(7, 8, 4,true);
32 | }
33 |
34 | boolean valid(int []dis)
35 | {
36 |
37 | return dis[0] == 0 &&
38 | dis[1] == 1 &&
39 | dis[2] == 4 &&
40 | dis[3] == 7 &&
41 | dis[4] == 5 &&
42 | dis[5] == 8 &&
43 | dis[6] == 10 &&
44 | dis[7] == 12 &&
45 | dis[8] == 16 ;
46 | }
47 | @Test
48 | public void test() {
49 | SPFA s = new SPFA();
50 | int source = 0;
51 | int [] d = s.spfa(source, g);
52 | assertTrue(valid(d));
53 | for (int i=0;i();
11 | int [] v= {1,2,3,9,5,8};
12 | for(int i:v) {il.insert(i);testInt.add(i);}
13 |
14 | sl = new DoubleList();
15 | String [] strs = {"hello","nice","nee you","mei guo"};
16 | for(String s:strs) {sl.insert(s);testStr.add(s);}
17 | }
18 | @Override
19 | public void assertEqual()
20 | {
21 | super.assertEqual();
22 | assertTrue(il.equalReverseList(testInt));
23 | assertTrue(sl.equalReverseList(testStr));
24 |
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/src/test/java/List/Test/ListTest.java:
--------------------------------------------------------------------------------
1 | package List.Test;
2 | import static org.junit.Assert.*;
3 |
4 | import java.util.*;
5 |
6 | import org.junit.Before;
7 | import org.junit.Test;
8 |
9 | import List.MyList.MyList;
10 | import List. MyList.SingleList;
11 | public class ListTest {
12 | MyList il;
13 | MyList sl;
14 |
15 | ArrayList testInt = new ArrayList();
16 | ArrayList testStr = new ArrayList();
17 |
18 | void init()
19 | {
20 | il = new SingleList();
21 | int [] v= {1,2,3,9,5,8};
22 | for(int i:v) {il.insert(i);testInt.add(i);}
23 |
24 | sl = new SingleList();
25 | String [] strs = {"hello","nice","nee you","mei guo"};
26 | for(String s:strs) {sl.insert(s);testStr.add(s);}
27 | }
28 | @Before
29 | public void setUp() throws Exception {
30 | init();
31 | }
32 | public void assertEqual()
33 | {
34 | assertTrue(il.equalsList(testInt));
35 | assertTrue(sl.equalsList(testStr));
36 | }
37 |
38 | @Test
39 | public void test() {
40 | assertEqual();
41 |
42 | for(int i=0;i