今天是学习java的第22天,今天学习的是树的存储。
对于树的储存,用到了循环队列。具体操作是建立了两个队列,一个存值,另一个存对应的在二叉树中的序号。
Code1:对之前写的循环队列进行一个改写,使其适合树的储存。
package datastructure; public class CircleObjectQueue { /** * The tatal space. One space can't used. */ public static final int TOTAL_SPACE = 10; Object[] data; /** * 队列的头和尾。 */ int head; int tail; public CircleObjectQueue() { data = new Object[TOTAL_SPACE]; head = 0; tail = 0; } // Of the first constructure public void enQueue(Object paraValue) { if ((tail + 1) % TOTAL_SPACE == head) { System.out.println("Queue full."); return; } // Of if data[tail % TOTAL_SPACE] = paraValue; tail++; } // Of enQueue public Object deQueue() { if (tail == head) { return null; } Object resultValue = data[head % TOTAL_SPACE]; head++; return resultValue; } // Of deQueue public String toString() { String resultString = ""; if (head == tail) { return "empty"; } // Of if for (int i = head; i < tail - 1; i++) { resultString += data[i % TOTAL_SPACE] + ", "; } // Of for i resultString += data[(tail - 1) % TOTAL_SPACE]; return resultString; } // Of toString public static void main(String[] args) { CircleObjectQueue tempQueue = new CircleObjectQueue(); } // Of main }
Code 2 存储二叉树
package datastructure; import java.util.Arrays; import datastructure.CircleObjectQueue.*; public class BinaryCharTree { /** * The value of node. */ char value; /** * 二叉树的左孩子。 */ BinaryCharTree leftChild; /** * 二叉树的右孩子。 */ BinaryCharTree rightChild; /** ************** * The first constructor. * * @param paraValue The given value. ************** */ public BinaryCharTree(char paraValue) { value = paraValue; leftChild = null; rightChild = null; } // Of the first construe public static BinaryCharTree manualConstructTree() { // step 1. Creat root. BinaryCharTree resultTree = new BinaryCharTree('a'); // step 2. creat children and linked them. BinaryCharTree tempTreeB = new BinaryCharTree('b'); BinaryCharTree tempTreeC = new BinaryCharTree('c'); BinaryCharTree tempTreeD = new BinaryCharTree('d'); BinaryCharTree tempTreeE = new BinaryCharTree('e'); BinaryCharTree tempTreeF = new BinaryCharTree('f'); BinaryCharTree tempTreeG = new BinaryCharTree('g'); // Link strat resultTree.leftChild = tempTreeB; resultTree.rightChild = tempTreeC; tempTreeB.rightChild = tempTreeD; tempTreeC.leftChild = tempTreeE; tempTreeD.leftChild = tempTreeF; tempTreeD.rightChild = tempTreeG; return resultTree; } // Of manualConstructTree /** ************** * 先根、中根、后根遍历树。 ************** */ public void preOrderVisit() { System.out.print(value + " "); if (leftChild != null) { leftChild.preOrderVisit(); } // Of if if (rightChild != null) { rightChild.preOrderVisit(); } // Of if } // Of preOrderVisit public void inOrderVisit() { if (leftChild != null) { leftChild.inOrderVisit(); } // Of if System.out.print(value + " "); if (rightChild != null) { rightChild.inOrderVisit(); } // Of if } public void postOrderVisit() { if (leftChild != null) { leftChild.postOrderVisit(); } // Of if if (rightChild != null) { rightChild.postOrderVisit(); } // Of if System.out.print(value + " "); } public int getDepth() { if ((leftChild == null) && (rightChild == null)) { return 1; } // Of if // check left int tempLeftDepth = 0; if (leftChild != null) { tempLeftDepth = leftChild.getDepth(); } // Of if // Check right int tempRightDepth = 0; if (rightChild != null) { tempRightDepth = rightChild.getDepth(); } // Of if if (tempLeftDepth >= tempRightDepth) { return tempLeftDepth + 1; } else { return tempRightDepth + 1; } // Of if } // Of getDepth // 遍历时存储节点中的值。 char[] valueArray; // 二叉树的索引。 int[] indicesArray; public void toDataArrays() { int tempLength = getNumNodes(); valueArray = new char[tempLength]; indicesArray = new int[tempLength]; int i = 0; // Traverse and convert at the same time. CircleObjectQueue tempQueue = new CircleObjectQueue(); tempQueue.enQueue(this); CircleObjectQueue tempIntQueue = new CircleObjectQueue(); tempIntQueue.enQueue(0); BinaryCharTree tempTree = (BinaryCharTree) tempQueue.deQueue(); int tempIndex = (int) tempIntQueue.deQueue(); while (tempTree != null) { valueArray[i] = tempTree.value; indicesArray[i] = tempIndex; i++; if (tempTree.leftChild != null) { tempQueue.enQueue(tempTree.leftChild); tempIntQueue.enQueue(tempIndex * 2 + 1); } // Of if if (tempTree.rightChild != null) { tempQueue.enQueue(tempTree.rightChild); tempIntQueue.enQueue(tempIndex * 2 + 2); } // Of if tempTree = (BinaryCharTree) tempQueue.deQueue(); if (tempTree != null) tempIndex = (int) tempIntQueue.deQueue(); } // Of while } public int getNumNodes() { if ((leftChild == null) && (rightChild == null)) { return 1; } // Of if int tempLeftNodes = 0; if (leftChild != null) { tempLeftNodes = leftChild.getNumNodes(); } // Of if int tempRightNodes = 0; if (rightChild != null) { tempRightNodes = rightChild.getNumNodes(); } // Of if return tempLeftNodes + tempRightNodes + 1; }// Of getNumNodes /** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ public static void main(String args[]) { BinaryCharTree tempTree = manualConstructTree(); System.out.println("\r\nPreorder visit:"); tempTree.preOrderVisit(); System.out.println("\r\nIn-order visit:"); tempTree.inOrderVisit(); System.out.println("\r\nPost-order visit:"); tempTree.postOrderVisit(); System.out.println("\r\n\r\nThe depth is: " + tempTree.getDepth()); System.out.println("The number of nodes is: " + tempTree.getNumNodes()); tempTree.toDataArrays(); System.out.println("The values are: " + Arrays.toString(tempTree.valueArray)); System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray)); }// Of main }
运行结果: