import java.util.Stack;
/**
* 分别用递归和非递归的方式遍历二叉树
*/
public class PreInPosTraversal {
public static class Node {
public int data;
public Node left;
public Node right;
public Node(int data) {
this.data = data;
}
}
/**
* 递归方式先序遍历
* @param head
*/
public static void preOrderRecur(Node head) {
if (head == null) {
return;
}
System.out.print(head.data+" ");
preOrderRecur(head.left);
preOrderRecur(head.right);
}
/**
* 递归方式中序遍历
* @param head
*/
public static void inOrderRecur(Node head) {
if (head == null) {
return;
}
inOrderRecur(head.left);
System.out.print(head.data+" ");
inOrderRecur(head.right);
}
/**
* 递归方式后序遍历
* @param head
*/
public static void posOrderRecur(Node head) {
if (head == null) {
return;
}
posOrderRecur(head.left);
posOrderRecur(head.right);
System.out.print(head.data+" ");
}
/**
* 递归方式先序遍历
* @param head
*/
public static void preOrderUnRecur(Node head) {
if (head != null) {
Stack stack = new Stack<>();
stack.push(head);
while (!stack.isEmpty()) {
head=stack.pop();
System.out.print(head.data+" ");
if (head.right != null) {
stack.push(head.right);
}
if (head.left != null) {
stack.push(head.left);
}
}
}
}
/**
* 递归方式中序遍历
*/
public static void inOrderUnRecur(Node head) {
if (head != null) {
Stack stack = new Stack<>();
while (head != null || !stack.isEmpty()) {
if (head != null) {
stack.push(head);
head = head.left;
} else {
head=stack.pop();
System.out.print(head.data+" ");
head=head.right;
}
}
}
}
/**
*
*/
public static void posOrderUnRecur(Node head) {
if (head != null) {
Stack stack = new Stack<>();
Stack res = new Stack<>();
stack.push(head);
while (!stack.isEmpty()) {
head=stack.pop();
res.push(head);
if (head.left != null) {
stack.push(head.left);
}
if (head.right != null) {
stack.push(head.right);
}
}
while (!res.isEmpty()) {
System.out.print(res.pop().data+" ");
}
}
}
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);
System.out.println();
inOrderUnRecur(head);
System.out.println();
posOrderUnRecur(head);
}
}
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 SuccessorNode {
public static class Node {
public int data;
public Node parent;
public Node left;
public Node right;
public Node(int data) {
this.data = data;
}
}
public static Node findSuccessorNode(Node node) {
if (node == null) {
return node;
}
if (node.right != null) {
return getLeftMost(node.right);
} else {
Node parent = node.parent;
while (parent != null && parent.left != node) {
node = parent;
parent = parent.parent;
}
return parent;
}
}
private static Node getLeftMost(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.data + " next: " + findSuccessorNode(test).data);
test = head.left.left.right;
System.out.println(test.data + " next: " + findSuccessorNode(test).data);
test = head.left;
System.out.println(test.data + " next: " + findSuccessorNode(test).data);
test = head.left.right;
System.out.println(test.data + " next: " + findSuccessorNode(test).data);
test = head.left.right.right;
System.out.println(test.data + " next: " + findSuccessorNode(test).data);
test = head;
System.out.println(test.data + " next: " + findSuccessorNode(test).data);
test = head.right.left.left;
System.out.println(test.data + " next: " + findSuccessorNode(test).data);
test = head.right.left;
System.out.println(test.data + " next: " + findSuccessorNode(test).data);
test = head.right;
System.out.println(test.data + " next: " + findSuccessorNode(test).data);
test = head.right.right; // 10's next is null
System.out.println(test.data + " next: " + findSuccessorNode(test));
}
}
import java.util.LinkedList;
import java.util.Queue;
/**
* 序列化和反序列化二叉树
*/
public class SerializeAndReconstructTree {
public static class Node {
public int data;
public Node left;
public Node right;
public Node(int data) {
this.data = data;
}
}
public static String preSerializeTree(Node head) {
if (head == null) {
return null;
}
String str = head.data + "_";
if (head.left != null) {
str += preSerializeTree(head.left);
} else {
str += "#_";
}
if (head.right != null) {
str += preSerializeTree(head.right);
} else {
str += "#_";
}
return str;
}
public static Node preReconstructTree(String str) {
if (str == null) {
return null;
}
Queue queue = new LinkedList();
Node head = null;
Node res = null;
String nodes[] = str.split("_");
for (int i = 0; i < nodes.length; i++) {
if (!"#".equals(nodes[i])) {
queue.add(new Node(Integer.valueOf(nodes[i])));
} else {
queue.add(null);
}
}
return process(queue);
}
public static Node process(Queue queue) {
Node head = queue.poll();
if (head == null) {
return null;
}
head.left = process(queue);
head.right = process(queue);
return head;
}
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.data + 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(2);
head.right = new Node(3);
String pre = preSerializeTree(head);
System.out.println("serialize tree by pre-order: " + pre);
Node head1 = preReconstructTree(pre);
printTree(head1);
}
}
/**
* 判断一颗二叉树是否是平衡二叉树
*/
public class IsBalancedTree {
public static class Node{
public int data;
public Node left;
public Node right;
public Node(int data) {
this.data = data;
}
}
public static class ReturnData{
public int height;
public boolean isB;
public ReturnData(int height, boolean isB) {
this.height = height;
this.isB = isB;
}
}
public static boolean isB(Node head) {
return process(head).isB;
}
public static ReturnData process(Node head) {
if (head == null) {
return new ReturnData(0, true);
}
ReturnData leftData=process(head.left);
if (!leftData.isB) {
return new ReturnData(0, false);
}
ReturnData rightData=process(head.right);
if (!rightData.isB) {
return new ReturnData(0, false);
}
if (Math.abs(leftData.height - rightData.height) > 1) {
return new ReturnData(0, false);
}
return new ReturnData(Math.max(leftData.height , rightData.height)+1, true);
}
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(isB(head));
}
}
判断一棵树是否是二叉搜索树的思路:若二叉树的中序遍历序列的值是依次升序的,则满足条件
代码:
/**
* 判断一棵树是否是搜索二叉树
*/
public class IsBST {
public static class Node{
public int data;
public Node left;
public Node right;
public Node(int data) {
this.data = data;
}
}
public static boolean isBST(Node head) {
int res=Integer.MIN_VALUE;
if (head == null) {
return true;
}
Stack stack = new Stack<>();
while (head != null || !stack.isEmpty()) {
if (head != null) {
stack.push(head);
head=head.left;
}else {
head=stack.pop();
res = Math.max(res, head.data);
if (res != head.data) {
return false;
}
head=head.right;
}
}
return true;
}
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(12);
head.right.right.right = new Node(11);
System.out.println(isBST(head));
}
}
判断一棵树是否是完全二叉树
思路:
代码:
import java.util.LinkedList;
import java.util.Queue;
public class IsCBT {
public static class Node{
public int data;
public Node left;
public Node right;
public Node(int data) {
this.data = data;
}
}
public static boolean isCBT(Node head) {
if (head == null) {
return true;
}
Queue queue=new LinkedList<>();
Node left=null;
Node right=null;
boolean leaf=false;
queue.add(head);
while (!queue.isEmpty()) {
head=queue.poll();
left=head.left;
right=head.right;
if (leaf && (left!=null || right!=null) || (right != null && left == null)) {
return false;
}
if (left != null) {
queue.add(left);
}
if (right != null) {
queue.add(right);
}
if (left == null || right == null) {
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(isCBT(head));
}
}
##已知一棵完全二叉树,求其节点的个数(要求:时间复杂度低于O(N),N为这棵树的节点个数)
public class CompleteTreeNodeNumber {
public static class Node{
public int data;
public Node left;
public Node right;
public Node(int data) {
this.data = data;
}
}
public static int nodeNum(Node head) {
if (head == null) {
return 0;
}
return bs(head,1,mostLeftLevel(head,1));
}
private static int bs(Node node, int level, int h) {
if (level == h) {
return 1;
}
if (h == mostLeftLevel(node.right, level + 1)) {
return (1 << (h - level)) + bs(node.right, level + 1, h);
}else {
return (1 << (h - level-1)) + bs(node.left, level + 1, h);
}
}
private 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));
}
}
public class TrieTree {
public static class TrieNode {
public int path;
public int end;
public TrieNode[] nexts;
public TrieNode() {
this.path = 0;
this.end = 0;
this.nexts = new TrieNode[26];
}
}
public static class Trie {
TrieNode root;
public Trie() {
root = new TrieNode();
}
//向前缀树中添加一个字符串
public void insert(String word) {
if (word == null) {
return;
}
TrieNode node = root;
char[] chs = word.toCharArray();
for (int i = 0; i < chs.length; i++) {
int index = chs[i] - 'a';
if (node.nexts[index] == null) {
node.nexts[index] = new TrieNode();
}
node = node.nexts[index];
node.path++;
}
node.end++;
}
//在前缀树中查找字符串
public int search(String word) {
if (word == null) {
return 0;
}
TrieNode node = root;
char[] chs = word.toCharArray();
for (int i = 0; i < chs.length; i++) {
int index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.end;
}
//删除某个字符串
public void delete(String word) {
if (search(word) == 0) {
return;
}
TrieNode node = root;
char[] chs = word.toCharArray();
for (int i = 0; i < chs.length; i++) {
int index = chs[i] - 'a';
if (--node.nexts[index].path == 0) {
node.nexts[index] = null;
return;
}
node = node.nexts[index];
}
node.end--;
}
//查找以某一个字符串序列开头的字符串个数
public int prefixNumber(String pre) {
if (pre == null) {
return 0;
}
TrieNode node = root;
char[] chs = pre.toCharArray();
for (int i = 0; i < chs.length; i++) {
int index = chs[i] - 'a';
if (node.nexts[index] == null) {
return 0;
}
node = node.nexts[index];
}
return node.path;
}
public static void main(String[] args) {
Trie trie = new Trie();
System.out.println(trie.search("zuo"));
trie.insert("zuo");
System.out.println(trie.search("zuo"));
trie.delete("zuo");
System.out.println(trie.search("zuo"));
trie.insert("zuo");
trie.insert("zuo");
trie.delete("zuo");
System.out.println(trie.search("zuo"));
trie.delete("zuo");
System.out.println(trie.search("zuo"));
trie.insert("zuoa");
trie.insert("zuoac");
trie.insert("zuoab");
trie.insert("zuoad");
trie.delete("zuoa");
System.out.println(trie.search("zuoa"));
System.out.println(trie.prefixNumber("zuo"));
}
}
}