关于霍夫曼树与霍夫曼编码的相关知识在另一篇博文中有较为详细的阐述,详情请移步前文
本篇侧重代码干货,实现涉及到很多的类的操作,想查看更多请移步我的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
字符权重类