实现二叉树的先序、中序、后序遍历,包括递归方式和非递归方式
上图为从上到下,从左到右,递归方式真实遇到节点的情况。
(遇到4时其左节点为null重新回到4,再看4的右节点还是null,再回到4,故....4,4,4.....; 其余节点类似。)
先中后序遍历的区别,即为每个数的打印时机:
- 先序
- 递归:打印第一次出现的数:1,2,4,5,3,6,7
- 非递归:从上到下,中左右
- 中序
- 递归方式理解:打印第二次出现的数:4,2,5,1,6,3,7
- 非递归方式理解:从上到下,左中右
在中序遍历中,一个节点的前一个节点被称为先导节点,后一个节点被称为后继节点(比如4后继节点为2,5后继1)
-
后序:
- 递归理解:打印第三次出现的数:4,5,2,6,7,3,1
- 非递归理解:从下到上,左右中
可由先序推导得出: 先序:中左右 -〉 中右左(压栈的时候先左后右)-〉 左右中(将上一步的结果先放入一个辅助栈中再弹出)即得出后序遍历
代码实现:
package com.godzuo.java;
import java.util.Stack;
/**
* @author quanquan
* @create 2020-04-27-11:35
*/
public class PreInPosTraversal {
public static class Node{
private int value;
private Node left;
private Node right;
public Node(int value){
this.value = value;
}
}
//递归方法======================================================================
//递归方式天然的,可以回到一个节点三次
//先序遍历
public static void preOrderRecur(Node head){
if (head == null){
return;
}
//递归中,第一次触达当前head节点的地方:自身作为头第一次进来
System.out.print(head.value + " ");
preOrderRecur(head.left);
//递归中,第二次:左边递归完后出来回到这里
preOrderRecur(head.right);
//递归中,第三次:右边递归出来后会到这里
}
//中序遍历
public static void inOrderRecur(Node head) {
if (head == null) {
return;
}
inOrderRecur(head.left);
System.out.print(head.value + " ");
inOrderRecur(head.right);
}
//后序遍历
public static void posOrderRecur(Node head) {
if (head == null) {
return;
}
posOrderRecur(head.left);
posOrderRecur(head.right);
System.out.print(head.value + " ");
}
//非递归方法=====================================================================
//递归本质是一个函数的调用栈
//这里自己搞了个栈来实现
//为什么要使用栈:二叉树本身,遍历是从上到下的,不回返的;但又需要通过某种结构让他能够回去(回到上一节点找另一边子节点)
//那么栈结构“压栈出栈”就符合这种要求。而如果使用队列的话,还是只能单向的流动。
//先序遍历
public static void preOrderUnRecur(Node head){
System.out.println("preOrder:");
if (head != null){
Stack<Node> stack = new Stack<>();
stack.push(head);
while (!stack.isEmpty()){
head = stack.pop();
System.out.print(head.value + " ");
//整体的顺序是先压右,再压左,这样弹出时,即先左后右
if (head.right != null){
stack.push(head.right);
}
if (head.left != null){
stack.push(head.left);
}
}
}
System.out.println();
}
//中序遍历
public static void inOrderUnRecur(Node head){
System.out.println("inOrder:");
if (head != null){
Stack<Node> stack = new Stack<>();
while(!stack.isEmpty() || head != null){ //如果都为空,说明没东西了
if(head != null){
//当前节点不为空,压栈,当前节点继续左移。
//不为空时,每次循环都能进来,一压压一溜,把所有左边都压入了
stack.push(head);
head = head.left;
}else { //当前节点为空,出栈一个,打印;然后当前节点右移
head = stack.pop();
System.out.print(head.value + " ");
head = head.right;
}
}
}
System.out.println();
}
//后序遍历
public static void posOrderUnRecur(Node head){
System.out.println("proOrder:");
if(head != null){
Stack<Node> stack1 = new Stack<>();
Stack<Node> stack2 = new Stack<>();
stack1.push(head);
while (!stack1.isEmpty()){
head = stack1.pop();
//这里即为先序遍历时打印的地方,而后序这里压入辅助栈中
stack2.push(head);
if (head.left != null){
stack1.push(head.left);
}
if (head.right != null){
stack1.push(head.right);
}
}
//上面循环跑完之后,再统一打印,即~
while (!stack2.isEmpty()){
System.out.print(stack2.pop().value + " ");
}
}
System.out.println();
}
public static void main(String[] args) {
Node head = new Node(5);
head.left = new Node(3);
head.right = new Node(8);
head.left.left = new Node(2);
head.left.right = new Node(4);
head.left.left.left = new Node(1);
head.right.left = new Node(7);
head.right.left.left = new Node(6);
head.right.right = new Node(10);
head.right.right.left = new Node(9);
head.right.right.right = new Node(11);
// recursive
System.out.println("==============recursive==============");
System.out.print("pre-order: ");
preOrderRecur(head);
System.out.println();
System.out.print("in-order: ");
inOrderRecur(head);
System.out.println();
System.out.print("pos-order: ");
posOrderRecur(head);
System.out.println();
// unrecursive
System.out.println("============unrecursive=============");
preOrderUnRecur(head);
inOrderUnRecur(head);
posOrderUnRecur(head);
}
}
直观的打印一棵二叉树(工具性质)
打印出的结果以及看法(留意其中的“V”和“^” 标识方向,用来表示和自己最近的节点的关系)
package class_04;
public class Code_02_PrintBinaryTree {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static void printInOrder(Node head, int height, String to, int len) {
if (head == null) {
return;
}
printInOrder(head.right, height + 1, "v", len);
String val = to + head.value + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
System.out.println(getSpace(height * len) + val);
printInOrder(head.left, height + 1, "^", len);
}
public static String getSpace(int num) {
String space = " ";
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf.toString();
}
public static void main(String[] args) {
Node head = new Node(1);
head.left = new Node(-222222222);
head.right = new Node(3);
head.left.left = new Node(Integer.MIN_VALUE);
head.right.left = new Node(55555555);
head.right.right = new Node(66);
head.left.left.right = new Node(777);
printTree(head);
head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.right.left = new Node(5);
head.right.right = new Node(6);
head.left.left.right = new Node(7);
printTree(head);
head = new Node(1);
head.left = new Node(1);
head.right = new Node(1);
head.left.left = new Node(1);
head.right.left = new Node(1);
head.right.right = new Node(1);
head.left.left.right = new Node(1);
printTree(head);
}
}
在二叉树中找到一个节点的后继节点
现在有一种新的二叉树节点类型如下:
public class Node {
public int value;
public Node left;
public Node right;
public Node parent; //多了个父节点
public Node(int data) {
this.value = data; }
}
该结构比普通二叉树节点结构多了一个指向父节点的parent指针。假设有一 棵Node类型的节点组成的二叉树,树中每个节点的parent指针都正确地指向自己的父节点,头节点的parent指向null。只给一个在二叉树中的某个节点node,请实现返回node的后继节点的函数。在二叉树的中序遍历的序列中, node的下一个节点叫作node的后继节点。
- 思路:
(规律总结出的)
- 如果一个节点有右子树,则它的后继节点为他的右子树上最左的节点(右子树上再无节点则为自身)。(正常情况,因为中序遍历:从下到上,左中右)
- 如果一个元素没有右子树,则寻找哪一个节点的左子树是以该节点结尾的:
- 即向上找,找到某一X节点,该X节点是它父节点的左孩子,则X节点的父节点即为那个原始节点后继节点;
package com.godzuo.java;
public class SuccessorNode {
public static class Node{
public int value;
public Node left;
public Node right;
public Node parent; //最顶节点的父节点为空
public Node(int data) { this.value = data; }
}
public static Node getSuccessorNode(Node node){
if (node == null) return null;
if (node.right != null){ //有右子树,获取右子树的左孩子。
return getLeft(node.right);
}else{
Node parent = node.parent;
//parent!=null边界条件,以免整个树的最后一个节点无后继。接着后面直接return null了
while (parent != null && parent.left != node){
node = parent;
parent = node.parent;
}
return parent;
}
}
public static Node getLeft(Node head){
if (head == null){
return head;
}
while (head.left != null){
head = head.left;
}
return head;
}
public static void main(String[] args) {
Node head = new Node(6);
head.parent = null;
head.left = new Node(3);
head.left.parent = head;
head.left.left = new Node(1);
head.left.left.parent = head.left;
head.left.left.right = new Node(2);
head.left.left.right.parent = head.left.left;
head.left.right = new Node(4);
head.left.right.parent = head.left;
head.left.right.right = new Node(5);
head.left.right.right.parent = head.left.right;
head.right = new Node(9);
head.right.parent = head;
head.right.left = new Node(8);
head.right.left.parent = head.right;
head.right.left.left = new Node(7);
head.right.left.left.parent = head.right.left;
head.right.right = new Node(10);
head.right.right.parent = head.right;
Node test = head.left.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.left.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.left.right.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.left.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.left;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right;
System.out.println(test.value + " next: " + getSuccessorNode(test).value);
test = head.right.right; // 10's next is null
System.out.println(test.value + " next: " + getSuccessorNode(test));
}
}
找前驱节点:
- x如果有左子树,则左子树上最右的节点为它的前驱,
- 如果x没有左子树,则寻找哪一个节点的右子树是以该节点结尾的(即向上找,直到当前节点是他父节点的右孩子)。
二叉树的序列化和反序列化
- 按先序方式序列化:
- 按层方式做序列化
package com.godzuo.java;
import java.util.LinkedList;
import java.util.Queue;
public class SerializeAndReconstructTree {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
//这里的 "!" 即和上图的下划线“_”一个意思,表示遇到一个新节点,上个节点的结尾
//前序序列化
public static String serialByPre(Node head){
if (head == null){
return "#!";
}
String res = head.value + "!";
res += serialByPre(head.left);
res += serialByPre(head.right);
return res;
}
//前序反序列化
public static Node reconByPreString(String preStr){
String[] values = preStr.split("!");
//也可以直接使用上面的数组,每次用角标标记就是了,用队列可方便每次取出
Queue<String> queue = new LinkedList<>();
for (int i = 0; i != values.length; i++){
queue.offer(values[i]);
}
return reconPreOrder(queue);
}
public static Node reconPreOrder(Queue<String> queue){
String value = queue.poll();
//遇到空,直接返回
if(value.equals("#")){
return null;
}
//开始消费当前一个值,前序方式反序列化:中-左-右
Node head = new Node(Integer.valueOf(value));
head.left = reconPreOrder(queue);
head.right = reconPreOrder(queue);
return head;
}
//层序序列化=================================================================================
public static String serialByLevel(Node head){
if (head == null){
return "#!";
}
String res = head.value + "!";
Queue<Node> queue = new LinkedList<>();
queue.offer(head);
while (!queue.isEmpty()){
head = queue.poll();
if (head.left != null) {
res += head.left.value + "!";
queue.offer(head.left);
} else {
res += "#!";
}
if (head.right != null) {
res += head.right.value + "!";
queue.offer(head.right);
} else {
res += "#!";
}
}
return res;
}
//层序反序列化
public static Node reconByLevelString(String levelStr) {
String[] values = levelStr.split("!");
int index = 0;
Node head = generateNodeByString(values[index++]);
Queue<Node> queue = new LinkedList<Node>();
if (head != null) {
queue.offer(head);
}
Node node = null;
while (!queue.isEmpty()) {
node = queue.poll();
node.left = generateNodeByString(values[index++]);
node.right = generateNodeByString(values[index++]);
if (node.left != null) {
queue.offer(node.left);
}
if (node.right != null) {
queue.offer(node.right);
}
}
return head;
}
public static Node generateNodeByString(String val) {
if (val.equals("#")) {
return null;
}
return new Node(Integer.valueOf(val));
}
// for test -- print tree
public static void printTree(Node head) {
System.out.println("Binary Tree:");
printInOrder(head, 0, "H", 17);
System.out.println();
}
public static void printInOrder(Node head, int height, String to, int len) {
if (head == null) {
return; }
printInOrder(head.right, height + 1, "v", len);
String val = to + head.value + to;
int lenM = val.length();
int lenL = (len - lenM) / 2;
int lenR = len - lenM - lenL;
val = getSpace(lenL) + val + getSpace(lenR);
System.out.println(getSpace(height * len) + val);
printInOrder(head.left, height + 1, "^", len);
}
public static String getSpace(int num) {
String space = " ";
StringBuffer buf = new StringBuffer("");
for (int i = 0; i < num; i++) {
buf.append(space);
}
return buf.toString();
}
public static void main(String[] args) {
Node head = null;
printTree(head);
String pre = serialByPre(head);
System.out.println("serialize tree by pre-order: " + pre);
head = reconByPreString(pre);
System.out.print("reconstruct tree by pre-order, ");
printTree(head);
String level = serialByLevel(head);
System.out.println("serialize tree by level: " + level);
head = reconByLevelString(level);
System.out.print("reconstruct tree by level, ");
printTree(head);
System.out.println("====================================");
head = new Node(1);
printTree(head);
pre = serialByPre(head);
System.out.println("serialize tree by pre-order: " + pre);
head = reconByPreString(pre);
System.out.print("reconstruct tree by pre-order, ");
printTree(head);
level = serialByLevel(head);
System.out.println("serialize tree by level: " + level);
head = reconByLevelString(level);
System.out.print("reconstruct tree by level, ");
printTree(head);
System.out.println("====================================");
head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.right.right = new Node(5);
printTree(head);
pre = serialByPre(head);
System.out.println("serialize tree by pre-order: " + pre);
head = reconByPreString(pre);
System.out.print("reconstruct tree by pre-order, ");
printTree(head);
level = serialByLevel(head);
System.out.println("serialize tree by level: " + level);
head = reconByLevelString(level);
System.out.print("reconstruct tree by level, ");
printTree(head);
}
}
判断平衡二叉树
- 满二叉树一定是平衡二叉树
- 平衡二叉树是指,在这棵树的任何一个节点,其左子树和右子树的高度差不超过1。
思路:
需要收集的信息:
左树是否平衡,右树是否平衡
左树的高度,右树的高度
package com.godzuo.java;
public class IsBalance {
public static class Node {
public int value;
public Node left;
public Node right;
public Node(int data) {
this.value = data;
}
}
//定义一个返回体,包含两个信息
public static class ReturnData {
public boolean balance;
public int height;
public ReturnData(boolean balance, int height){
this.balance = balance;
this.height = height;
}
}
//递归主体函数
public static ReturnData process(Node head){
if (head == null){
return new ReturnData(true,0);
}
//假设通过这个函数可以获取到返回信息,左边
ReturnData leftData = process(head.left);
if (!leftData.balance){
return new ReturnData(false,0); //不平衡时,直接返回,高度随便设定(这里置为0)后面不会再用到
}
//。。。 右边
ReturnData rightData = process(head.right);
if (!rightData.balance){
return new ReturnData(false,0);
}
if (Math.abs(leftData.height-rightData.height)>1){
return new ReturnData(false,0);
}
//上述主逻辑走完,最后要返回当前这个节点的信息:经过上面判定肯定平衡,其高度为左右两边高度的最大值+1
return new ReturnData(true,Math.max(leftData.height,rightData.height)+1);
}
public static void main(String[] args) {
Node head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.right.left = new Node(6);
head.right.right = new Node(7);
System.out.println(process(head).balance);
}
}
TIPS:二叉树有一个套路化的模式:递归很好用! 因为对于二叉树的递归来讲,一个节点X会被光顾三次(先来到X,然后X.left再回到X,然后然后X.right再回到X): 这样的一个过程,天然的很容易将一个节点的子节点的各种信息收集回来,进一步处理。 比如上述process的过程,需要收集的信息,定义为ReturnData,然后分别对当前节点的左节点,右节点进行收集,最后在返回当前节点的信息。 树形DB? 二叉树上的动态规划??
判断搜索二叉树/完全二叉树
- 搜索二叉树:对于该树上的任何一个节点X,作为父节点时,其左子节点都小于X,其右子节点都大于X。(一般来讲,搜索二叉树是不会有重复节点的,用于搜索,同一key内容都会放在一个节点中,没必要有重复节点)
- 完全二叉树,在堆排序(数组结构实现的堆,也可以用二叉树实现一个堆,较难不常见罢了;使用二叉树实现的堆,相比数组,没有扩容代价,不会提前浪费空间)那节提到过,从上到下,从左到右依次有的,否则不是完全二叉树。
- 判断搜索二叉树:二叉树中序遍历(左中右),依次升序的,即为搜索二叉树。
可以使用之前的非递归中序遍历版本,在打印处,当前数和上一个数(在循环外使用一个临时变量last,初始值可定义为Integer.MIN_VALUE)比较是否要更大一些,否则直接return。
- 判断完全二叉树:按层遍历,发现的规律
- 一个节点只有右孩子没有左孩子,则非完全二叉树,
- 一个节点不是左右孩子都有(即有左没右或者两个都没有),后面的所有节点如果都是叶节点(不作为父节点,没有子节点),则为完全二叉树
package com.godzuo.java;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
public class IsBSTAndCBT {
public static class Node{
private int value;
private Node left;
private Node right;
public Node(int value){
this.value = value;
}
}
//判断是否为搜索二叉树
public static Boolean isBST(Node head){
int pre = Integer.MIN_VALUE;
if (head == null){
return true;
}
Stack<Node> stack = new Stack<>();
while (!stack.isEmpty() || head != null){
if (head != null){
stack.push(head);
head = head.left;
}else {
head =stack.pop();
System.out.println(head.value + " ");
Boolean isBST =head.value > pre ? true : false;
if (!isBST){
return isBST;
}
pre = head.value;
head = head.right;
}
}
return true;
}
//判断是否为完全二叉树
public static boolean isCBT(Node head){
if (head == null) {
return true;
}
Queue<Node> queue = new LinkedList<>();
boolean leaf = false;
Node l = null;
Node r = null;
queue.offer(head);
while (!queue.isEmpty()){
head = queue.poll();
l = head.left;
r = head.right;
if ((leaf && (l != null || r != null)) // 是否开启了判断叶节点的阶段&&其左/右都不能有值,有的话就不是叶节点了
|| (l == null && r != null)){ //没开启叶节点判断阶段时,如果左无右有,直接false。
return false;
}
//后面这段等效于下面注释的代码
if (l != null){
queue.offer(l);
}
if (r != null){
queue.offer(r);
}
if(l != null || r != null){
leaf = true;
}
//if (l != null){
// queue.offer(l);
//}
//if (r != null){
// queue.offer(r);
//}else {
// leaf = true;
//}
}
return true;
}
public static void main(String[] args) {
Node head = new Node(4);
head.left = new Node(2);
head.right = new Node(6);
head.left.left = new Node(1);
head.left.right = new Node(3);
head.right.left = new Node(5);
System.out.println(isBST(head));
System.out.println(isCBT(head));
}
}
已知一棵完全二叉树,求其节点的个数
要求:时间复杂度低于O(N),N为这棵树的节点个数。
思路:因为时完全二叉树,其每个节点的子节点都是从左到右依次排满的。另外:一个满二叉树,高度为L(从1开始,一层就是1,二层就是2),其总节点数公式为:2L - 1 个。
- 设定该完全二叉树的父节点为X,获取其左边界,即拿到了该树的深度h,再判断X的右子节点的左边界,判断深度是否为h
- 等于h,说明X的左子树整个为一个完全二叉树,左子树可通过公式计算,右子树递归计算。
- 不等于h,说明X的右子树整个为一个完全二叉树(高度比左边-1),右子树公式计算,左子树递归计算
package com.godzuo.java;
public class CompleteTreeNodeNumber {
public static class Node{
private int value;
private Node left;
private Node right;
public Node(int value){
this.value = value;
}
}
//时间复杂度O(logN)^2
//第一个㏒₂N:因为到每一层只需要遍历其中的一个,一共有O(㏒₂N)层
//第一个㏒₂N:计算每一个左子树的边界,也是一个O(㏒₂N)
public static int nodeNum(Node head){
if (head == null){
return 0;
}
return bs(head,1,mostLeftLevel(head,1));
}
//node为当前节点,l指node在第几层,h指整颗树的深度,一个不变的全局变量,
//返回当前节点的总个数
public static int bs(Node node, int l, int h){
if (l == h){ //已经是最后一层,那即为叶节点,即节点个数只有一个
return 1;
}
//判断右子树的左边界的深度
if (mostLeftLevel(node.right,l+1) == h){
//即,左子树是个满二叉树了,(左子树就可以使用公式计算+一个父节点)+ (右子树个数)
//一个数a左移n位,相当于a ✖ 2的n次方(左移<<,无符号左移<<<)
return (1 << (h-l) - 1 + 1) + bs(node.right, l+1,h);
}else {
return (1 << (h-l-1) - 1 + 1) + bs(node.left,l+1,h);
}
}
//level,node所在的层数
public static int mostLeftLevel(Node node, int level){
while (node != null){
level++;
node = node.left;
}
return level-1;
}
public static void main(String[] args) {
Node head = new Node(1);
head.left = new Node(2);
head.right = new Node(3);
head.left.left = new Node(4);
head.left.right = new Node(5);
head.right.left = new Node(6);
System.out.println(nodeNum(head));
}
}