二叉树
一、概念
一棵二叉树是节点的有限集合,该集合可以为空,也可以是由一个根节点和左子树、右子树组成的二叉树。
二叉树的特点:
- 每个节点最多有两棵子树,即二叉树的度最大为2
- 二叉树是有序树,不是指排好序的树,而是二叉树有左右之分
二、二叉树的几种形态
从左到右依次为:空树、只有根节点的二叉树、只有左节点的二叉树、只有右节点的二叉树、带左右节点的二叉树
三、完全二叉树和满二叉树
满二叉树:如果一棵二叉树,它的每一层节点数都达到了该层节点数的最大值,那么这棵树就是一个满二叉树。即一棵满二叉树有n层,那么它的节点数为2^n - 1
个。
完全二叉树:对于深度为k,有n个节点的二叉树,当且仅当其每一个节点都与深度为k的满二叉树中编号从1到n的节点一一对应时称为完全二叉树。满二叉树是一种特殊的完全二叉树。
对于完全二叉树,如果一个节点有右孩子,那么它必须有左孩子,否则该树不能称作完全二叉树,下图就不是完全二叉树
四、二叉树的性质
- 如果规定根节点的层数为1,那么一棵非空二叉树的第i层上最多有
2^(i - 1)
个节点 - 如果固定根节点的深度为1,那么一颗深度为k的二叉树最多有
2^k - 1
个节点 - 有n个节点的完全二叉树的深度为log2(n + 1),上取整
- 任意一颗二叉树,如果叶子节点数为n0,度为2的非叶节点个数为n2,则
n0 = n2 + 1
- 对于有n个节点的完全二叉树,如果从上至下从左至右的顺序对每个节点从0开始编号,则对于序号为i的节点有
- 若i > 0,双亲序号:
(i - 1) / 2
;i = 0,i为根节点,无双亲 - 若
2i + 1 < n
,左孩子序号:2i + 1
,否则无左孩子 - 若
2i+ 1 < n
,右孩子序号:2i + 2
,否则无右孩子
- 若i > 0,双亲序号:
五、二叉树的遍历
二叉树的遍历顺序是固定的,不同的地方在于打印节点值的时机。
1、前序遍历 (根左右)
递归方法:
public void preorder(TreeNode root) {
if(root == null) return;
System.out.print(root.val); //每次遇到root先打印root的值
preorder(root.left); //遍历左子树
preorder(root.right);//遍历右子树
}
非递归方法:用栈实现
public void preorder(TreeNode root) {
if(root == null) return;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root; //cur指向root
while(cur != null || !stack.isEmpty()){ //右子树为空,但stack不为空
while(cur != null) { //cur不为空,则将cur入栈打印,cur = cur.left
stack.push(cur);
System.out.print(cur.val);
cur = cur.left
}
//cur为空时,pop出栈顶元素
TreeNode top = stack.pop();
cur = top.right; //遍历子树,右子树可能为空
}
}
2、中序遍历 (左根右)
递归方法:
public void inorder(TreeNode root) {
if(root == null) return;
inorder(root.left); //每次先遍历左子树,左子树遍历完成后打印root的值
System.out.print(root.val); //打印
inorder(root.right); //遍历右子树
}
非递归方法:同样用栈实现,思路与前序遍历非递归相同
public void inorder(TreeNode root) {
if(root == null) return;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
while(cur != null || !stack.isEmpty()) {
while(cur != null) { //不为空就push
stack.push(cur);
cur = cur.left;
}
//cur为空,pop栈顶元素并打印
TreeNode top = stack.pop();
System.out.print(top.val);
//遍历栈顶元素的右子树
cur = top.right;
}
}
3、后序遍历 (左右根)
递归方法:
public void postorder(TreeNode root) {
if(root == null) return;
postorder(root.left); //遍历左子树
postorder(root.right); //遍历右子树
System.out.print(root.val); //打印根节点值
}
非递归方法:后续遍历的非递归比较复杂,同样用栈实现,但是需要做判断
后序非递归与前中序非递归基本思路一致,cur不为空则入栈,cur一直往左走.当cur == null时,说明左子树已经遍历完,peek栈顶元素cur = stack.peek(),栈顶元素的右子树为空则pop该节点并打印,如果非空则cur = cur.right 以为这样就完了?看下图
//图就只能画这样了,水平有限,能看懂就行
public void postorder(TreeNode root) {
if(root == null) return;
Stack<TreeNode> stack = new Stack<>();
TreeNode cur = root;
TreeNode pre = null;
while(cur != null || !stack.isEmpty()) {
while(cur != null) {
stack.push(cur);
cur = cur.left;
}
cur = stack.peek(); //peek不是
if(cur.right == null || cur.right == prev) {
stack.pop();
System.out.print(cur.val);
prev = cur; //标记上一个打印的节点
cur = null; //打印cur之后要置空
}else {
cur = cur.right;
}
}
}
4、层序遍历
public void levelorder(TreeNode root) {
if(root == null) return;
Queue<Node> queue = new LinkedList<>();
queue.offer(root); //先放根节点
while(!queue.isEmpty()) {
TreeNode cur = queue.poll(); //poll,打印,如果该节点有左右孩子且不为空就入队
System.out.print(cur.val);
if(cur.left != null) {
queue.offer(cur.left);
}
if(cur.right != null) {
queue.offer(cur.right);
}
}
}
六、二叉树的重建
根据二叉树的遍历结果重建二叉树,前序遍历和后序遍历用来确定根,中序遍历用来确定左右节点。因此,只知道前序遍历结果和后序遍历结果不能重建二叉树,必须要有中序遍历结果。
1、前序、中序结果重建二叉树
通过前序遍历知道A是根,则在中序中按照A将其分成左右两部分。然后B也是根,则DBE就很明显了。剩下CF,C是根,在中序中是FC,说明F在C的左边。
//前中序重建二叉树
/**
*
*class Node{
* public char val;
* public Node left;
* public Node right;
* public Node(char value) {
* this.val = value;
* }
*}
*
*/
public static Node reCreateTreeWithPreAndIn(char[] pre,char[] in) {
if (pre.length == 0 || in.length == 0) return null;
Node root = new Node(pre[0]); //前序遍历的第一个节点就是根
for (int i = 0; i < in.length; i++) {
if (pre[0] == in[i]) { //在中序遍历中找根,找到然后分成两部分递归
//Arrays.copyOfRange()左闭右开
root.left = reCreateTreeWithPreAndIn(Arrays.copyOfRange(pre,1,i + 1),Arrays.copyOfRange(in,0,i));
root.right = reCreateTreeWithPreAndIn(Arrays.copyOfRange(pre,i + 1,pre.length),Arrays.copyOfRange(in,i + 1,in.length));
}
}
return root;
}
2、中序、后序结果重建二叉树
通过后序知道A是根,然后将中划分为两个部分,接着C是根,由于DBE划分在左边,右边只有FC,C是根,根据中序遍历左根右知F是左。然后B是根,B的左右分别为D、E。
public static Node reCreateTreeWitchInAndPost(char[] post,char[] in) {
if (post.length == 0 || in.length == 0) return null;
Node root = new Node(post[post.length - 1]); //后序遍历最后一个元素就是根
for (int i = 0; i < in.length; i++) {
if (post[post.length - 1] == in[i]) { //中序遍历中找根,然后分成两部分递归
//Arrays.copyOfRange()左闭右开
root.left = reCreateTreeWitchInAndPost(Arrays.copyOfRange(post,0,i),Arrays.copyOfRange(in,0,i ));
root.right = reCreateTreeWitchInAndPost(Arrays.copyOfRange(post,i,post.length - 1),Arrays.copyOfRange(in,i + 1,in.length));//in从i + 1到length。不能是length - 1,因为左闭右开,length - 1会少一个元素
}
}
return root;
}
3、层序结果重建二叉树
层序遍历结果创建二叉树需要用到队列,具体方法看图
注意:层序遍历重建树时输入的数组必须是包装类数组,且数组的数据个数是一个满二叉树的节点个数。
- 包装类数组可以存null,方便比较
- 使用队重建二叉树,一次从数组中读取两个值,重建左右子树,因此数组中节点个数必须是一个满二叉树的节点个数。null节点用null填入
public static Node reCreateTreeWithLevel(Character[] level) { //包装类数组,可以存null
if (level.length == 0) return null;
Node root = new Node(level[0]); //层序遍历第一个就是根
int i = 1; //数组下标
Queue<Node> queue = new LinkedList<Node>();
queue.offer(root); //根入队
while(i < level.length) {
Node cur = queue.poll();
Character temp = level[i++];
if (temp != null ) { //左节点不为空
Node node = new Node(temp);
cur.left = node;
queue.offer(node);
}
temp = level[i++];
if (temp != null) { //右节点不为空
Node node = new Node(temp);
cur.right = node;
queue.offer(node);
}
}
return root;
}