日撸代码300行:第23天( 使用具有通用性的队列)

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

1、Integer 类在对象中包装了一个基本类型int的值;该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,同时还提供了其他一些处理int类型时非常有用的常量和方法。常用方法如下表:
在这里插入图片描述
2、循环队列存储对象,利用强制转换实现,代码如下。

	/**
	 * ***************************************************************
	 * Convert the tree to data arrays, including a char array and an int array.
	 * The results are stored in two member variables.
	 * 
	 * @see #valueArray
	 * @see #indicesArray
	 * ***************************************************************
	 */
	public void toDataArraysObjectQueue() {
		//Initialize arrays.
		int tempLength = getNumNodes();
		valueArray = new char[tempLength];
		indicesArray = new int[tempLength];
		int i = 0;
		
		//Initialize circle queue. 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) {
			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) {
				break;
			}//of if
			tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
		}//of while
		
	}//of toDataArraysObjectQueue

这里有一个问题没有想明白,为啥最后要加一个if语句判断temptree是否为空。自己也进行了测试,不判断强制转换会报错。前一天的二叉树存储toDataArray()中没有进行判断。
3、泛型实现

/**
	 * *******************************************************************************
	 * Convert the tree to data arrays, including a char array and an int array.
	 * The results are stored in two member variables.
	 * 
	 * @see #valueArray
	 * @see #indicesArray
	 * *******************************************************************************
	 */
	public void toDataArrayGenericQueue() {
		//Initialize arrays.
		int tempLength = getNumNodes();
		valueArray = new char[tempLength];
		indicesArray = new int[tempLength];
		int i = 0;
		
		//Initialize circle queue. Traverse and convert at the same time.
		CircleGenericQueue<BinaryCharTree> tempQueue = new CircleGenericQueue<BinaryCharTree>();
		tempQueue.enqueue(this);
		CircleGenericQueue<Integer> tempIntQueue = new CircleGenericQueue<Integer>();
		Integer tempIndexInteger = Integer.valueOf(0);
		tempIntQueue.enqueue(tempIndexInteger);
		
		BinaryCharTree tempTree = tempQueue.dequeue();
		int tempIndex = (tempIntQueue.dequeue()).intValue();
		
		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 = tempQueue.dequeue();
			if (tempTree == null) {
				break;
			}//of if
			tempIndex = (tempIntQueue.dequeue()).intValue();
		}//of while
		
	}//of toDataArrayGenericQueue
	/**
	 * *********************************************************
	 * 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));

	}//of main
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值