/** ********************* * Get the depth of the binary tree. * * @return The depth. It is 1 if there is only one node, i.e., the root. ********************* */ publicintgetDepth() { // It is a leaf. if ((leftChild == null) && (rightChild == null)) { return1; } // Of if
// The depth of the left child. inttempLeftDepth=0; if (leftChild != null) { tempLeftDepth = leftChild.getDepth(); } // Of if
// The depth of the right child. inttempRightDepth=0; if (rightChild != null) { tempRightDepth = rightChild.getDepth(); } // Of if
// The depth should increment by 1. if (tempLeftDepth >= tempRightDepth) { return tempLeftDepth + 1; } else { return tempRightDepth + 1; } // Of if }// Of getDepth
/** ********************* * Get the number of nodes. * * @return The number of nodes. ********************* */ publicintgetNumNodes() { // It is a leaf. if ((leftChild == null) && (rightChild == null)) { return1; } // Of if
// The number of nodes of the left child. inttempLeftNodes=0; if (leftChild != null) { tempLeftNodes = leftChild.getNumNodes(); } // Of if
// The number of nodes of the right child. inttempRightNodes=0; if (rightChild != null) { tempRightNodes = rightChild.getNumNodes(); } // Of if
// The total number of nodes. return tempLeftNodes + tempRightNodes + 1; }// Of getNumNodes
/** * Binary tree with char type elements. * * @author Shihuai Wen Email:wshysxcc@outlook.com */ publicclassBinaryCharTree { /** * The value in char. */ char value;
/** * The left child. */ BinaryCharTree leftChild;
/** * The right child. */ BinaryCharTree rightChild;
/** ********************* * The first constructor. * * @param paraName The value. ********************* */ publicBinaryCharTree(char paraName) { value = paraName; leftChild = null; rightChild = null; }// Of the constructor
/** ********************* * Manually construct a tree. Only for testing. ********************* */ publicstatic BinaryCharTree manualConstructTree() { // Step 1. Construct a tree with only one node. BinaryCharTreeresultTree=newBinaryCharTree('a');
// Step 2. Construct all nodes. The first node is the root. // BinaryCharTreeNode tempTreeA = resultTree.root; BinaryCharTreetempTreeB=newBinaryCharTree('b'); BinaryCharTreetempTreeC=newBinaryCharTree('c'); BinaryCharTreetempTreeD=newBinaryCharTree('d'); BinaryCharTreetempTreeE=newBinaryCharTree('e'); BinaryCharTreetempTreeF=newBinaryCharTree('f'); BinaryCharTreetempTreeG=newBinaryCharTree('g');
if (leftChild != null) { leftChild.preOrderVisit(); } // Of if
if (rightChild != null) { rightChild.preOrderVisit(); } // Of if }// Of preOrderVisit
/** ********************* * In-order visit. ********************* */ publicvoidinOrderVisit() { if (leftChild != null) { leftChild.inOrderVisit(); } // Of if
System.out.print("" + value + " ");
if (rightChild != null) { rightChild.inOrderVisit(); } // Of if }// Of inOrderVisit
/** ********************* * Post-order visit. ********************* */ publicvoidpostOrderVisit() { if (leftChild != null) { leftChild.postOrderVisit(); } // Of if
if (rightChild != null) { rightChild.postOrderVisit(); } // Of if
System.out.print("" + value + " "); }// Of postOrderVisit
/** ********************* * Get the depth of the binary tree. * * @return The depth. It is 1 if there is only one node, i.e., the root. ********************* */ publicintgetDepth() { // It is a leaf. if ((leftChild == null) && (rightChild == null)) { return1; } // Of if
// The depth of the left child. inttempLeftDepth=0; if (leftChild != null) { tempLeftDepth = leftChild.getDepth(); } // Of if
// The depth of the right child. inttempRightDepth=0; if (rightChild != null) { tempRightDepth = rightChild.getDepth(); } // Of if
// The depth should increment by 1. if (tempLeftDepth >= tempRightDepth) { return tempLeftDepth + 1; } else { return tempRightDepth + 1; } // Of if }// Of getDepth
/** ********************* * Get the number of nodes. * * @return The number of nodes. ********************* */ publicintgetNumNodes() { // It is a leaf. if ((leftChild == null) && (rightChild == null)) { return1; } // Of if
// The number of nodes of the left child. inttempLeftNodes=0; if (leftChild != null) { tempLeftNodes = leftChild.getNumNodes(); } // Of if
// The number of nodes of the right child. inttempRightNodes=0; if (rightChild != null) { tempRightNodes = rightChild.getNumNodes(); } // Of if
// The total number of nodes. return tempLeftNodes + tempRightNodes + 1; }// Of getNumNodes
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { BinaryCharTreetempTree= 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()); }// Of main } // Of class BinaryCharTree
七、运行截图
二叉树的存储
一、描述
二叉树的存储并非一个简单的问题. 引用 (指针)
是无法存储到文件里面的.
当然要是不考虑复杂性的话, 我首选就是拿 XML 或者 JSON
的文件格式来存储, 这不仅可以解决二叉树的存储问题,
还能在一个文件里面实现多个二叉树的存储.但是如果二叉树的深度过高就会出现看起来很奇怪的文件格式.
/** * Circle Object queue. * * @author Shihuai Wen Email:wshysxcc@outlook.com */ publicclassCircleObjectQueue { /** * The total space. One space can never be used. */ publicstaticfinalintTOTAL_SPACE=10;
/** * The data. */ Object[] data;
/** * The index of the head. */ int head;
/** * The index of the tail. */ int tail;
/** ******************* * The constructor ******************* */ publicCircleObjectQueue() { data = newObject[TOTAL_SPACE]; head = 0; tail = 0; }// Of the first constructor
/** ********************* * Enqueue. * * @param paraValue The value of the new node. ********************* */ publicvoidenqueue(Object paraValue) { if ((tail + 1) % TOTAL_SPACE == head) { System.out.println("Queue full."); return; } // Of if
data[tail % TOTAL_SPACE] = paraValue; tail++; }// Of enqueue
/** ********************* * Dequeue. * * @return The value at the head. ********************* */ public Object dequeue() { if (head == tail) { // System.out.println("No element in the queue"); returnnull; } // Of if
ObjectresultValue= data[head % TOTAL_SPACE];
head++;
return resultValue; }// Of dequeue
/** ********************* * Overrides the method claimed in Object, the superclass of any class. ********************* */ public String toString() { StringresultString="";
if (head == tail) { return"empty"; } // Of if
for (inti= head; i < tail; i++) { resultString += data[i % TOTAL_SPACE] + ", "; } // Of for i
return resultString; }// Of toString
/** ********************* * The entrance of the program. * * @param args Not used now. ********************* */ publicstaticvoidmain(String args[]) { CircleObjectQueuetempQueue=newCircleObjectQueue(); }// Of main
/** * The values of nodes according to breadth first traversal. */ char[] valuesArray;
/** * The indices in the complete binary tree. */ int[] indicesArray;
/** ******************** * Convert the tree to data arrays, including a char array and an int array. * The results are stored in two member variables. * * @see #valuesArray * @see #indicesArray ********************* */ publicvoidtoDataArrays() { //Initialize arrays. inttempLength= getNumNodes();
//Traverse and convert at the same time. CircleObjectQueuetempQueue=newCircleObjectQueue(); tempQueue.enqueue(this); CircleIntQueuetempIntQueue=newCircleIntQueue(); tempIntQueue.enqueue(0);