在Java中镜像二叉树

时间:2018-01-04 15:40:40

标签: java data-structures tree binary-tree mirroring

我目前正在高中学习java中的数据结构课程,我需要帮助镜像树。下面我有我所有有用的代码和两种方法来解决mirrorImage方法,它们总是因某些未知原因返回null。我的主要问题是,为什么这些方法不起作用?我该怎么办呢?我认为它可能与我自己使用的树有关,因为插入方法无法正常工作。

先谢谢,这是我第一次在Stack Overflow上发帖,我期待着它!

注意:这种方法不是我刚才做的功课

public class TreeLab {

  public static void main(String[] args)
  {
    String s = "XCOMPUTERSCIENCE";

    TreeNode root = new TreeNode("" + s.charAt(1), null, null);

    for(int pos = 2; pos < s.length(); pos++)
        insert(root, "" + s.charAt(pos), pos, (int)(1 + Math.log(pos) / 
        Math.log(2)));

    insert(root, "B", 17, 5); 
    insert(root, "A", 18, 5); 
    insert(root, "C", 37, 6); //B's right child

    // display the tree sideways
    display(root, 0);

    System.out.println("\n\nMirror Image: ");
    display(root.mirrorImage(), 0);
  }


  public static void insert(TreeNode t, String s, int pos, int level) {

    TreeNode p = t;
    for(int k = level - 2; k > 0; k--)
        if((pos & (1 << k)) == 0) 
            p = p.getLeft();
        else                        
            p = p.getRight();

    if((pos & 1) == 0)
        p.setLeft(new TreeNode(s, null, null));
    else
        p.setRight(new TreeNode(s, null, null));
  } // end of insert


  //postcondition: display the tree sideways
  public static void display(TreeNode t, int level) {
    if(t == null)
        return;
    display(t.getRight(), level + 1); //recurse right

    for(int k = 0; k < level; k++)
        System.out.print("\t");
    System.out.println(t.getValue());

    display(t.getLeft(), level + 1); //recurse left
  }  // end of display

  public static TreeNode mirrorImage (TreeNode t) {

    //way 1
    /*
    if (t == null)
        return null;

    TreeNode right = t.getRight();
    TreeNode left = t.getLeft();

    t.setLeft(mirrorImage(right));
    t.setRight(mirrorImage(left));

    return t;
    */      

    //way 2
    /*
    if (t == null)
        return null;
    else
        return new TreeNode (t.getValue(), mirrorImage(t.getRight()), 
      mirrorImage(t.getLeft()));
    */      

    return null; //only here to help complile
  }
}//end of TreeLab

//TreeNode class

class TreeNode {

  private Object value; 
  private TreeNode left, right;

  public TreeNode(Object initValue) { 
    value = initValue; 
    left = null; 
    right = null; 
  }

  public TreeNode mirrorImage() {
    return null;
  }

  public TreeNode(Object initValue, TreeNode initLeft, TreeNode initRight) { 
    value = initValue; 
    left = initLeft; 
    right = initRight; 
  }

  public Object getValue() { 
    return value; 
  }

  public TreeNode getLeft()  { 
    return left; 
  }

  public TreeNode getRight()  { 
    return right; 
  }

  public void setValue(Object theNewValue)  { 
    value = theNewValue; 
  }

  public void setLeft(TreeNode theNewLeft)  { 
    left = theNewLeft;
  }

  public void setRight(TreeNode theNewRight) { 
    right = theNewRight;
  }
}

2 个答案:

答案 0 :(得分:1)

mirrorImage方法中,您应该创建一个新的节点副本作为参数(现在只需重新分配其左右属性)并递归地复制其子节点:

public static TreeNode mirrorImage(TreeNode t) {
    if (t == null) {
        return null;
    }
    return new TreeNode(
        t.getValue(),
        mirrorImage(t.getLeft()),
        mirrorImage(t.getRight())
    );
}

答案 1 :(得分:0)

我相信我发现了自己的错误。我调用root.mirrorImage()而不是mirrorImage(root),导致它传递给我偶然生成的TreeNode()类中的mirrorImage()方法,并始终返回null。更改后我知道我看到了解决方法正常工作的两种方法。谢谢大家!