直接上代码了,代码说得很清楚了
package BTree; public class BTree { private Node root; private class Node { private Node lchild; private Node rchild; private int data; public Node(int data) { this.lchild = null; this.rchild = null; this.data = data; } } public BTree() { root = null; } public Node getNode() { return root; } public void createTree(Node node, int data) { if (root == null) { root = new Node(data); } else { if (data < node.data) { if (node.lchild == null) { node.lchild = new Node(data); } else { createTree(node.lchild, data); } } else { if (node.rchild == null) { node.rchild = new Node(data); } else { createTree(node.rchild, data); } } } } /*前序遍历*/ public void PreOrder(Node node) { if (node != null) { System.out.print(node.data + " "); PreOrder(node.lchild); PreOrder(node.rchild); } } /*中序遍历*/ public void InOrder(Node node) { if (node != null) { InOrder(node.lchild); System.out.print(node.data + " "); InOrder(node.rchild); } } /*后序遍历*/ public void TailOrder(Node node) { if (node != null) { TailOrder(node.lchild); TailOrder(node.rchild); System.out.print(node.data + " "); } } /*二叉树高度*/ public int Depth(Node node){ int dl = 0,dr =0; if(node!=null){ dl = Depth(node.lchild); dr = Depth(node.rchild); if(dl>dr) return dl+1; return dr+1; } return 0; } /*节点的个数*/ public int Point(Node node){ if(node!=null){ return Point(node.lchild)+Point(node.rchild)+1; } return 0; } /*叶子结点的个数*/ public int Leaf(Node node){ if(node==null) return 0; boolean flag = (node.lchild==null&&node.rchild==null); if(flag) return Leaf(node.lchild)+Leaf(node.rchild)+1; return Leaf(node.lchild)+Leaf(node.rchild); } /*出度为一的节点个数*/ public int oneDegree(Node node){ if(node==null) return 0; boolean flag = (node.lchild==null&&node.rchild!=null)||(node.lchild!=null&&node.rchild==null); if(flag) return oneDegree(node.lchild)+oneDegree(node.rchild)+1; return oneDegree(node.lchild)+oneDegree(node.rchild); } /*出度为二的节点个数*/ public int twoDegree(Node node){ if(node==null) return 0; boolean flag = (node.lchild!=null&&node.rchild!=null); if(flag) return twoDegree(node.lchild)+twoDegree(node.rchild)+1; return twoDegree(node.lchild)+twoDegree(node.rchild); } }
package BTree; public class BTreeDemo { /** * @param args */ public static void main(String[] args) { int [] a ={4,3,2,6,11,8,9,10,1,5}; //int [] a = {1,2,3,4,5}; BTree bt = new BTree(); for(int i=0;i<a.length;i++){ bt.createTree(bt.getNode(), a[i]); } System.out.print("前序遍历:"); bt.PreOrder(bt.getNode()); System.out.println(); System.out.print("中序遍历:"); bt.InOrder(bt.getNode()); System.out.println(); System.out.print("后序遍历:"); bt.TailOrder(bt.getNode()); System.out.println(); System.out.println("二叉树的高度:"+bt.Depth(bt.getNode())); System.out.println("节点的个数:"+bt.Point(bt.getNode())); System.out.println("叶子节点个数:"+bt.Leaf(bt.getNode())); System.out.println("出度为一的节点个数:"+bt.oneDegree(bt.getNode())); System.out.println("出度为二的节点个数:"+bt.twoDegree(bt.getNode())); } }
前序遍历:4 3 2 1 6 5 11 8 9 10 中序遍历:1 2 3 4 5 6 8 9 10 11 后序遍历:1 2 3 5 10 9 8 11 6 4 二叉树的高度:6 节点的个数:10 叶子节点个数:3 出度为一的节点个数:5 出度为二的节点个数:2
时间: 2024-10-22 12:56:12