import java.util.LinkedList;
import java.util.Queue;
public class BinaryTree<T> {
private TreeNode<T> root;
// 构建二叉树
public void buildTree(T[] values) {
if (values == null || values.length == 0) {
return;
}
root = buildTree(values, 0);
}
private TreeNode<T> buildTree(T[] values, int index) {
if (index < values.length) {
TreeNode<T> node = new TreeNode<>(values[index]);
node.left = buildTree(values, 2 * index + 1);
node.right = buildTree(values, 2 * index + 2);
return node;
}
return null;
}
// 前序遍历
public void preOrderTraversal() {
preOrderTraversal(root);
}
private void preOrderTraversal(TreeNode<T> node) {
if (node != null) {
System.out.print(node.value + " ");
preOrderTraversal(node.left);
preOrderTraversal(node.right);
}
}
// 中序遍历
public void inOrderTraversal() {
inOrderTraversal(root);
}
private void inOrderTraversal(TreeNode<T> node) {
if (node != null) {
inOrderTraversal(node.left);
System.out.print(node.value + " ");
inOrderTraversal(node.right);
}
}
// 后序遍历
public void postOrderTraversal() {
postOrderTraversal(root);
}
private void postOrderTraversal(TreeNode<T> node) {
if (node != null) {
postOrderTraversal(node.left);
postOrderTraversal(node.right);
System.out.print(node.value + " ");
}
}
// 层序遍历
public void levelOrderTraversal() {
if (root == null) {
return;
}
Queue<TreeNode<T>> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty()) {
TreeNode<T> node = queue.poll();
System.out.print(node.value + " ");
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
}
// 获取总结点个数
public int getNodeCount() {
return getNodeCount(root);
}
private int getNodeCount(TreeNode<T> node) {
if (node == null) {
return 0;
}
return 1 + getNodeCount(node.left) + getNodeCount(node.right);
}
// 获取叶子结点个数
public int getLeafNodeCount() {
return getLeafNodeCount(root);
}
private int getLeafNodeCount(TreeNode<T> node) {
if (node == null) {
return 0;
}
if (node.left == null && node.right == null) {
return 1;
}
return getLeafNodeCount(node.left) + getLeafNodeCount(node.right);
}
// 获取第n层结点个数
public int getNodeCountAtLevel(int level) {
return getNodeCountAtLevel(root, level);
}
private int getNodeCountAtLevel(TreeNode<T> node, int level) {
if (node == null || level < 1) {
return 0;
}
if (level == 1) {
return 1;
}
return getNodeCountAtLevel(node.left, level - 1) + getNodeCountAtLevel(node.right, level - 1);
}
// 获取二叉树的深度
public int getDepth() {
return getDepth(root);
}
private int getDepth(TreeNode<T> node) {
if (node == null) {
return 0;
}
int leftDepth = getDepth(node.left);
int rightDepth = getDepth(node.right);
return 1 + Math.max(leftDepth, rightDepth);
}
// 插入指定元素结点
public void insertNode(T value) {
if (root == null) {
root = new TreeNode<>(value);
} else {
insertNode(root, value);
}
}
private void insertNode(TreeNode<T> node, T value) {
if (node.left == null) {
node.left = new TreeNode<>(value);
} else if (node.right == null) {
node.right = new TreeNode<>(value);
} else {
insertNode(node.left, value);
}
}
// 删除指定元素结点
public void deleteNode(T value) {
if (root == null) {
return;
}
if (root.value.equals(value)) {
root = null;
} else {
deleteNode(root, value);
}
}
private void deleteNode(TreeNode<T> node, T value) {
if (node == null) {
return;
}
if (node.left != null && node.left.value.equals(value)) {
node.left = null;
} else if (node.right != null && node.right.value.equals(value)) {
node.right = null;
} else {
deleteNode(node.left, value);
deleteNode(node.right, value);
}
}
// 查找指定元素结点
public boolean searchNode(T value) {
return searchNode(root, value);
}
private boolean searchNode(TreeNode<T> node, T value) {
if (node == null) {
return false;
}
if (node.value.equals(value)) {
return true;
}
return searchNode(node.left, value) || searchNode(node.right, value);
}
private static class TreeNode<T> {
T value;
TreeNode<T> left;
TreeNode<T> right;
TreeNode(T value) {
this.value = value;
this.left = null;
this.right = null;
}
}
}