Java练习代码(一)

写下今天的总结, 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));
    }
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值