当前位置:网站首页>Day code 300 lines learning notes day 22

Day code 300 lines learning notes day 22

2022-06-11 02:09:00 Leeyz_ one

1. Binary tree storage

Code :

package tree;

import java.util.Arrays;
import tree.BinaryCharTree;


public class CircleObjectQueue2 {
	//  The node value obtained by breadth first traversal 
	char[] valuesArray;

	//  Index of complete binary tree 
	int[] indicesArray;

	//  Convert tree to data array , Including a  char  Array and a  int  Array .  The results are stored in two member variables .
	public void toDataArrays() {
		//  Initialize array   ????

		/*int tempLength = getNumNodes();
		valuesArray = new char[tempLength];
		indicesArray = new int[tempLength];
		int i = 0;
        */

		//  Traversal and transformation at the same time 
		CircleObjectQueue tempQueue = new CircleObjectQueue();//  Instantiation 
		tempQueue.enqueue(this);//  The team 
		CircleIntQueue tempIntQueue = new CircleIntQueue();
		tempIntQueue.enqueue(0);//  The team 

		/*
		 * ??????????????
		 */
		BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
		int tempIndex = tempIntQueue.dequeue();
		while (tempTree != null) {
			valuesArray[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();
			tempIndex = tempIntQueue.dequeue();
		} // Of while
	}// Of toDataArrays

	// main
	public static void main(String args[]) {
		// CircleObjectQueue tempQueue = new CircleObjectQueue();
		BinaryCharTree tempTree = BinaryCharTree.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.valuesArray));
		System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));

	}// Of main
}

2. summary

People are confused , There was a problem in calling functions of other classes , for example 56 In line , The source code is not added to the right BinaryCharTree Name of class , If you don't add... To the right of the equal sign BinaryCharTree The name of this class , An error will be reported if this function is not defined . Another example is 16 Row initializing array ,18 It seems that whether the line is not running or not will not affect the final result ? Because this line calls getNumNodes When this function , There are also some problems , It is also shown in CircleObjectQueue2 This function is not defined in this class . Today, I will focus on the code , Calling functions in other classes is not very clear , Some can be called successfully , Some calls are unsuccessful , Add the calculation traversal and numbering , Stupid ... If you say will 22 The code of the day , Just put it all in 21 Days of code , It can also be run directly , After all, all the functions it calls are 21 Created within days ...

原网站

版权声明
本文为[Leeyz_ one]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/03/202203020618428459.html