class 4
DAY6
- 实现二叉树的先序、中序、后序遍历,包括递归方式和非递归方式
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;
}
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);
}
}
- 如何直观的打印一颗二叉树
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没有左子树,则寻找哪一个节点的右子树是已该节点结尾的(即向上找,直到当前节点是他父节点的右孩子)。
package com.godzuo.java;
/**
* @author quanquan
* @create 2020-04-28-14:31
*/
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;
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));
}
}
- 介绍二叉树的序列化和反序列化
package com.godzuo.java;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author quanquan
* @create 2020-04-28-22:37
*/
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次,压出折痕后展开。此时 折痕是凹下去的,即折痕突起的方向指向纸条的背面。如果从纸条的下边向上方连续对折2 次,压出折痕后展开,此时有三条折痕,从上到下依次是下折痕、下折痕和上折痕。
给定一 个输入参数N,代表纸条都从下边向上方连续对折N次,请从上到下打印所有折痕的方向。 例如:N=1时,打印: down;N=2时,打印: down down up【答】第一次对折产生的折痕看作是根结点,那么第二次对折产生的下折痕就是该结点的左子结点,二第二次对折产生的上折痕就是该结点的右子结点;
这棵树有这样的特点:
1:根结点为下折痕;
2:每一个结点的左子结点为下折痕;
3:每一个结点的右子结点为上折痕;则,打印折痕则为这个树的中序遍历
package com.godzuo.java;
/**
* @author quanquan
* @create 2020-04-29-14:50
*/
public class PaperFolding {
public static void printAllFolds(int N){
printProcess(1, N,true); //true表示下折痕
}
//i为当前所在的层数,利用down来区分左子树和右子树
public static void printProcess(int i,int N,boolean down){
if (i>N){
return;
}
printProcess(i+1,N,true);
System.out.println(down ? "down" : "up");
printProcess(i+1,N,false);
}
public static void main(String[] args) {
printAllFolds(3);
}
}
-
判断一棵二叉树是否是平衡二叉树
树形BP?
package com.godzuo.java;
/**
* @author quanquan
* @create 2020-04-29-18:08
*/
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);
}
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);
}
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);
}
}
-
判断一棵树是否是搜索二叉树、判断一棵树是否是完全二叉树
判断是否是搜索二叉树:一个二叉树的中序遍历结果是升序的,则为搜索二叉树(一般搜索二叉树不含重复节点)
判断是否为完全二叉树:按层遍历1.一个节点只有右孩子没有左孩子,则非完全二叉树,2.一个节点不是左右孩子都有(即有左没右或者两个都没有),后面的所有节点都是叶节点,则为完全二叉树
package com.godzuo.java;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;
/**
* @author quanquan
* @create 2020-04-29-20:27
*/
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)){
return false;
}
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为这棵树的节点个数
package com.godzuo.java;
/**
* @author quanquan
* @create 2020-04-29-22:53
*/
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
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){
return (1 << (h-l)) + bs(node.right, l+1,h);
}else {
return (1 << (h-l-1)) + bs(node.left,l+1,h);
}
}
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));
}
}