java学习第23天,使用具有通用性的队列

队列有两种: 存储二叉树节点的队列; 存储整数的队列. 这样的话, 难道我们要为每种类型单独写一个队列? 这样显然没有充分利用代码的复用性. 实际上, 我们只需要一个存储对象的队列就够啦!
1,Java 里面, 所有的类均为 Object 类的 (直接或间接) 子类. 如果不写就默认为直接子类. 例如
public class CircleObjectQueue;
等价于
public class CircleObjectQueue extends Object;
2,存储对象的队列, 实际上是存储对象的地址 (引用、指针). 因此, 可以存储任何类的对象 (的引用).
3,可以通过强制类型转换将对象转成其本身的类别. 例如前面程序
tempTree = (BinaryCharTree) tempQueue.dequeue();
括号中的类型即表示强制类型转换.
4,Java 本身将 int, double, char 分别封装到 Integer, Double, Char 类.

package java21to30;

import java.util.Arrays;

public class D23_BinaryCharTree {
	char value;
	D23_BinaryCharTree leftChild;
	D23_BinaryCharTree rightChild;

	public D23_BinaryCharTree(char paraName) {
		value = paraName;
		leftChild = null;
		rightChild = null;
	}

	public static D23_BinaryCharTree manualConstructTree() {
		D23_BinaryCharTree resultTree = new D23_BinaryCharTree('a');
		D23_BinaryCharTree tempTreeB = new D23_BinaryCharTree('b');
		D23_BinaryCharTree tempTreeC = new D23_BinaryCharTree('c');
		D23_BinaryCharTree tempTreeD = new D23_BinaryCharTree('d');
		D23_BinaryCharTree tempTreeE = new D23_BinaryCharTree('e');
		D23_BinaryCharTree tempTreeF = new D23_BinaryCharTree('f');
		D23_BinaryCharTree tempTreeG = new D23_BinaryCharTree('g');

		resultTree.leftChild = tempTreeB;
		resultTree.rightChild = tempTreeC;
		tempTreeB.rightChild = tempTreeD;
		tempTreeC.leftChild = tempTreeE;
		tempTreeD.leftChild = tempTreeF;
		tempTreeD.rightChild = tempTreeG;

		return resultTree;
	}

	public void preOrderVisit() {
		System.out.print("" + value + " ");

		if (leftChild != null) {
			leftChild.preOrderVisit();
		}
		if (rightChild != null) {
			rightChild.preOrderVisit();
		}
	}

	public void inOrderVisit() {
		if (leftChild != null) {
			leftChild.inOrderVisit();
		}
		System.out.print("" + value + " ");

		if (rightChild != null) {
			rightChild.inOrderVisit();
		}
	}

	public void postOrderVisit() {
		if (leftChild != null) {
			leftChild.postOrderVisit();
		}
		if (rightChild != null) {
			rightChild.postOrderVisit();
		}
		System.out.print("" + value + " ");
	}

	public int getDepth() {
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		}
		int tempLeftDepth = 0;
		if (leftChild != null) {
			tempLeftDepth = leftChild.getDepth();
		}
		int tempRightDepth = 0;
		if (rightChild != null) {
			tempRightDepth = rightChild.getDepth();
		}
		if (tempLeftDepth >= tempRightDepth) {
			return tempLeftDepth + 1;
		} else {
			return tempRightDepth + 1;
		}
	}

	public int getNumNodes() {
		if ((leftChild == null) && (rightChild == null)) {
			return 1;
		}
		int tempLeftNodes = 0;
		if (leftChild != null) {
			tempLeftNodes = leftChild.getNumNodes();
		}
		int tempRightNodes = 0;
		if (rightChild != null) {
			tempRightNodes = rightChild.getNumNodes();
		}
		return tempLeftNodes + tempRightNodes + 1;
	}

	char[] valuesArray;
	int[] indicesArray;

	public void toDataArrays() {
		int tempLength = getNumNodes();

		valuesArray = new char[tempLength];
		indicesArray = new int[tempLength];
		int i = 0;
		D22_CircleObjectQueue tempQueue = new D22_CircleObjectQueue();
		tempQueue.enqueue(this);
		D22_CircleObjectQueue tempIntQueue = new D22_CircleObjectQueue();
		tempIntQueue.enqueue(0);

		D23_BinaryCharTree tempTree = (D23_BinaryCharTree) tempQueue.dequeue();
		int tempIndex = (int) 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);
			}
			if (tempTree.rightChild != null) {
				tempQueue.enqueue(tempTree.rightChild);
				tempIntQueue.enqueue(tempIndex * 2 + 2);
			}
			tempTree = (D23_BinaryCharTree) tempQueue.dequeue();
			if (tempTree != null)
				tempIndex = (int) tempIntQueue.dequeue();
		}
	}

	public void toDataArraysObjectQueue() {
		int tempLength = getNumNodes();

		valuesArray = new char[tempLength];
		indicesArray = new int[tempLength];
		int i = 0;
		D22_CircleObjectQueue tempQueue = new D22_CircleObjectQueue();
		tempQueue.enqueue(this);
		D22_CircleObjectQueue tempIntQueue = new D22_CircleObjectQueue();
		Integer tempIndexInteger = new Integer(0);
		tempIntQueue.enqueue(tempIndexInteger);

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

			if (tempTree.leftChild != null) {
				tempQueue.enqueue(tempTree.leftChild);
				tempIntQueue.enqueue(new Integer(tempIndex * 2 + 1));
			}
			if (tempTree.rightChild != null) {
				tempQueue.enqueue(tempTree.rightChild);
				tempIntQueue.enqueue(new Integer(tempIndex * 2 + 2));
			}
			tempTree = (D23_BinaryCharTree) tempQueue.dequeue();
			if (tempTree == null) {
				break;
			}
			tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
		}
	}

	public static void main(String args[]) {
		D23_BinaryCharTree tempTree = manualConstructTree();
		System.out.println("\r\n先序遍历:");
		tempTree.preOrderVisit();
		System.out.println("\r\n中序遍历:");
		tempTree.inOrderVisit();
		System.out.println("\r\n后序遍历:");
		tempTree.postOrderVisit();

		System.out.println("\r\n\r\n深度: " + tempTree.getDepth());
		System.out.println("节点个数: " + tempTree.getNumNodes());
		tempTree.toDataArrays();
		System.out.println("值是: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("指数是:" + Arrays.toString(tempTree.indicesArray));
		tempTree.toDataArraysObjectQueue();
		System.out.println("对象序列.");
		System.out.println("值: " + Arrays.toString(tempTree.valuesArray));
		System.out.println("指数是: " + Arrays.toString(tempTree.indicesArray));
	}
}

输出结果:

先序遍历:
a b d f g c e 
中序遍历:
b f d g a e c 
后序遍历:
f g d b e c a 

深度: 4
节点个数: 7
值是: [a, b, c, d, e, f, g]
指数是:[0, 1, 2, 4, 5, 9, 10]
tempIndex = 0
对象序列.: [a, b, c, d, e, f, g]
指数是: [0, 1, 2, 4, 5, 9, 10]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值