本文实现了如下二叉树的基本操作:
1.手动创建一棵二叉树 返回这棵树的根节点
2.前序遍历
3.中序遍历
4.后序遍历
5.迭代获取树中节点的个数
6.递归获取节点的个数
7.迭代获取叶子节点的个数
8.递归获取叶子节点的个数
9.获取第K层节点的个数
10.获取二叉树的高度
11.检测值为value的元素是否存在
12.层序遍历
13.判断一棵树是不是完全二叉树
代码:
package TreeOperation;
//二叉树的基本操作
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.LinkedList;
public class BinaryTree {
public static int nodeSize;
public static int leafSize = 0;
//节点
static class TreeNode {
public char val;
public TreeNode left;//左孩子的引用
public TreeNode right;//右孩子的引用
public TreeNode(char val) {
this.val = val;
}
}
/**
* 创建一棵二叉树 返回这棵树的根节点
*
* @return
*/
public static TreeNode createTree() {
TreeNode a = new TreeNode('A');
TreeNode b = new TreeNode('B');
TreeNode c = new TreeNode('C');
TreeNode d = new TreeNode('D');
TreeNode e = new TreeNode('E');
TreeNode f = new TreeNode('F');
TreeNode g = new TreeNode('G');
TreeNode h = new TreeNode('H');
// 手动构建其关系
a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.left = f;
c.right = g;
d.left = null;
d.right = null;
e.left = null;
e.right = h;
f.left = null;
f.right = null;
g.left = null;
g.right = null;
h.left = null;
h.right = null;
// 返回根结点即可
return a;
}
// 前序遍历
public static void preOrder(TreeNode root) {
Deque<TreeNode> stack = new ArrayDeque<>();
if (root != null) {
stack.push(root);
}
while (!stack.isEmpty()) {
TreeNode cur = stack.pop();
System.out.print(cur.val + " ");
if (cur.right != null) {
stack.push(cur.right);
}
if (cur.left != null) {
stack.push(cur.left);
}
}
}
// 中序遍历
static void inOrder(TreeNode root) {
Deque<TreeNode> stack = new ArrayDeque<>();
while (root != null || !stack.isEmpty()) {
if (root != null) {
stack.push(root);
root = root.left;
} else {
TreeNode cur = stack.pop();
System.out.print(cur.val + " ");
root = cur.right;
}
}
}
// 后序遍历
static void postOrder(TreeNode root) {
Deque<TreeNode> stack = new ArrayDeque<>();
TreeNode cur = root;
TreeNode tail = null;
while (cur != null || !stack.isEmpty()) {
while (cur != null) {
stack.push(cur);
cur = cur.left;
}
TreeNode top = stack.peek();
if (top.right == null) {
System.out.print(top.val + " ");
tail = top;
stack.pop();
} else if (top.right == tail) {
System.out.print(top.val + " ");
tail = top;
stack.pop();
} else {
cur = top.right;
}
}
}
/**
* 获取树中节点的个数:遍历思路
*/
static void size(TreeNode root) {
Deque<TreeNode> queue = new ArrayDeque<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
int size = queue.size();
nodeSize += size;
while (size-- != 0) {
TreeNode cur = queue.poll();
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
}
}
/**
* 获取节点的个数:子问题的思路
*
* @param root
* @return
*/
static int size2(TreeNode root) {
if (root == null) {
return 0;
}
int leftCount = size2(root.left);
int rightCount = size2(root.right);
return leftCount + rightCount + 1;
}
/*
获取叶子节点的个数:遍历思路
*/
static void getLeafNodeCount1(TreeNode root) {
Deque<TreeNode> queue = new ArrayDeque<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- != 0) {
TreeNode cur = queue.poll();
if (cur.left == null && cur.right == null) {
leafSize++;
}
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
}
}
/*
获取叶子节点的个数:子问题
*/
static int getLeafNodeCount2(TreeNode root) {
if (root == null) {
return 0;
}
if (root.left == null && root.right == null) {
return 1;
}
int left = getLeafNodeCount2(root.left);
int right = getLeafNodeCount2(root.right);
return left + right;
}
/*
获取第K层节点的个数
*/
static int getKLevelNodeCount(TreeNode root, int k) {
Deque<TreeNode> queue = new ArrayDeque<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
int size = queue.size();
k--;
if (k == 0) {
return size;
}
while (size-- != 0) {
TreeNode cur = queue.poll();
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
}
return 0;
}
/*
获取二叉树的高度
时间复杂度:O(N)
*/
static int getHeight(TreeNode root) {
if (root == null) {
return 0;
}
int left = getHeight(root.left);
int right = getHeight(root.right);
return Math.max(left, right) + 1;
}
// 检测值为value的元素是否存在
static TreeNode find(TreeNode root, char val) {
if (root == null) {
return null;
}
if (root.val == val) {
return root;
}
TreeNode left = find(root.left, val);
if (left != null) {
return left;
}
TreeNode right = find(root.right, val);
if (right != null) {
return right;
}
return null;
}
//层序遍历
static void levelOrder(TreeNode root) {
Deque<TreeNode> queue = new ArrayDeque<>();
if (root != null) {
queue.offer(root);
}
while (!queue.isEmpty()) {
int size = queue.size();
while (size-- != 0) {
TreeNode cur = queue.poll();
System.out.print(cur.val + " ");
if (cur.left != null) {
queue.offer(cur.left);
}
if (cur.right != null) {
queue.offer(cur.right);
}
}
}
}
// 判断一棵树是不是完全二叉树
static boolean isCompleteTree(TreeNode root) {
if (root == null) {
return true;
}
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(root);
//层序遍历找到第一个null结点
while (true) {
TreeNode cur = queue.poll();
if (cur == null) {
break;
}
queue.offer(cur.left);
queue.offer(cur.right);
}
while (!queue.isEmpty()) {
TreeNode cur = queue.poll();
if (cur != null) {
return false;
}
}
return true;
}
//验证
public static void main(String[] args) {
TreeNode root = createTree();
System.out.println("-------------------------");
System.out.println("先序遍历:");
preOrder(root);
System.out.println();
System.out.println("-------------------------");
System.out.println("中序遍历:");
inOrder(root);
System.out.println();
System.out.println("-------------------------");
System.out.println("后序遍历:");
postOrder(root);
System.out.println();
System.out.println("-------------------------");
System.out.println("层序遍历:");
levelOrder(root);
System.out.println();
System.out.println("-------------------------");
size(root);
System.out.println("层序遍历求得树的节点数为:" + nodeSize);
System.out.println("-------------------------");
System.out.println("递归求得树的节点数为:" + size2(root));
System.out.println("-------------------------");
getLeafNodeCount1(root);
System.out.println("层序遍历求得树的叶子数为:" + leafSize);
System.out.println("-------------------------");
System.out.println("递归求得树的节点数为:" + getLeafNodeCount2(root));
System.out.println("-------------------------");
System.out.println("第k层的节点数为:" + getKLevelNodeCount(root, 4));
System.out.println("-------------------------");
System.out.println("树的高度为:" + getHeight(root));
System.out.println("-------------------------");
System.out.println("查找val并输出:" + find(root, 'F').val);
System.out.println("-------------------------");
System.out.println("判断是否是完全二叉树:" + isCompleteTree(root));
}
}