输入一颗二元查找树,将该树转换为它的镜像, 即在转换后的二元查找树中,左子树的结点都大于右子树的结点。 用递归和循环两种方法完成树的镜像转换:

题目:输入一颗二元查找树,将该树转换为它的镜像,
即在转换后的二元查找树中,左子树的结点都大于右子树的结点。
用递归和循环两种方法完成树的镜像转换。   
例如输入:
  8
  / /
  6 10
 // //
5 7 9 11

输出:
  8
  / /
 10 6
 // //

11 9 7 5


package cn.itcast.demo.MirrorTree;

import java.util.LinkedList;
import java.util.Scanner;

public class MirrorTree {

	private class Node {
		public int data;
		public Node left;
		public Node right;

		public Node() {
			super();
		}

		public Node(int data, Node left, Node right) {
			super();
			this.data = data;
			this.left = left;
			this.right = right;
		}
	}

	public static void main(String[] args) {
		System.out.println("请随意输入一串数字:");
		Scanner sc = new Scanner(System.in);
		String[] num = null;
		num = sc.nextLine().split(" ");
		int[] a = new int[num.length];
		for (int i = 0; i < a.length; i++) {
			a[i] = Integer.valueOf(num[i]);
		}
		MirrorTree mt = new MirrorTree();
		Node root = null;
		root = mt.buildTree(root, a);
		System.out.println("这是原二叉排序的中序遍历结果:");
		mt.inOrder(root);
		
		System.out.print("\n根节点是:"+root.data);
		// 交换二叉树的左右子树
		mt.exchangeRecursive(root);
		System.out.println("\n这是用递归将该树转换为它的镜像的中序遍历结果:");
		mt.inOrder(root);
		// 将树转换回原二叉排序树
		mt.exchangeCirculatory(root);// 循环交换二叉排序树的左右子树
		System.out.println("\n这是借助先序遍历循环将该树转换为它的镜像的中序遍历结果:");
		mt.inOrder(root);

		mt.exchangeRecursive(root); // 8)将树转换回原二叉排序树
		mt.levelOrder(root); // 9)层序交换二叉排序树的左右子树
		System.out.println("\n这是借助层序循环将该树转换为它的镜像的中序遍历结果:");
		mt.inOrder(root); // 10)打印循环交换之后的二叉排序树的中序遍历结果
	}

	private void levelOrder(Node root) {
		// TODO Auto-generated method stub
		if (root == null) {
			System.out.println("\n二叉树为空!");
			return;
		}
		LinkedList<Node> queue = new LinkedList<Node>();
		queue.add(root); // 根节点先入队
		while (queue.size() != 0) {
			Node node = queue.removeFirst(); // 得到并删除队列的第一个节点
			swap(node);
			if (node.left != null) { // 该节点的左孩子入队
				queue.add(node.left);
			}
			if (node.right != null) { // 该节点的右孩子入队
				queue.add(node.right);
			}
		}
		
	}

	// 运用先序遍历循环将该树转换为它的镜像,用LinkedList模拟栈来实现 DLR
	private void exchangeCirculatory(Node root) {
		// TODO Auto-generated method stub
		if (root == null) {
			return;
		}
		LinkedList<Node> nodelist = new LinkedList<Node>();
		nodelist.add(root);
		while (nodelist.size() != 0) {
			root = nodelist.removeLast(); // 获得栈顶元素
			swap(root);
			if (root.right != null) { // 如果栈顶元素的右孩子不为空,右孩子节点进栈
				nodelist.add(root.right);
			}
			if (root.left != null) {
				nodelist.add(root.left);
			}
		}
	}

	private void exchangeRecursive(Node root) {
		// TODO Auto-generated method stub
		if (root != null) {
			swap(root);
			exchangeRecursive(root.left);
			exchangeRecursive(root.right);

		}
	}

	private void swap(Node root) {
		// TODO Auto-generated method stub
		Node temp = root.left;
		root.left = root.right;
		root.right = temp;
	}

	private void inOrder(Node root) {
		// TODO Auto-generated method stub
		if (root == null) {
			return;
		} else {
			inOrder(root.left);
			System.out.print(root.data + " ");
			inOrder(root.right);
		}
	}

	private Node buildTree(Node root, int[] a) {
		// TODO Auto-generated method stub
		for (int i = 0; i < a.length; i++) {
			root = insert(root, a[i]);
		}
		return root;
	}

	private Node insert(Node node, int data) {
		//
		if (node == null) {
			node = new Node(data, null, null);
		} else {
			if (data <= node.data) {
				node.left = insert(node.left, data);
			} else {
				node.right = insert(node.right, data);
			}
		}
		return node;
	}

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DisFney

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值