题目:输入一颗二元查找树,将该树转换为它的镜像,即在转换后的二元查找树中,左子树的结点都大于右子树的结点。用递归和循环两种方法完成树的镜像转换。
例如输入:
8
/ \
6 10
/\ /\
5 7 9 11
输出:
8
/ \
10 6
/\ /\
11 9 7 5
参考网址:http://zhedahht.blog.163.com/blog/static/2541117420072159363370/
import java.util.LinkedList;
public class Test_11 {
public static void main(String[] args) {
BSTree bst = new BSTree();
int[] arr = {8,6,10,5,9,7,11};
for(int i =0;i<arr.length;i++){
bst.insert(arr[i]);
}
bst.Midorder(bst.getRoot());
Mirror_loop(bst.getRoot());
bst.Midorder(bst.getRoot());
}
//递归实现镜像
public static void Mirror(TreeNode1 root){
if(root == null){
return;
}
TreeNode1 temp = root.getLeftchild();
root.setLeftchild(root.getRightchild());
root.setRightchild(temp);
if(root.getLeftchild()!=null){
Mirror(root.getLeftchild());
}
if(root.getRightchild() !=null){
Mirror(root.getRightchild());
}
}
//循环实现镜像,用队列
public static void Mirror_loop(TreeNode1 root){
if(root == null){
return;
}
LinkedList<TreeNode1> queue = new LinkedList<TreeNode1>();//用LinkedList模拟队列
queue.add(root);
while(queue.size() !=0){
TreeNode1 pnode = queue.removeFirst();//弹出队列第一个元素
//交换他的左右孩子
TreeNode1 temp = pnode.getLeftchild();
pnode.setLeftchild(pnode.getRightchild());
pnode.setRightchild(temp);
//若左孩子不为空,把左孩子放入队列
if(pnode.getLeftchild() != null){
queue.add(pnode.getLeftchild());
}
//若右孩子不为空,把右孩子放入队列
if(pnode.getRightchild() != null){
queue.add(pnode.getRightchild());
}
}
}
}
class TreeNode1{
private int key;
private TreeNode1 leftchild;
private TreeNode1 rightchild;
private TreeNode1 parent;
public int getKey() {
return key;
}
public void setKey(int key) {
this.key = key;
}
public TreeNode1 getLeftchild() {
return leftchild;
}
public void setLeftchild(TreeNode1 leftchild) {
this.leftchild = leftchild;
}
public TreeNode1 getRightchild() {
return rightchild;
}
public void setRightchild(TreeNode1 rightchild) {
this.rightchild = rightchild;
}
public TreeNode1 getParent() {
return parent;
}
public void setParent(TreeNode1 parent) {
this.parent = parent;
}
public TreeNode1(int key,TreeNode1 leftchild,TreeNode1 rightchild,TreeNode1 parent){
this.key = key;
this.leftchild = leftchild;
this.rightchild = rightchild;
this.parent = parent;
}
}
class BSTree{
private TreeNode1 root;
public TreeNode1 getRoot() {
return root;
}
public void setRoot(TreeNode1 root) {
this.root = root;
}
public void insert(int num){
TreeNode1 newnode = new TreeNode1(num,null,null,null);
TreeNode1 parentnode = null;
TreeNode1 pnode = root;
if(root == null){
root = newnode;
return;
}
while(pnode != null){
parentnode = pnode;
if(num<pnode.getKey()){
pnode = pnode.getLeftchild();
}
else if(num>pnode.getKey()){
pnode = pnode.getRightchild();
}
else {
return;//树中已有此节点
}
}
if(num<parentnode.getKey()){
parentnode.setLeftchild(newnode);
newnode.setParent(parentnode);
}
else{
parentnode.setRightchild(newnode);
newnode.setParent(parentnode);
}
}
public void Midorder(TreeNode1 root){
if(root == null){
System.out.println("树为空!");
return;
}
if(root.getLeftchild() != null)
Midorder(root.getLeftchild());
System.out.println(root.getKey());
if(root.getRightchild() != null)
Midorder(root.getRightchild());
}
}