数据结构与算法
一、数据结构
1、数据结构包括:线性结构与非线性结构。
线性结构:
(1)有顺序储存结构(数组,储存元素是连续的)和链式储存结构(链表,储存元素不一定是连续的)
(2)数组、队列、链表与栈
非线性结构:二维数据组、多维数组、广义表、树结构、图结构
2、稀疏数组
当一个数组大部分元素为0,或者为同一个值时,使用稀疏数组来保存该数组
(1)记录数组有几行几列,共有多少个不同值
(2)把不同值元素的行列与值记录到另外一个数组
二维数组转换稀疏数组思路:
1、遍历原始二维数组,得到有效数据个数sum,从而得到稀疏数组大小
2、根据sum就可以创建稀疏数组,a [sum+1] [3]
3、将二维数组有效数据按照 行、列、值的方式存入稀疏数组
4、io流存储稀疏数组
换稀疏数组转二维数组思路:
1、先读取稀疏数组第一行,得到原始二维数组的大小,比如上面 a [6] [7]
2、在读取稀疏数组后几行的数据,然后赋值给原始二维数组。
应用实例:
1、使用稀疏数组,来保存类似前面的二维数组 。如:棋盘存储、地图等等。
2、把稀疏数组存盘,可以重新恢复原来的二维数组。
代码实现:
棋盘存储、复盘(使用IO流存储稀疏数组):
import java.io.File;
import java.io.IOException;
/**
* @author 紫风
* @date 2022年01月27日 18:58
*/
public class SparseArrayTest {
public static void main(String[] args) throws IOException {
// 创建一个原始的二维数组 9*9
// 棋盘 :
// 0-----> 没有棋子
// 奇数----> 黑棋子
// 非零偶数-----> 白棋子
int chessArr1[][] = new int[9][9];
chessArr1[0][1] = 1;
chessArr1[1][2] = 2;
chessArr1[2][3] = 3;
chessArr1[3][4] = 4;
chessArr1[4][5] = 5;
chessArr1[5][6] = 6;
chessArr1[6][7] = 7;
chessArr1[7][8] = 8;
// 输出原始二维数组
System.out.println("输出原始二维数组");
for (int[] row : chessArr1) {
for (int data : row)
System.out.printf("%d\t", data);
System.out.println();
}
// 二维数组转稀疏数组
// 1、先遍历,先得到非零数据个数
int sum = 0;
for (int[] row : chessArr1)
for (int data : row) {
if (data != 0)
sum++;
}
System.out.println("原始二维数组有效个数" + sum);
// 2、创建对应的稀疏数组
int sparseArr[][] = new int[sum + 1][3];
// 3、赋值到稀疏数组
sparseArr[0][0] = chessArr1.length;
sparseArr[0][1] = chessArr1[0].length;
sparseArr[0][2] = sum;
// 4、遍历二维数组,非0数据赋值到稀疏数组
int count = 0;
for (int i = 0; i < chessArr1.length; i++) {
for (int j = 0; j < chessArr1.length; j++)
if (chessArr1[i][j] != 0) {
count++;
System.out.println(count);
sparseArr[count][0] = i;
sparseArr[count][1] = j;
sparseArr[count][2] = chessArr1[i][j];
}
}
// 5、保存稀疏数组
File file = new File("C:\\Users\\Administrator\\Desktop\\git\\suanfa\\sparsearray\\src\\chess.txt");
Writer writer = new Writer();
File file1 = writer.save(sparseArr, file);
// 6、读取文件获取稀疏数组
Reader reader = new Reader();
int[][] sparseArray = reader.getSparseArray(file1);
// 7、读取文件获取稀疏数组:输出稀疏数组
System.out.println("读取文件获取稀疏数组");
for (int[] row : sparseArray) {
for (int data : row)
System.out.printf("%d\t", data);
System.out.println();
}
// 8、恢复原始二维数组数组
// 1、要知道原始数组的大小,遍历稀疏数组
// 2、创建二维数组
int chessArr2[][] = new int[sparseArray[0][0]][sparseArray[0][1]];
// 3、遍历稀疏数组,赋值恢复的二维数组
for (int i = 1; i < sparseArray.length; i++)
chessArr2[sparseArray[i][0]][sparseArray[i][1]] = sparseArray[i][2];
// 4、输出恢复的二维数组
System.out.println("输出恢复的二维数组");
for (int[] row : chessArr2) {
for (int data : row)
System.out.printf("%d\t", data);
System.out.println();
}
}
}
IO流存储稀疏数组:
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
/**
* @author 紫风
* @date 2022年01月27日 18:55
*/
public class Writer {
public File save(int[][] sparseArr, File file) {
// 保存稀疏数组
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter(file));
//将压缩后的棋盘存入到chess.txt文件中
for (int[] row : sparseArr) {
for (int data : row)
// 以空格隔开
bw.write(data + " ");
// 换行
bw.write("\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null)
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
}
IO流读取稀疏数组:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
* @author 紫风
* @date 2022年01月27日 18:42
*/
public class Reader {
// 读取文件获得稀疏数组方法类
public int[][] getSparseArray(File file) throws IOException {
// 方法二、 通过读取chess.txt得到稀疏数组的大小
String line;
int value;
int row = 0;
// 用于读取第一行获取稀疏数组大小
BufferedReader br1 = new BufferedReader(new FileReader(file));
BufferedReader br2 = new BufferedReader(new FileReader(file));
// 先获取原始二维数组大小,及有效数据
// 读取第一行
String[] s = br1.readLine().split(" ");
value = Integer.parseInt(s[2]);
// 创建稀疏数组
int[][] sparseArr2 = new int[value + 1][3];
br1.close();
// 每次读取一行
while ((line = br2.readLine()) != null) {
// 以空格分割
String[] temp = line.split(" ");
// 遍历
for (int i = 0; i < temp.length; i++)
// 赋值到稀疏数组
sparseArr2[row][i] = Integer.parseInt(temp[i]);
row++;
}
br2.close();
return sparseArr2;
}
}
运行结果:
输出原始二维数组
0 1 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0
0 0 0 3 0 0 0 0 0
0 0 0 0 4 0 0 0 0
0 0 0 0 0 5 0 0 0
0 0 0 0 0 0 6 0 0
0 0 0 0 0 0 0 7 0
0 0 0 0 0 0 0 0 8
0 0 0 0 0 0 0 0 0
原始二维数组有效个数8
1
2
3
4
5
6
7
8
读取文件获取稀疏数组
9 9 8
0 1 1
1 2 2
2 3 3
3 4 4
4 5 5
5 6 6
6 7 7
7 8 8
输出恢复的二维数组
0 1 0 0 0 0 0 0 0
0 0 2 0 0 0 0 0 0
0 0 0 3 0 0 0 0 0
0 0 0 0 4 0 0 0 0
0 0 0 0 0 5 0 0 0
0 0 0 0 0 0 6 0 0
0 0 0 0 0 0 0 7 0
0 0 0 0 0 0 0 0 8
0 0 0 0 0 0 0 0 0
Process finished with exit code 0