写下今天的总结, Java二叉树的学习过程代码
package Java20200319;
import sun.text.normalizer.UCharacter;
import java.nio.charset.StandardCharsets;
import java.util.*;
/**
* @Create with IntelliJ IDEA
* @Description :
* @Auther : HMW
* @Date: 2021-03-19
* @Time: 22:08
**/
class Node {
public char data_;
public Node left_;
public Node right_;
//初始化
Node(char data_) {
this.data_ = data_;
this.left_ = this.right_ = null;
}
}
class Binary_Tree {
private int pi = 0;
//成树
Node beTree(String str) {
if(str.isEmpty()) {
return null;
}
if(str.charAt(pi) != '#') {
Node root = new Node(str.charAt(pi));
pi++;
root.left_ = beTree(str);
pi++;
root.right_ = beTree(str);
return root;
}
else {
return null;
}
}
//先序遍历
void preorder(Node root) {
if(root == null) {
return;
}
System.out.print(root.data_ + " ");
preorder(root.left_);
preorder(root.right_);
}
//先序遍历非递归实现
List<Character> preorder1(Node root) {
if(root == null) { return null; }
//创建栈st保存 - 模拟递归回溯
Stack<Node> st = new Stack<>();
List<Character> list = new ArrayList<>();
Node cur = root;
while(cur != null || !st.empty()) {
while(cur != null) {
st.add(cur);
// System.out.print(cur.data_ + " ");
list.add(cur.data_);
cur = cur.left_;
}
//出来之后是应为该左子树昨走完, 处理右子树
Node tmp_node = st.pop();
if(tmp_node.right_ != null) {
cur = tmp_node.right_;
}
}
return list;
}
//中序遍历
void midorder(Node root) {
if(root == null) { return; }
midorder(root.left_);
System.out.print(root.data_ + " ");
midorder(root.right_);
}
//中序遍历非递归实现
List<Character> midorder1(Node root) {
if(root == null) { return null; }
Stack<Node> stack = new Stack<>();
List<Character> list = new ArrayList<>();
Node cur = root;
while(cur != null || !stack.empty()) {
while(cur != null) {
stack.add(cur);
cur = cur.left_;
}
Node tmp_node = stack.pop();
list.add(tmp_node.data_);
if(tmp_node.right_ != null) { cur = tmp_node.right_; }
}
return list;
}
//后序遍历
void endorder(Node root) {
if(root == null) { return; }
endorder(root.left_);
endorder(root.right_);
System.out.print(root.data_ + " ");
}
//后序遍历非递归实现
List<Character> endorder1(Node root) {
if(root == null) { return null; }
Stack<Node> stack = new Stack<>();
List<Character> list = new ArrayList<>();
Node cur = root;
Node last_node = null;
while(cur != null | !stack.empty()) {
while(cur != null) {
stack.add(cur);
cur = cur.left_;
}
Node tmp_node = stack.peek();
if(tmp_node.right_ == null || tmp_node.right_ == last_node) {
list.add(tmp_node.data_);
stack.pop();
last_node = tmp_node;
}
else {
cur = tmp_node.right_;
last_node = tmp_node.right_;
}
}
return list;
}
//判断两棵树是否相同
public boolean isSame(Node head1, Node head2) {
if(head1 == null && head2 == null) { return true; }
if(head1 == null || head2 == null) { return false; }
if(head1.data_ != head2.data_) { return false; }
return isSame(head1.left_, head2.left_) && isSame(head1.right_, head2.right_);
}
//判断一个树是否为另一颗树的子树
public boolean isSubTree(Node head1, Node head2) {
if(head1 == null && head2 == null) { return true; }
if(head1 == null || head2 == null) { return false; }
boolean ret = false;
if(head1.data_ == head2.data_) {
ret = isSame(head1, head2);
}
return ret || isSubTree(head1.left_, head2) || isSubTree(head2.right_, head2);
}
//判断一棵树的高度
public int maxDepth(Node head) {
if(head == null) { return 0; }
if(head.left_ == null && head.right_ == null) { return 1; }
int leftDepth = maxDepth(head.left_);
int rightDepth = maxDepth(head.right_);
return leftDepth > rightDepth ? leftDepth : rightDepth + 1;
}
//判断一棵树是否为完全平衡二叉树
public boolean isBalaceTree(Node root) {
if(root == null) { return true; }
if(root.left_ == null && root.right_ == null) { return true; }
//先进行左右递归, 回到 最底层
int leftDepth = maxDepth(root.left_);
int rightDepth = maxDepth(root.right_);
if(leftDepth - rightDepth > 1 || rightDepth - leftDepth > 1) {
return false;
}
return isBalaceTree(root.left_) && isBalaceTree(root.right_);
}
//判断两棵树是否为镜像
public boolean isMirror(Node root1, Node root2) {
if(root1 == null && root2 == null) { return true; }
if(root1 == null || root2 == null) { return false; }
if(root1.data_ != root2.data_) { return false; }
return isMirror(root1.left_, root1.right_) && isMirror(root1.right_, root2.left_);
}
//层序遍历
public List<Character> levelOrder(Node root) {
//借助队列实现
if(root == null) { return null; }
Queue<Node> queue = new LinkedList<>();
List<Character> list = new ArrayList<>();
queue.offer(root);
while(!queue.isEmpty()) {
int size = queue.size();
for(int i = 0; i < size; i++) {
Node tmp_node = queue.poll();
list.add(tmp_node.data_);
if(tmp_node.left_ != null) { queue.offer(tmp_node.left_); }
if(tmp_node.right_ != null) { queue.offer(tmp_node.right_); }
}
}
return list;
}
//判断是否为完全二叉树
public boolean isInterviewTree(Node root) {
if(root == null) { return true; }
Queue<Node> queue = new LinkedList<>();
queue.offer(root);
//1. 将所有节点信息保存到队列当中
while(!queue.isEmpty()) {
int size = queue.size();
boolean flag = false;
for(int i = 0; i < size; i++) {
Node tmp_node = queue.poll();
if(tmp_node == null) {
flag = true;
break;
}
queue.offer(tmp_node.left_);
queue.offer(tmp_node.right_);
}
if(flag) {
break;
}
}
//2. 对于队列进行分析处理
while(!queue.isEmpty()) {
Node tmp_node = queue.poll();
if(tmp_node != null) { return false; }
}
return true;
}
//层序遍历
public List<List<Character>> levelOrder1(Node root) {
//借助队列实现
if (root == null) {
return null;
}
Queue<Node> queue = new LinkedList<>();
List<List<Character>> list = new ArrayList<>();
queue.offer(root);
while (!queue.isEmpty()) {
int size = queue.size();
List<Character> tmpList = new ArrayList<>();
for (int i = 0; i < size; i++) {
Node tmp_node = queue.poll();
tmpList.add(tmp_node.data_);
if (tmp_node.left_ != null) {
queue.offer(tmp_node.left_);
}
if (tmp_node.right_ != null) {
queue.offer(tmp_node.right_);
}
}
list.add(tmpList);
}
return list;
}
//查找两个结点最近公共父节点
private Node fatherNode = null;
public Node lowertCommonAncestor(Node root, Node p, Node q) {
DFS(root, p, q);
return fatherNode;
}
public boolean DFS(Node root, Node p, Node q) {
if(root == null) { return false; }
int A = DFS(root.left_, p, q) ? 1 : 0;
int B = DFS(root.right_, p, q) ? 1 : 0;
int C = (root == q || root == q) ? 1 : 0;
if(A + B + C > 1) {
fatherNode = root;
}
return (A + B + C) > 0;
}
//根据平衡二叉树构建双向链表
private Node lastNode = null;
public Node convert(Node root) {
if(root == null) { return null; }
healper(root);
while(lastNode.left_ != null) { lastNode = lastNode.left_; }
return lastNode;
}
public void healper(Node root) {
if(root == null) { return; }
if(root.left_ != null) { healper(root.left_); }
root.left_ = lastNode;
if(lastNode != null) { lastNode.right_ = root; }
lastNode = root;
if(root.right_ != null) { healper(root.right_); }
}
//根据前序序列和中序序列构建二叉树
public Node buildTree(char[] preorder, char[] inorder) {
return buildTreeHealper(preorder, 0, preorder.length - 1,
inorder, 0, inorder.length - 1);
}
public Node buildTreeHealper(char[] preorder, int pre_begin, int pre_end,
char[] inorder, int inor_begin, int inor_end) {
if(pre_begin > pre_end || inor_begin > inor_end) {
return null;
}
//创建结点
Node node = new Node(preorder[pre_begin]);
//从中序序列当中找到先序序列的位置
for(int i = inor_begin; i <= inor_end; i++) {
if(inorder[i] == preorder[pre_begin]) {
node.left_ = buildTreeHealper(preorder, pre_begin+1, pre_begin+i-inor_begin,
inorder, inor_begin, i-1);
node.right_ = buildTreeHealper(preorder, pre_begin+i-inor_begin+1, pre_end,
inorder, i+1, inor_end);
break;
}
}
return node;
}
//根据二叉树构建字符串
private StringBuilder sb = new StringBuilder();
public String tree2str(Node root) {
if(root == null) { return ""; }
tree2strHealper(root);
sb.deleteCharAt(0);
return sb.toString();
}
private void tree2strHealper(Node root) {
if(root == null) { return; }
sb.append('(');
sb.append(root.data_);
tree2strHealper(root.left_);
if(root.left_ == null && root.right_ != null) {
sb.append("()");
}
tree2strHealper(root.right_);
sb.append(')');
}
}
public class test_binTree {
public static void main(String[] args) {
Binary_Tree bt = new Binary_Tree();
// Node head = bt.beTree("124##5##3##");
// bt.preorder(head);
// List<Character> list = bt.endorder1(head);
// System.out.println(list);
//
// Binary_Tree bt1 = new Binary_Tree();
// Node head1 = bt1.beTree("24##5##");
// System.out.println(bt.isSame(head, head1));
// System.out.println(bt.isSubTree(head, head1));
// System.out.println(bt.maxDepth(head));
//List<Character> list = bt.levelOrder(head);
// System.out.println(head);
//
// System.out.println(bt.isInterviewTree(head));
//
// List<List<Character>> list = bt.levelOrder1(head);
// for(List<Character> it : list) {
// for(char it1 : it) {
// System.out.print(it1 + " ");
// }
// System.out.println();
// }
// Node head = bt.beTree("532##4##76##8##");
// Node head = bt.beTree("21##3##");
// Node tmp = bt.convert(head);
//
// while(tmp != null) {
// System.out.print(tmp.data_ + " ");
// tmp = tmp.right_;
// }
// System.out.println();
// char[] str1 = {'9', '3', '4', '6', '7'};
// char[] str2 = {'9', '4', '7', '6', '3'};
// Node tmp = bt.buildTree(str1, str2);
// List<Character> list = bt.levelOrder(tmp);
// System.out.println(list);
Node head = bt.beTree("124###3##");
String str = bt.tree2str(head);
System.out.println(str);
}
}