java学习第24天,二叉树的建立

代码的有点难度整个代码需要花费时间来整理思路,一定要搞懂搞透才能开始下一步

package java21to30;

import java.util.Arrays;

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

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

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

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

		return resultTree;
	}

	public D24_BinaryCharTree(char[] paraDataArray, int[] paraIndicesArray) {
		int tempNumNodes = paraDataArray.length;
		D24_BinaryCharTree[] tempAllNodes = new D24_BinaryCharTree[tempNumNodes];
		for (int i = 0; i < tempNumNodes; i++) {
			tempAllNodes[i] = new D24_BinaryCharTree(paraDataArray[i]);
		}
		for (int i = 1; i < tempNumNodes; i++) {
			for (int j = 0; j < i; j++) {
				System.out.println("indices " + paraIndicesArray[j] + " vs. " + paraIndicesArray[i]);
				if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 1) {
					tempAllNodes[j].leftChild = tempAllNodes[i];
					System.out.println("Linking " + j + " with " + i);
					break;
				} else if (paraIndicesArray[i] == paraIndicesArray[j] * 2 + 2) {
					tempAllNodes[j].rightChild = tempAllNodes[i];
					System.out.println("Linking " + j + " with " + i);
					break;
				}
			}
		}
		value = tempAllNodes[0].value;
		leftChild = tempAllNodes[0].leftChild;
		rightChild = tempAllNodes[0].rightChild;
	}

	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);

		D24_BinaryCharTree tempTree = (D24_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 = (D24_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);

		D24_BinaryCharTree tempTree = (D24_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 = (D24_BinaryCharTree) tempQueue.dequeue();
			if (tempTree == null) {
				break;
			}
			tempIndex = ((Integer) tempIntQueue.dequeue()).intValue();
		}
	}

	public static void main(String args[]) {
		D24_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));

		char[] tempCharArray = { 'A', 'B', 'C', 'D', 'E', 'F' };
		int[] tempIndicesArray = { 0, 1, 2, 4, 5, 12 };
		D24_BinaryCharTree tempTree2 = new D24_BinaryCharTree(tempCharArray, tempIndicesArray);

		System.out.println("\r\n先序遍历:");
		tempTree2.preOrderVisit();
		System.out.println("\r\n中序遍历:");
		tempTree2.inOrderVisit();
		System.out.println("\r\n后序遍历:");
		tempTree2.postOrderVisit();
	}
}

输出结果:


先序遍历:
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]
indices 0 vs. 1
Linking 0 with 1
indices 0 vs. 2
Linking 0 with 2
indices 0 vs. 4
indices 1 vs. 4
Linking 1 with 3
indices 0 vs. 5
indices 1 vs. 5
indices 2 vs. 5
Linking 2 with 4
indices 0 vs. 12
indices 1 vs. 12
indices 2 vs. 12
indices 4 vs. 12
indices 5 vs. 12
Linking 4 with 5

先序遍历:
A B D C E F 
中序遍历:
B D A E F C 
后序遍历:
D B F E C A 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值