日撸代码300行:第24天(二叉树的建立)

代码来自闵老师”日撸 Java 三百行(21-30天)“,链接:https://blog.csdn.net/minfanphd/article/details/116975721

    /**
	 * ***********************************************************************
	 * The second constructor. The parameters must be correct since no
	 * validity check is undertaken.
	 * 
	 * @param paraDataArray   The array for data.
	 * @param paraIndicesArray   The array for indices.
	 * *********************************************************************** 
	 */
	public BinaryCharTree(char[] paraDataArray, int[] paraIndicesArray ) {
		// TODO Auto-generated constructor stub
		//Step 1. Use a sequential list to store all nodes.
		int tempNumNodes = paraDataArray.length;
		BinaryCharTree[] tempAllNodes = new BinaryCharTree[tempNumNodes];
		
		for (int i = 0; i < tempNumNodes; i++) {	
			tempAllNodes[i] = new BinaryCharTree(paraDataArray[i]);
		}//of for i
		
		//Step 2. Link these nodes.
		for (int i = 0; i < tempNumNodes; i++) {
			for (int j = 0; j < i; j++) {
				if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 1) {
					tempAllNodes[j].leftChild = tempAllNodes[i];
					break;
				}//of if
				if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2) {
					tempAllNodes[j].rightChild = tempAllNodes[i];
					break;
				}//of if
				
			}//of for j
		}//of for i
		
		//Step 3. The root is the first node.
		value =tempAllNodes[0].value;
		leftChild = tempAllNodes[0].leftChild;
		rightChild = tempAllNodes[0].rightChild;
	}//of the second constructor
	
	/**
	 * *********************************************************
	 * The entrance of the program.
	 * 
	 * @param args  Not used now
	 * *********************************************************
	 */
	public static void main(String args[]) {
		BinaryCharTree tempTree = manualConstructTree();
		
		System.out.println("\r\nPre-order visit:");
		tempTree.preOrderVisit();
		
		System.out.println("\r\nIn-order visit:");
		tempTree.inOrderVisit();
		
		System.out.println("\r\nPost-order visit:");
		tempTree.postOrderVisit();
		
		int tempDepth = tempTree.getDepth();
		System.out.println("\nThe depth is: " + tempDepth);
		
		System.out.println("\rThe number of nodes for the binary tree 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));
		
		tempTree.toDataArraysObjectQueue();
		System.out.println("Only object queue.");
	   	System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
	   	System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
	   	
		tempTree.toDataArrayGenericQueue();
		System.out.println("Generic queue.");
	   	System.out.println("The values are: " + Arrays.toString(tempTree.valueArray));
	   	System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
	   	
	   	//char[] tempCharArray = {'A','B','C','D','E','F'};
	   	//int[] tempIndicesArray = {0, 1, 2, 4, 5, 12};
	   	char[] tempCharArray = {'a','b','c','d','e','f','g'};
	   	int[] tempIndicesArray = {0, 1, 2, 4, 5, 9, 10};
	   	BinaryCharTree tempTree2 = new BinaryCharTree(tempCharArray, tempIndicesArray);
	   	
		System.out.println("\r\nPre-order visit:");
		tempTree2.preOrderVisit();	
		System.out.println("\r\nIn-order visit:");
		tempTree2.inOrderVisit();
		System.out.println("\r\nPost-order visit:");
		tempTree2.postOrderVisit();

	}//of main

今天的代码没啥,主要是理解。首先声明一个二叉树类型的数组(顺序表),来装所有的节点。接下来将所有节点连接起来,方法是通过叶子找根。给定一个数i,在比自己小的数里总能找到一个数j,等于i减去1(或2)除以2。换言之就是总能找到一个数j乘以2加1(或2)等于i,那么i就是j的左(或右)孩子节点。最后一步别忘了根节点设为第一个节点。

上一篇:石子合并


下一篇:日撸代码300行:第20天(小结)