二叉树的链式结构


//二叉树的链式结构及实现
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

public class TextBinaryTree {
    class TreeNode {
        char value;
        TreeNode left;
        TreeNode right;

        public TreeNode(char value) {
            this.value = value;
        }
    }

    //前序遍历
    private void binaryTreePrevOrder(TreeNode root) {
        if(root == null){
            return;
        }
        System.out.println(root.value+" ");
        binaryTreePrevOrder(root.left);
        binaryTreePrevOrder(root.right);
    }
//中序遍历
    private void binaryTreeInOrder(TreeNode root) {
        if(root == null){
            return;
        }
        binaryTreeInOrder(root.left);
        System.out.println(root.value+" ");
        binaryTreeInOrder(root.right);
    }
 //后序遍历
    private void binaryTreePostOrder(TreeNode root) {
        if(root == null){
            return;
        }
        binaryTreePostOrder(root.left);
        binaryTreePostOrder(root.right);
        System.out.println(root.value+" ");
    }
    //根据字符串创建二叉树 ABC###DFR#DF
    public int i = 0;
    private TreeNode createTextTree(String s){
        TreeNode root = null;
        if(s.charAt(i) != '#'){
            root = new TreeNode(s.charAt(i));
            i++;
            root.left =  createTextTree(s);
            root.right =  createTextTree(s);
        }else{
            i++;
        }
        return root;
    }
    //结点个数
    private int getSide(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return getSide(root.left) + 1 + getSide(root.right);
    }
    //叶子节点个数
    private int getLeafSide(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if(root.left == null&&root.right ==null){
            return 1;
        }
        return getSide(root.left)  + getSide(root.right);
    }
//第K层节点个数
    private int getKLevelSize(TreeNode root, int k){
        if(root == null){
            return 0;
        }else if(k == 1){
            return 1;
        }
        return getKLevelSize(root.left,k-1)+getKLevelSize(root.right,k-1);
    }
    //在根左右中找value
    private TreeNode find(TreeNode root, int value){
        if(root == null){
            return null;
        }
        if(root.value == value){
            return root;
        }
        TreeNode result = find(root.left,value);
        if(result != null){
            return result;
        }
         result = find(root.right,value);
        if(result != null){
            return result;
        }
        return null;
    }
    //树的高度
    private int height(TreeNode root){
        if(root == null){
            return 0;
        }
        // not good
//       int left = height(root.left)+1;
//        int right = height(root.right)+1;
//        return left>right?left:right;
        int left = height(root.left);
        int right = height(root.right);
        return left>right?left+1:right+1;
    }
    //前序遍历非递归
    void binaryTreePrevOrderR(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        while(cur != null||!stack.empty()){
            while(cur != null){
                stack.push(cur);
                System.out.println(stack.peek().value+" ");
                cur = cur.left;
            }
            top = stack.pop();
            cur = top.right;
        }
        System.out.println();
    }
    //中序遍历非递归
    void binaryTreeInOrderNonR(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        TreeNode top = null;
        while(cur != null||!stack.empty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            top = stack.pop();
            System.out.println(stack.peek().value+" ");
            cur = top.right;
        }
        System.out.println();
    }
    //后续遍历非递归
    void binaryTreePostOrderNonR(TreeNode root){
        Stack<TreeNode> stack = new Stack<>();
        TreeNode cur = root;
        //记录被打印右子树
        //若没有这个会陷入死循环,在左子树里出不来
        TreeNode prev = null;
        while(cur != null||!stack.empty()){
            while(cur != null){
                stack.push(cur);
                cur = cur.left;
            }
            cur =  stack.peek();
            //此处cur.right == prev 意即判断cur.right是否被打印过
            if(cur.right == null||cur.right == prev){
                stack.pop();
                System.out.println(cur.value+" ");
                //打印了谁prev就指向谁
                prev =  cur;
                cur = null;
            }
            else{
                cur = cur.right;
            }
        }
        System.out.println();
    }
    //二叉树的层序遍历
    //利用了队列
    void binaryTreeLevelOrder(TreeNode root){
        Queue<TreeNode> queue = new LinkedList<>();
        if(root != null){
            queue.offer(root);
        }
        while(!queue.isEmpty()){
           TreeNode cur =  queue.poll();
            System.out.println(cur.value+" ");
            if(cur.left != null){
                queue.offer(cur.left);
            }
            if(cur.right != null){
                queue.offer(cur.right);
            }
        }
    }
    //是否为完全二叉树
    int binaryTreeComplete(TreeNode root){
        Queue<TreeNode> queue  = new LinkedList<>();
        if(root != null){
            queue.offer(root);
            //LinkedList的offer ,add方法的区别
        }
        while(!queue.isEmpty()){
        TreeNode per = queue.poll();
            System.out.println(per.value+" ");
           if(per.left!=null){
               queue.offer(per.left);
           }
           if(per.right != null){
               queue.offer(per.right);
           }
        }
        return 0;
    }

    //根据二叉树创建字符串LeetCode
    public static void tree2strChild(TreeNode t,StringBuilder sb){
        if(t == null){
            return;
        }
        sb.append(t.value);
        if(t.left != null){
            sb.append("(");
            tree2strChild(t.left,sb);
            sb.append(")");
       }
       //左为空,右不为空不可省略()
        if(t.left == null&&t.right!= null){
            sb.append("()");
            }
        if(t.right == null){
            return;
        }else{
            sb.append("(");
            tree2strChild(t.right,sb);
            sb.append(")");
        }
    }

    public static void main(String[] args) {
        TextBinaryTree textBinaryTree = new TextBinaryTree();
       TreeNode treeNode =  textBinaryTree.createTextTree("124###3##");
       StringBuilder sb = new StringBuilder();
        tree2strChild(treeNode,sb);
        System.out.println(sb.toString());
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值