Day11——通用队列

前几天有遇到int类型的队列、char类型的队列还有以对象作为元素的队列,它们之间除了数据类型不同,其它都相同,之前我们都是一个类型对应一个类,今天就写一个通用队列。
老师的思路:Java中万物皆对象,我们只用写一个类型为Object的队列,使用时可以通过强制类型转换,得到我们想要的类型,所以我们可以直接用CircleObjectQueue来创建我们所需要的所有队列。

package day11;

import java.util.Arrays;

public class BinaryCharTree {

	/**
	 * The value in char.
	 */
	char value;

	/**
	 * The left child.
	 */
	BinaryCharTree leftChild;

	/**
	 * The right child.
	 */
	BinaryCharTree rightChild;

	/**
	 * 
	 *********************
	 * The first constructor.
	 * 
	 * @param paraName The value.
	 *********************
	 *
	 */
	public BinaryCharTree(char paraName) {
		value = paraName;
		leftChild = null;
		rightChild = null;
	}// Of the constructor

	/**
	 * 
	 *********************
	 * @Title: manualConstructTree
	 * @Description: TODO(Manually construct a tree. Only for testing.)
	 *
	 * @return A binary tree.
	 *********************
	 */
	public static BinaryCharTree manualConstructTree() {
		// Step 1. Construct a tree with only one node.
		BinaryCharTree resultTree = new BinaryCharTree('a');

		// Step 2. Construct all nodes. The first node is the root.
		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');

		// Step 3. Link all nodes.
		resultTree.leftChild = tempTreeB;
		resultTree.rightChild = tempTreeC;
		tempTreeB.rightChild = tempTreeD;
		tempTreeC.leftChild = tempTreeE;
		tempTreeD.leftChild = tempTreeF;
		tempTreeD.rightChild = tempTreeG;

		return resultTree;
	}// Of manualConstructTree

	/**
	 * 
	 *********************
	 * @Title: getNumNodes
	 * @Description: TODO(Get the number of nodes)
	 *
	 * @return The number of nodes.
	 *********************
	 *
	 */
	public int getNumNodes() {
		// It is a leaf.
		if (leftChild == null && rightChild == null) {
			return 1;
		} // Of if

		int leftChildNodes = 0, rightChildNodes = 0;

		// Get the number of nodes of the left child.
		if (leftChild != null) {
			leftChildNodes = leftChild.getNumNodes();
		} // Of if

		// Get the number of nodes of the right child.
		if (rightChild != null) {
			rightChildNodes = rightChild.getNumNodes();
		} // Of if

		// The total number of nodes.
		return leftChildNodes + rightChildNodes + 1;
	}// Of getNumNodes

	/**
	 * The values of nodes according to breadth first traversal.
	 */
	char[] valuesArray;

	/**
	 * The indices in the complete binary tree.
	 */
	int[] indicesArray;

	/**
	 * 
	 *********************
	 * @Title: toDataArrays
	 * @Description: TODO(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
	 *
	 *********************
	 *
	 */
	public void toDataArrays() {
		// Initialize arrays.
		int tempLength = getNumNodes();

		valuesArray = 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();
		Integer tempIndexInteger = Integer.valueOf(0);
		tempIntQueue.enqueue(tempIndexInteger);

		BinaryCharTree tempTree = (BinaryCharTree) tempQueue.dequeue();
		int tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
		while (tempTree != null) {
			valuesArray[i] = tempTree.value;
			indicesArray[i] = tempIndex;
			i++;

			if (tempTree.leftChild != null) {
				tempQueue.enqueue(tempTree.leftChild);
				tempIndexInteger = Integer.valueOf(tempIndex * 2 + 1);
				tempIntQueue.enqueue(tempIndexInteger);
			} // Of if

			if (tempTree.rightChild != null) {
				tempQueue.enqueue(tempTree.rightChild);
				tempIndexInteger = Integer.valueOf(tempIndex * 2 + 2);
				tempIntQueue.enqueue(tempIndexInteger);
			} // Of if

			tempTree = (BinaryCharTree) tempQueue.dequeue();
			if (tempTree == null) {
				break;
			} // Of if

			tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
		} // Of while
	}// Of toDataArrays

	/**
	 * 
	 *********************
	 * @Title: main
	 * @Description: TODO(The entrance of the program.)
	 *
	 * @param args Not used now.
	 *********************
	 *
	 */
	public static void main(String args[]) {
		BinaryCharTree tempTree = manualConstructTree();
		tempTree.toDataArrays();
		System.out.println("The values are: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("The indices are: " + Arrays.toString(tempTree.indicesArray));
	}// Of main

}// Of class BinaryCharTree

需要注意进行强制类型转换:
在这里插入图片描述
总结:今天的代码给了我一个新思路,我发现自己之前的想法挺呆的(在类里面声明多个不同类型的变量,根据调用者在创建对象实例时传入的类型,再为这个类型new一个数组。由于数据类型不同,所以每个功能都要写多个重载方法),虽然也能实现,但我觉得代码很丑。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值