【二叉树】java源码实现
BiTree 是 Binary Tree 的缩写,中文意思是 二叉树
Node.java
public class Node<T> {public T data;public Node<T> left;public Node<T> right;public Node(T data, Node<T> left, Node<T> right) {this.data = data;this.left = left;this.right = right;}public Node(T data) {this.data = data;}public Node(Node<T> left, Node<T> right) {this.left = left;this.right = right;}public Node() {}
}
BiTree.java
public class BiTree<T> {public Node<T> root;public BiTree() {}public BiTree(Node<T> root) {this.root = root;}public BiTree(T data, Node<T> lp , Node<T> rp) {this.root = new Node<T>(data,lp,rp);}public boolean isEmpty() {return root == null;}public Node<T> getRoot() {return root;}public Node<T> getLeft(Node<T> p) {return p.left;}public Node<T> getRight(Node<T> p) {return p.right;}public void insertL(T val,Node<T> p) {Node<T> tmp = new Node<T>(val);tmp.left = p.left;p.left = tmp;}public void insertR(T val,Node<T> p) {Node<T> tmp = new Node<T>(val);tmp.right = p.right;p.right = tmp;}public Node<T> deleteL(Node<T> p) {if ((p==null) || (p.left == null)) {return null;}Node<T> tmp = p.left;tmp.left = null;return tmp;}public Node<T> deleteR(Node<T> p) {if ((p==null) || (p.right == null)) {return null;}Node<T> tmp = p.right;tmp.right = null;return tmp;}public boolean isLeaf(Node<T> p) {if ((p != null) && (p.left == null) && (p.right == null)){return true;}else{return false;}}
// 前序public void preOrder(Node<T> root) {if (root == null) {return;}System.out.println(root.data);preOrder(root.left);preOrder(root.right);}
// 中序遍历public void inOrder(Node<T> root) {if (root == null) {return;}inOrder(root.left);System.out.println(root.data);inOrder(root.right);}
// 后序遍历public void postOrder(Node<T> root) {if (root == null) {return;}postOrder(root.left);postOrder(root.right);System.out.println(root.data);}
// 深度public int getDepth(Node<T> root) {if (root == null) {return 0;}int leftDepth = getDepth(root.left);int rightDepth = getDepth(root.right);return leftDepth > rightDepth ? leftDepth + 1 : rightDepth + 1;}public void getParent(Node<T> cur, Node<T> target) {if (cur == null) {return;}if (cur.left == target || cur.right == target) {System.out.println(cur.data);return;}getParent(cur.left, target);getParent(cur.right, target);}public int countNode(Node<T> p){if (p == null) {return 0;}return 1+countNode(p.left)+countNode(p.right);}public int countleaves(Node<T> p){if (p == null) {return 0;}if (isLeaf(p)) {return 1;}return countleaves(p.left)+countleaves(p.right);}
}
练习:
BiTreeMain.java
public class BiTreeMain {public static void main(String[] args) {// 创建根节点(根节点为 'A')Node<Character> root = new Node<Character>('A');BiTree<Character> tree = new BiTree<Character>(root);// 插入左孩子 'B' 和右孩子 'C'tree.insertL('B', root);tree.insertR('C', root);// 获取左右孩子节点Node<Character> leftChild = tree.getLeft(root); // 'B'Node<Character> rightChild = tree.getRight(root); // 'C'// 继续插入节点tree.insertL('D', leftChild); // 'B' 的左孩子是 'D'tree.insertR('E', leftChild); // 'B' 的右孩子是 'E'tree.insertL('F', rightChild); // 'C' 的左孩子是 'F'tree.insertR('G', rightChild); // 'C' 的右孩子是 'G'System.out.println("前序遍历:");tree.preOrder(root); // A, B, D, E, C, F, GSystem.out.println("\n中序遍历:");tree.inOrder(root); // D, B, E, A, F, C, GSystem.out.println("\n后序遍历:");tree.postOrder(root); // D, E, B, F, G, C, ASystem.out.println("\n树的深度:");System.out.println(tree.getDepth(root)); // 3// 输出所有节点数System.out.println("\n树的节点个数:");System.out.println(tree.countNode(root));
// 输出所有叶子节点System.out.println("\n树的树叶节点个数:");System.out.println(tree.countleaves(root));}
}