All pastes #352061 Raw Edit

Chris's Crap

public java v1 · immutable
·published 2007-02-12 15:45 UTC
rendered paste body
//Christopher Prashadpublic class TreeNodeWork{  public static boolean isLeaf(TreeNode node)  {    return  node!=null && node.getLeft()==null && node.getRight()==null;  }  public static int sumTree(TreeNode root)  {    if(root == null)      return 0;    return (Integer)root.getValue() + sumTree(root.getLeft()) + sumTree(root.getRight());  }  public static int countPaths(TreeNode root)  {    int sum=0;    if(root==null)      return 0;    if(root.getLeft() != null)      sum += 1+countPaths(root.getLeft());    if(root.getRight() != null)      sum += 1+countPaths(root.getRight());    return sum;  }  public static int depth(TreeNode root)  {    if(root == null)      return -1;    else      return 1+Math.max(depth(root.getLeft()), depth(root.getRight()));  }    public static int countNodes(TreeNode root)  {    if(root == null)      return 0;    return 1 + countNodes(root.getLeft()) + countNodes(root.getRight());  }  public static double bushRatio(TreeNode root)  {    if(root == null)      return 0.;    return countNodes(root)/(Math.pow(2., depth(root)) - 1.);  }  public static TreeNode copy(TreeNode root)  {    if(root == null)      return null;    return new TreeNode(root.getValue(), copy(root.getLeft()),                        copy(root.getRight()));  }  public static TreeNode mirrorImage(TreeNode root)  {    if(root == null)      return null;    return new TreeNode(root.getValue(), mirrorImage(root.getRight()),                        mirrorImage(root.getLeft()));  }  public static boolean sameShape(TreeNode root1, TreeNode root2)  {    if(root1 == null && root2 == null)      return true;    if((root1 == null && root2 != null) ||       (root1 != null && root2 == null))      return false;    return sameShape(root1.getLeft(), root2.getLeft()) && sameShape(root1.getRight(), root2.getRight());  }  public static boolean hasSameSubtree(TreeNode root1, TreeNode root2)  {    if(root1 == null)      return false;    if(root2 == null)      return true;    if(sameShape(root1,root2))      return true;    return sameShape(root1.getLeft(),root2) ||  sameShape(root1.getRight(),root2);   }  public static void printTree(TreeNode root)  {    if(root==null)      return;    System.out.print(root.getValue() + "");    printTree(root.getLeft());    printTree(root.getRight());      }  public static int countLeaves(TreeNode root)  {    if(root == null)      return 0;    if(root.getLeft() == null && root.getRight() == null)      return 1;    return countLeaves(root.getLeft()) + countLeaves(root.getRight());  }  public static void main()  {    TreeNode n6=new TreeNode(6),             n5=new TreeNode(5),              n4=new TreeNode(4),             n3=new TreeNode(3,n6,null),             n2=new TreeNode(2,n4,n5),             n1=new TreeNode(1,n2,n3);    System.out.println("Count of Paths(n1) " + countPaths(n1));    System.out.println("Sum of Tree(n1) " + sumTree(n1));    System.out.println("Depth of Tree(n1) " + depth(n1));    System.out.println("Is Leaf(3) " + isLeaf(n3));    System.out.println("Is Leaf(6) " + isLeaf(n6));    System.out.println("Count Nodes(1) " + countNodes(n1));    System.out.println("Count Leaves(1) " + countLeaves(n1));        printTree(n1);  }}