Java学习笔记-基于霍夫曼编码的文本文件压缩与解压缩

关于霍夫曼树与霍夫曼编码的相关知识在另一篇博文中有较为详细的阐述,详情请移步前文本篇侧重代码干货,实现涉及到很多的类的操作,想查看更多请移步我的GitHub二叉树节点类,提供对于节点的增删改查相关操作package Code;public class TreeNode { private Object item; private TreeNode leftChild; privat...
摘要由CSDN通过智能技术生成

关于霍夫曼树与霍夫曼编码的相关知识在另一篇博文中有较为详细的阐述,详情请移步前文

本篇侧重代码干货,实现涉及到很多的类的操作,想查看更多请移步我的GitHub

二叉树节点类,提供对于节点的增删改查相关操作

package Code;

public class TreeNode {
  private Object item;
  private TreeNode leftChild;
  private TreeNode rightChild;

  public TreeNode(Object newItem) {
  // Initializes tree node with item and no children.
    item = newItem;
    leftChild  = null;
    rightChild = null;
  }  // end constructor

  public TreeNode(Object newItem,
                  TreeNode left, TreeNode right) {
  // Initializes tree node with item and
  // the left and right children references.
    item = newItem;
    leftChild  = left;
    rightChild = right;
  }  // end constructor

  public Object getItem() {
  // Returns the item field.
    return item;
  }  // end getItem

  public void setItem(Object newItem) {
  // Sets the item field to the new value newItem.
  item  = newItem;
  }  // end setItem

  public TreeNode getLeft() {
  // Returns the reference to the left child.
    return leftChild;
  }  // end getLeft

  public void setLeft(TreeNode left) {
  // Sets the left child reference to left.
    leftChild  = left;
  }  // end setLeft

  public TreeNode getRight() {
  // Returns the reference to the right child.
    return rightChild;
  }  // end getRight

  public void setRight(TreeNode right) {
  // Sets the right child reference to right.
    rightChild  = right;
  }  // end setRight


  public String toString() {
	  return "" + item;
  }

  public boolean isLeaf() {
    return ((getLeft() == null) && (getRight() == null));
  }

}  // end TreeNode

二叉树基类,提供基本的二叉树操作

package Code;

public abstract class BinaryTreeBasis {
  protected TreeNode root;

  public BinaryTreeBasis() {
    root = null;
  }  // end default constructor

  public BinaryTreeBasis(Object rootItem) {
    root = new TreeNode(rootItem, null, null);
  }  // end constructor

  public boolean isEmpty() {
// Returns true if the tree is empty, else returns false.
    return root == null;
  }  // end isEmpty

  public void makeEmpty() {
// Removes all nodes from the tree.
    root = null;
  }  // end makeEmpty

  public Object getRootItem() throws TreeException {
// Returns the item in the tree�s root.
    if (root == null) {
      throw new TreeException("TreeException: Empty tree");
    }
    else {
      return root.getItem();
    }  // end if
  }  // end getRootItem

  public TreeNode getRoot() throws TreeException {
// additional method to return the root as TreeNode.
      return root;
  }  // end getRoot

}  // end BinaryTreeBasis

继承二叉树基类构建的霍夫曼树/最优二叉树

package Code;

public class BinaryTree extends BinaryTreeBasis {
  public BinaryTree() {
  }  // end default constructor

  public BinaryTree(Object rootItem) {
    super(rootItem);
  }  // end constructor

  public BinaryTree(Object rootItem, 
                    BinaryTree leftTree, 
                    BinaryTree rightTree) {
    root = new TreeNode(rootItem, null, null);
    attachLeftSubtree(leftTree);
    attachRightSubtree(rightTree);
  }  // end constructor

  public void setRootItem(Object newItem) {
    if (root != null) {
      root.setItem(newItem);
    }
    else {
      root = new TreeNode(newItem, null, null);
    }  // end if
  }  // end setRootItem

  public void attachLeft(Object newItem) {
    if (!isEmpty() && root.getLeft() == null) {
      // assertion: nonempty tree; no left child
      root.setLeft(new TreeNode(newItem, null, null));
    }  // end if
  }  // end attachLeft

  public void attachRight(Object newItem) {
    if (!isEmpty() && root.getRight() == null) {
      // assertion: nonempty tree; no right child
      root.setRight(new TreeNode(newItem, null, null));
    }  // end if
  }  // end attachRight

  public void attachLeftSubtree(BinaryTree leftTree) 
                                throws TreeException {
    if (isEmpty()) {
      throw new TreeException("TreeException:  Empty tree");
    }
    else if (root.getLeft() != null) {
      // a left subtree already exists; it should have been 
      // deleted first
      throw new TreeException("TreeException: " + 
                           "Cannot overwrite left subtree");
    }
    else {
      // assertion: nonempty tree; no left child
      root.setLeft(leftTree.root);
      // don't want to leave multiple entry points into 
      // our tree
      leftTree.makeEmpty(); 
    }  // end if
  }  // end attachLeftSubtree

  public void attachRightSubtree(BinaryTree rightTree)  
                                 throws TreeException {
    if (isEmpty()) {
      throw new TreeException("TreeException:  Empty tree");
    }
    else if (root.getRight() != null) {
      // a right subtree already exists; it should have been 
      // deleted first
      throw new TreeException("TreeException: " + 
                          "Cannot overwrite right subtree");
    }
    else {
      // assertion: nonempty tree; no right child
      root.setRight(rightTree.root);
      // don't want to leave multiple entry points into 
      // our tree
      rightTree.makeEmpty(); 
    }  // end if
  }  // end attachRightSubtree
  
  protected BinaryTree(TreeNode rootNode) {
    root = rootNode;
  }  // end protected constructor

  public BinaryTree detachLeftSubtree()  
                         throws TreeException {
    if (isEmpty()) {
      throw new TreeException("TreeException:  Empty tree");
    }
    else {
      // create a new binary tree that has root's left 
      // node as its root
      BinaryTree leftTree;
      leftTree = new BinaryTree(root.getLeft());
      root.setLeft(null);
      return leftTree;
    }  // end if
  }  // end detachLeftSubtree

  public BinaryTree detachRightSubtree() 
                         throws TreeException {
    if (isEmpty()) {
      throw new TreeException("TreeException:  Empty tree");
    }
    else {
      BinaryTree rightTree;
      rightTree = new BinaryTree(root.getRight());
      root.setRight(null);
      return rightTree;
    }  // end if
  }  // end detachRightSubtree

} // end BinaryTree

二叉树异常类(养成良好的编程习惯~)

package Code;

public class TreeException extends RuntimeException {
  public TreeException(String s) {
    super(s);
  }  // end constructor
} // end TreeException

字符权重类

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值