关闭

通过前序遍历和中序遍历重建二叉树以及输出后序遍历(Java实现)(二)

217人阅读 评论(0) 收藏 举报

前面写过一篇用数组表示的二叉树的重建,这篇补上用TreeNode连接起来的树的重建。

/*
 * 使用节点类实现树的重建以及各种遍历
 * 实现给定一个节点将该树镜像化的方法
 */
package sh.tree;

public class RebuildTreeTest {

	public static void main(String[] args) {
		RebuildTreeTest t = new RebuildTreeTest();
		int[] qx = { 1, 2, 4, 7, 3, 5, 6, 8 };
		int[] zx = { 4, 7, 2, 1, 5, 3, 8, 6 };
		TreeNode head = t.build(qx, zx);
		System.out.print("前序遍历为:");
		t.qxbl(head);
		System.out.println();
		System.out.print("中序遍历为:");
		t.zxbl(head);
		System.out.println();
		System.out.print("后序遍历为:");
		t.hxbl(head);
		System.out.println("\n*********镜像化**********");
		t.mirrorize(head);
		System.out.print("前序遍历为:");
		t.qxbl(head);
		System.out.println();
		System.out.print("中序遍历为:");
		t.zxbl(head);
		System.out.println();
		System.out.print("后序遍历为:");
		t.hxbl(head);
		
	}

	// 重建树
	public TreeNode build(int[] qx, int[] zx) {
		TreeNode head = null;
		if (qx.length > 0) {
			head = new TreeNode(qx[0]);
		}
		if (qx.length == 1 || zx.length == 1) {
			return head;
		}

		// 找到在中序遍历中的根节点位置
		int len = qx.length;
		int root = -1;
		for (int i = 0; i < len; i++) {
			if (zx[i] == head.val) {
				root = i;
				break;
			}
		}
		
		// 将前序数组和中序数组分成以根节点为中心的两组数组
		int leftqx[] = new int[root];
		int rightqx[] = new int[len - 1 - root];
		int leftzx[] = new int[root];
		int rightzx[] = new int[len - 1 - root];
		for (int j = 0; j < len; j++) {
			// 左边前序和右边前序赋值
			if (j < root)
				leftqx[j] = qx[j + 1];
			else if (j < len - 1)
				rightqx[j - root] = qx[j + 1];
			// 左边中序和右边中序
			if (head.val != zx[j]) {
				if (j < root)
					leftzx[j] = zx[j];
				else
					rightzx[j - root - 1] = zx[j];
			}
		}
		
		//根据是左边还是右边分别将其设置为左子树和右子树
		if (leftqx.length > 0 || leftzx.length > 0)
			head.left = build(leftqx, leftzx);
		if (rightqx.length > 0 || rightzx.length > 0)
			head.right = build(rightqx, rightzx);
		return head;
	}

	public void mirrorize(TreeNode root){

		if(root == null)
			return;
		
		TreeNode leftChild = root.left;
		TreeNode rightChild = root.right;
		
		if(leftChild != null || rightChild != null){
			root.left = rightChild;
			root.right = leftChild;
			mirrorize(leftChild);
			mirrorize(rightChild);
		}
		
	}
	
	// 树的前序遍历
	public void qxbl(TreeNode head) {
		// 先输出根节点再输出子节点
		if (head != null)
			System.out.print(head.val + " ");
		if (head.left != null) {
			qxbl(head.left);
		}
		if (head.right != null) {
			qxbl(head.right);
		}
	}

	// 中序遍历
	public void zxbl(TreeNode head) {
		if (head == null)
			return;
		if (head.left != null)
			zxbl(head.left);
		System.out.print(head.val + " ");
		if (head.right != null)
			zxbl(head.right);
	}

	// 后序遍历
	public void hxbl(TreeNode head) {
		if(head == null)
			return;
		if(head.left != null)
			hxbl(head.left);
		if(head.right != null)
			hxbl(head.right);
		System.out.print(head.val +" ");
	}
}

树的定义:

package sh.tree;

public class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;
	TreeNode(int x) { val = x; }
}
输出

前序遍历为:1 2 4 7 3 5 6 8 
中序遍历为:4 7 2 1 5 3 8 6 
后序遍历为:7 4 2 5 8 6 3 1 
*********镜像化**********
前序遍历为:1 3 6 8 5 2 4 7 
中序遍历为:6 8 3 5 1 2 7 4 
后序遍历为:8 6 5 3 7 4 2 1 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:9771次
    • 积分:303
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:13篇
    • 译文:0篇
    • 评论:1条
    文章分类
    最新评论