【发布时间】:2016-03-09 08:33:33
【问题描述】:
我正在尝试用一串字母创建一棵平衡树。如果你把“ABCDE”作为字符串,你的代码应该会给出类似这样的输出。
输入:“ABCDE”
输出:
.......................................................
+
+ E
+ + -- --
A B C D -- -- -- --
.......................................................
这本书建议我首先创建一个单节点树数组,其根将是字符串的每个字符。然后,从每对单节点树中创建一个三节点树,为根创建一个新的 + 节点,这将产生一个三节点树的森林。
我知道这个问题是最终编写 Huffman 树的垫脚石。
我无法将三节点树放回具有单节点树的数组中,然后通过组合两棵三节点树来生成一棵 7 节点树,依此类推。
下面是我的代码,
import java.util.*; // for Stack class
class StringNode {
public char iData; // data item (key)
public StringNode leftChild; // this node's left child
public StringNode rightChild; // this node's right child
StringNode(char d) {
iData = d;
}
public void displayNode() // display ourself
{
System.out.print('{');
System.out.print(iData);
System.out.print("} ");
}
} // end class Node
class STree {
private StringNode root; // first node of tree
public String sequence;
// -------------------------------------------------------------
public STree() // constructor
{
root = null;
} // no nodes in tree yet
public void makeBalanceTree() // creating a balanced tree
{
StringNode array[] = new StringNode[sequence.length()];
for (int i = 0; i < sequence.length(); i++)
array[i] =
new StringNode(sequence.charAt(i)); //fill array with node holding each character as key
STree forest[] = new STree[array.length]; //make a forest of trees
for (int j = 0; j < array.length; j++) { //store each node as the root of the tree
forest[j] = new STree();
forest[j].root = array[j];
}
int count = sequence.length();
while (count == 0) {}
}
public void displayTree() {
Stack globalStack = new Stack();
globalStack.push(root);
int nBlanks = 32;
boolean isRowEmpty = false;
System.out.println("......................................................");
while (isRowEmpty == false) {
Stack localStack = new Stack();
isRowEmpty = true;
for (int j = 0; j < nBlanks; j++) System.out.print(' ');
while (globalStack.isEmpty() == false) {
StringNode temp = (StringNode) globalStack.pop();
if (temp != null) {
System.out.print(temp.iData);
localStack.push(temp.leftChild);
localStack.push(temp.rightChild);
if (temp.leftChild != null || temp.rightChild != null) isRowEmpty = false;
} else {
System.out.print("--");
localStack.push(null);
localStack.push(null);
}
for (int j = 0; j < nBlanks * 2 - 2; j++) System.out.print(' ');
} // end while globalStack not empty
System.out.println();
nBlanks /= 2;
while (localStack.isEmpty() == false) globalStack.push(localStack.pop());
} // end while isRowEmpty is false
System.out.println("......................................................");
} // end displayTree()
} // end class Tree
public class StringTreeApp {
public static void main(String[] args) {
int value;
STree theTree = new STree();
theTree.sequence = "ABCDE";
theTree.makeBalanceTree();
theTree.displayTree();
} // end main()
} // end class TreeApp
【问题讨论】: