文件的压缩和解压(其余部分复用上面)
//将一个文件压缩
/**
* @param srcFile 要压缩的文件的全路径
* @param dstFile 压缩后要保存在哪里的全路径
*/
public static void zipFile(String srcFile, String dstFile) throws IOException {
FileInputStream is = null;
FileOutputStream os = null;
ObjectOutputStream oos = null;
try {
is = new FileInputStream(srcFile);
byte[] b = new byte[is.available()];//创建一个和原文件大小一样的byte数组
is.read(b);
byte[] huffmanCodeBytes = huffmanZip(b);
//创建文件的输出流,存放压缩文件
os = new FileOutputStream(dstFile);
//创建一个和文件输出流相关的对象输出流
oos = new ObjectOutputStream(os);
//以对象流的方式写入赫夫曼编码,为了恢复源文件时使用
oos.writeObject(huffmanCodeBytes);
oos.writeObject(huffmanCodes);//注意要将赫夫曼编码表也写入进去
} catch (IOException e) {
e.printStackTrace();
} finally {//先开后关
if (oos != null) {
oos.close();
}
if (os != null) {
os.close();
}
if (is != null) {
is.close();
}
}
}
/**
* 将压缩文件进行解压
* @param zipFile 要解压的压缩文件
* @param dstFile 要解压到的目标路径
*/
public static void unzipFile(String zipFile,String dstFile) throws IOException, ClassNotFoundException {
FileInputStream is = null;
ObjectInputStream ois = null;
OutputStream os =null;
try {
is=new FileInputStream(zipFile);
//创建一个和is关联的对象输入流
ois = new ObjectInputStream(is);
//注:存的顺序是什么,取的顺序就是什么
byte[] huffmanCodeBytes = (byte[]) ois.readObject();
Map<Byte,String> huffmanCodes = (Map<Byte, String>) ois.readObject();
//解码
byte[] bytes = decode(huffmanCodes, huffmanCodeBytes);
//输出
os = new FileOutputStream(dstFile);
os.write(bytes);
} catch (FileNotFoundException e) {
e.getMessage();
} finally {
if (os!=null){
os.close();
}
if (ois!=null){
ois.close();
}
if (is!=null){
is.close();
}
}
}
}

八、二叉排序树

1、二叉排序树的创建和遍历
2、二叉排序树的删除



右子树的最小值刚好满足比左子树的全部值大,比右子树剩余节点都小,按照中序遍历后仍然是顺序的
左子树的最大值同理
注意:由于删除总共有3种情况,因此我们只需要写两种情况,剩下的else自然是最后一种情况,通过这样可以简化我们的判断条件
package Tree.BinarySortTree;
import java.util.Arrays;
//二叉排序树
public class Demo {
public static void main(String[] args) {
int[] arr = {7, 3};
BinarySortTree tree = new BinarySortTree();
for (int item : arr) {
tree.add(new Node(item));
}
// tree.infixOrder();
//测试删除叶子节点
tree.del(7);
tree.infixOrder();
}
}
class BinarySortTree {
private Node root;
//添加节点
public void add(Node node) {
if (root == null) {//root为空时直接指向node
root = node;
} else {
root.add(node);
}
}
//中序遍历
public void infixOrder() {
if (root != null) {
root.infixOrder();
} else {
System.out.println("当前二叉排序树为空,无法遍历");
}
}
//查找要删除的节点
public Node searchDel(int value){
if (root!=null){
return root.searchDel(value);
} else {
return null;
}
}
//查找要删除节点的父节点
public Node searchParent(int value){
if (root!=null){
return root.searchParent(value);
} else {
return null;
}
}
//删除节点
public void del(int value){
if (root==null){
return;
}
else {
Node del = root.searchDel(value);
if (del==null){//未找到要删除的节点
return;
}
if (root.left==null&&root.right==null){//能走到这里未返回,并且只有一个节点,说明要删除的就是根节点,直接置空即可
//同时,根节点是唯一一个没有父节点的节点,后续也就不用再考虑父节点为空的情况了
root=null;
}
Node parent = root.searchParent(value);
if (del.right==null && del.left==null){//要删除的节点是叶子节点
//注:不能直接将del置空,del只是引用了对应那个节点的地址,del=null只是del这个变量被修改,对应的节点依旧存在
if (parent.left==del){
parent.left=null;
}
if (parent.right==del){
parent.right=null;
}
} else if (del.right!=null && del.left!=null){//要删除有2颗子树的节点
Node temp = del;
while (temp.left!=null){
temp=temp.left;
}
del(temp.value);//这里需要用一个变量保存右子树最小节点的数值,是因为如果直接给要删除的节点先赋值的话会造成死递归导致栈溢出
int val = temp.value;
del.value = temp.value;//del本身是一个引用,但是通过.属性的方式,修改的是真实存在树种的节点的value,因此是生效的
}
else {//此时del的左和右只会有一个为空
if (parent==null){//根节点,且只有一颗子树
if (del.right!=null){
root=del.right;
} else {
root=del.left;
}
}
else {
if (del.left!=null){
if (parent.left==del){
parent.left=del.left;
}
if (parent.right==del){
parent.right=del.left;
}
} else {
if (parent.left==del){
parent.left=del.right;
}
if (parent.right==del){
parent.right=del.right;
}
}
}
}
}
}
}
class Node {
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node{" +
"value=" + value +
'}';
}
public void add(Node node) {
if (node == null) {
return;
}
//判断传入的节点和当前子树的根节点的value值的大小
if (node.value <= this.value) {
if (this.left == null) {//当前左子节点为空直接关联,否则向左子树递归添加方法
this.left = node;
} else {
this.left.add(node);
}
} else {
if (this.right == null) {
this.right = node;
} else {
this.right.add(node);
}
}
}
//中序遍历
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
//查找要删除的节点
public Node searchDel(int value) {
if (this.value == value) {
return this;
} else if (value < this.value) {//小于当前节点时,向左子树递归查找
if (this.left != null) {
return this.left.searchDel(value);//问题:这个return不加可以吗
}
return null;
} else {
if (this.right != null) {
return this.right.searchDel(value);
}
return null;
}
}
//查找要删除节点的父节点
public Node searchParent(int value) {
if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
return this;
} else {
//如果要查找的值比当前节点小,并且左子节点不为空,则向左递归
if (this.left != null && value <= this.value) {
return this.left.searchParent(value);
} else if (this.right != null && value > this.value) {
return this.right.searchParent(value);
} else {
return null;//没有找到父节点(根节点)
}
}
}
}