写下今天的总结, 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 class test_binTree {
public static void main(String[] args) {
Binary_Tree bt = new Binary_Tree();
int index = 0;
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));
}
}