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