// 求二叉树的结点总数(递归) public static int nodes(BiTree tree) { if (tree == null) return 0; else { int left = nodes(tree.leftTree); int right = nodes(tree.rightTree); return left + right + 1; } }
// 求二叉树叶子节点的总数 public static int leaf(BiTree tree) { if (tree == null) return 0; else { int left = leaf(tree.leftTree); int right = leaf(tree.rightTree); if (tree.leftTree == null && tree.rightTree == null) return (left + right)+1;//增加一个叶子节点 else return left + right; } } //求二叉树父节点个数 public static int fatherNodes(BiTree tree) { //节点为空或者单节点 if (tree == null || (tree.leftTree == null && tree.rightTree == null)) return 0; else { int left = fatherNodes(tree.leftTree); int right = fatherNodes(tree.rightTree); return left + right + 1; } }
// 求只有一个孩子结点的父节点个数 public static int oneChildFather(BiTree tree) { int left,right; if (tree == null || (tree.rightTree == null && tree.leftTree == null)) return 0; else { left = oneChildFather(tree.leftTree); right = oneChildFather(tree.rightTree); if ((tree.leftTree != null && tree.rightTree == null)|| (tree.leftTree == null && tree.rightTree != null)) return left + right + 1; else return left + right;/* 加1是因为要算上根节点 */ } }
// 求二叉树只拥有左孩子的父节点总数 public static int leftChildFather(BiTree tree) { if (tree == null || tree.leftTree==null) return 0; else { int left = leftChildFather(tree.leftTree); int right = leftChildFather(tree.rightTree); if ((tree.leftTree != null && tree.rightTree == null)) return left + right + 1; else return left + right; } }
// 求二叉树只拥有右孩子的结点总数 public static int rightChildFather(BiTree tree) { if (tree == null || tree.rightTree == null) return 0; else { int left = rightChildFather(tree.leftTree); int right = rightChildFather(tree.rightTree); if (tree.leftTree == null && tree.rightTree != null) return left + right + 1; else return left + right; } }
// 计算有两个节点的父节点的个数 public static int doubleChildFather(BiTree tree) { int left,right; if (tree == null) return 0; else { left = doubleChildFather(tree.leftTree); right = doubleChildFather(tree.rightTree); if (tree.leftTree != null && tree.rightTree != null) return (left + right + 1); else return (left + right); } }
// 将树中的每个节点的孩子对换位置 public void exChange() { if (this == null) return; if (leftTree != null) { leftTree.exChange();//交换左树 } if (rightTree != null) { rightTree.exChange();//交换右树 } BiTree temp = leftTree; leftTree = rightTree; rightTree = temp; }
//递归求所有结点的和 public static int getSumByRecursion(BiTree tree){ if(tree==null){ return 0; } else{ int left=getSumByRecursion(tree.leftTree); int right=getSumByRecursion(tree.rightTree); return Integer.parseInt(tree.data.toString())+left+right; } }
//非递归求二叉树中所有结点的和 public static int getSumByNoRecursion(BiTree tree){ Stack<BiTree> stack = new Stack<BiTree>(); int sum=0;//求和 if(tree!=null){ stack.push(tree); while(!stack.isEmpty()){ tree=stack.pop(); sum+=Integer.parseInt(tree.data.toString()); if(tree.leftTree!=null) stack.push(tree.leftTree); if(tree.rightTree!=null) stack.push(tree.rightTree); } } return sum; }
/** * @param args */ public static void main(String[] args) { BiTree l = new BiTree(10); BiTree m = new BiTree(9); BiTree n = new BiTree(8); BiTree h = new BiTree(7); BiTree g = new BiTree(6); BiTree e = new BiTree(5,n,l); BiTree d = new BiTree(4,m,null); BiTree c = new BiTree(3,g,h); BiTree b = new BiTree(2, d, e); BiTree tree = new BiTree(1, b, c);