文件的压缩和解压(其余部分复用上面)
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()];
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();
}
}
}
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);
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.del(7);
tree.infixOrder();
}
}
class BinarySortTree {
private Node root;
public void add(Node node) {
if (root == null) {
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){
if (parent.left==del){
parent.left=null;
}
if (parent.right==del){
parent.right=null;
}
} else if (del.right!=null && del.left!=null){
Node temp = del;
while (temp.left!=null){
temp=temp.left;
}
del(temp.value);
int val = temp.value;
del.value = temp.value;
}
else {
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;
}
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 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;
}
}
}
}
|