二叉树基本操作

来源:互联网 发布:电视软件看电影免费 编辑:程序博客网 时间:2024/04/29 06:16
package java_study.JianZhiOffer;/** * Created by ethan on 2015/6/21. */public class TreeNode {    char value;    TreeNode lchild;    TreeNode rchild;    public TreeNode() {        this.value = 0;        this.lchild = null;        this.rchild = null;    }    public TreeNode(char value){        this.value = value;        this.lchild = null;        this.rchild = null;    }    public TreeNode(char value, TreeNode lchild, TreeNode rchild){        this.value = value;        this.lchild = lchild;        this.rchild = rchild;    }    public char getValue() {        return value;    }    public void setValue(char value) {        this.value = value;    }    public TreeNode getLchild() {        return lchild;    }    public void setLchild(TreeNode lchild) {        this.lchild = lchild;    }    public TreeNode getRchild() {        return rchild;    }    public void setRchild(TreeNode rchild) {        this.rchild = rchild;    }}package java_study.JianZhiOffer;import java.util.LinkedList;import java.util.Queue;import java.util.Stack;/** * Created by ethan on 2015/6/21. */public class TreeUtil {    // 接收前序和中序创建二叉树    public static TreeNode buildTree(String pre_str, int pre_start, int pre_end, String in_str, int in_start, int in_end){        if (pre_str==null || in_str==null) return null;        if (pre_start>pre_end || in_start>in_end) return null;        TreeNode root = new TreeNode(pre_str.charAt(pre_start));        int index_mid = in_start;        for (; index_mid<in_end; index_mid++)            if (in_str.charAt(index_mid) == pre_str.charAt(pre_start)) break;        if (index_mid > in_start)            root.setLchild(buildTree(pre_str, pre_start+1, pre_start+index_mid-in_start, in_str, in_start, index_mid-1));        if (index_mid < in_end)            root.setRchild(buildTree(pre_str, pre_start+index_mid-in_start+1, pre_end, in_str, index_mid+1, in_end));        return  root;    }        // 前序递归遍历    public static void preOrder(TreeNode root){        if (root != null){            System.out.print(root.getValue() + " ");            preOrder(root.getLchild());            preOrder(root.getRchild());        }    }    // 中序递归遍历    public static void inOrder(TreeNode root){        if (root != null ){            inOrder(root.getLchild());            System.out.print(root.getValue() + " ");            inOrder(root.getRchild());        }    }    // 后序递归遍历    public static void postOrder(TreeNode root){        if (root!=null){            postOrder(root.getLchild());            postOrder(root.getRchild());            System.out.print(root.getValue() + " ");        }    }    // 前序非递归遍历, 全压栈    public static void preOrderNoRecursive_1(TreeNode root){        if (root == null) return;        Stack<TreeNode> stack = new Stack<TreeNode>();        stack.push(root);        while (!stack.isEmpty()){            TreeNode tmp = stack.pop();            System.out.print(tmp.getValue() + " ");            if (tmp.getRchild()!=null){                stack.push(tmp.getRchild());            }            if (tmp.getLchild()!=null){                stack.push(tmp.getLchild());            }        }        System.out.println();    }    // 前序非递归遍历, 全压栈    public static void preOrderNoRecursive_2(TreeNode root){        if (root==null) return;        Stack<TreeNode> stack = new Stack<TreeNode>();        TreeNode tmp = root;        while (tmp!=null || !stack.isEmpty()){            while (tmp!=null){                System.out.print(tmp.value+" ");                stack.push(tmp);                tmp=tmp.getLchild();            }            if (!stack.isEmpty()){                tmp = stack.pop().getRchild();            }        }        System.out.println();    }    // 前序非递归遍历    public static void preOrderNoRecursive(TreeNode root){        if (root==null) return;        Stack<TreeNode> stack = new Stack<TreeNode>();        TreeNode tmp = root;        while (tmp!=null){            System.out.print(tmp.value+" ");            stack.push(tmp);            tmp=tmp.getLchild();        }        while (!stack.isEmpty()){            TreeNode top = stack.pop();            TreeNode tmp_1 = top.getRchild();            while (tmp_1!=null){                System.out.print(tmp_1.value+" ");                stack.push(tmp_1);                tmp_1=tmp_1.getLchild();            }        }        System.out.println();    }    // 中序非递归遍历    public static void inOrderNoRecursive_1(TreeNode root){        if (root==null) return;        Stack<TreeNode> stack = new Stack<TreeNode>();        TreeNode tmp = root;        while (tmp!=null || !stack.isEmpty()){            while (tmp!=null){                stack.push(tmp);                tmp = tmp.getLchild();            }            if (!stack.isEmpty()){                TreeNode myNode = stack.pop();                System.out.print(myNode.getValue()+" ");                tmp = myNode.getRchild();            }        }        System.out.println();    }    // 中序非递归遍历    public static void inOrderNoRecursive(TreeNode root){        if (root==null) return;        Stack<TreeNode> stack = new Stack<TreeNode>();        TreeNode tmp = root;        while (tmp!=null){            stack.push(tmp);            tmp = tmp.getLchild();        }        while (!stack.isEmpty()){            TreeNode top = stack.pop();            System.out.print(top.value + " ");            TreeNode tmp_1 = top.getRchild();            while (tmp_1!=null){                stack.push(tmp_1);                tmp_1=tmp_1.getLchild();            }        }        System.out.println();    }    // 后序非递归遍历    public static void postOrderNoRecursive(TreeNode root){        if (root==null) return;        TreeNode tmp = root;        TreeNode preVisited = null;        Stack<TreeNode> stack = new Stack<TreeNode>();        while (tmp!=null || !stack.isEmpty()){            while (tmp!=null){                stack.push(tmp);                tmp = tmp.getLchild();            }            tmp = stack.peek();            if (tmp.getRchild()==null || tmp.getRchild()==preVisited){                System.out.print(tmp.getValue()+ " ");                preVisited = tmp;                stack.pop();                tmp=null;            }else{                tmp = tmp.getRchild();            }        }        System.out.println();    }    // 层次遍历 利用队列    public static void levelTraverse(TreeNode root){        if (root == null) return;        Queue<TreeNode> stack = new LinkedList<TreeNode>();        stack.add(root);        while (!stack.isEmpty()){            TreeNode cur = stack.remove();            System.out.print(cur.getValue() + " ");            if (cur.getLchild()!=null)                stack.add(cur.getLchild());            if (cur.getRchild()!=null){                stack.add(cur.getRchild());            }        }        System.out.println();    }    // 二叉树的高度    public static int getDepth(TreeNode root){        if (root==null) return 0;        int depth_l = getDepth(root.getLchild());        int depth_r = getDepth(root.getRchild());        return (depth_l > depth_r ? depth_l : depth_r)+1;    }    // 二叉树的结点的个数    public static int countNodes(TreeNode root){        if (root==null) return 0;        return 1+countNodes(root.getLchild()) + countNodes(root.getRchild());    }}package java_study.JianZhiOffer;import org.junit.Test;/** * Created by ethan on 2015/6/21. */public class TestTreeUtil {    String pre_str = "abdhecfg";    String in_str = "hdbeafcg";    public TreeNode root = TreeUtil.buildTree(pre_str, 0, pre_str.length()-1, in_str, 0, in_str.length()-1);    // 手动初始化//    public TreeNode init(){//        TreeNode node0 = new TreeNode('a');//        TreeNode node1 = new TreeNode('b');//        TreeNode node2 = new TreeNode('c');//        TreeNode node3 = new TreeNode('d');//        TreeNode node4 = new TreeNode('e');//        TreeNode node5 = new TreeNode('f');//        TreeNode node6 = new TreeNode('g');//        TreeNode node7 = new TreeNode('h');//        node0.setLchild(node1);//        node0.setRchild(node2);//        node1.setLchild(node3);//        node1.setRchild(node4);//        node2.setLchild(node5);//        node2.setRchild(node6);//        node3.setLchild(node7);//        return node0;//    }    @Test    public void test3(){        System.out.println(TreeUtil.getDepth(root));        System.out.println(TreeUtil.countNodes(root));    }    @Test    public void test2(){        TreeUtil.preOrderNoRecursive(root);        TreeUtil.preOrderNoRecursive_1(root);        TreeUtil.preOrderNoRecursive_2(root);        TreeUtil.inOrderNoRecursive(root);        TreeUtil.inOrderNoRecursive_1(root);        TreeUtil.postOrderNoRecursive(root);        TreeUtil.levelTraverse(root);    }    @Test    public void test1(){        String pre_str = "abdhecfg";        String in_str = "hdbeafcg";        TreeNode root = TreeUtil.buildTree(pre_str, 0, pre_str.length()-1, in_str, 0, in_str.length()-1);        TreeUtil.postOrder(root);    }    @Test    public void test(){        TreeUtil.preOrder(root);        System.out.println();        TreeUtil.inOrder(root);        System.out.println();        TreeUtil.postOrder(root);        System.out.println();    }}

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 去健身房办卡老板跑了怎么办 买货我已经拒收商家不退款怎么办 在京东买了东西拒收不退款怎么办 罗马仕充电宝进入休眠状态怎么办 广发信用卡寄到家没拿到快递怎么办 包邮商家要买家出物流费怎么办? 美团外卖下单转化率低怎么办 京东退款不小心点了取消退款怎么办 近邻宝开了箱又关了怎么办 近邻宝箱子打开了东西忘记拿怎么办 京东取消订单商家总不取消怎么办 京东快递退货取件一直取不到怎么办 在京东线上付款了但没收到货怎么办 寄快递收件人电话号码写错了怎么办 京东购物实名认证被别人占用怎么办 货已发出单号还没填买家退货怎么办 自提柜还有一个包忘记拿怎么办 当顾客说衣服太贵的时候怎么办 京东快递送错了被别人签收了怎么办 乐视1s玩王者荣耀卡怎么办 京东商城买东西发现地址错了怎么办 京东分期付款买手机额度不够怎么办 亮皮银色高跟鞋时间放久变色怎么办 饿了么商家钱包提现被锁了怎么办 在淘手游买的游戏账号被找回怎么办 交易猫买手游梦幻号被找回了怎么办 绑定着苹果账号的邮箱忘记了怎么办 京东注册账号跟密码都忘记了怎么办 孩子大学一个宿舍宿友不行怎么办 微信手机支付密码忘记了怎么办 微信公众号不能付款了怎么办 头条误点了投放头条广告怎么办 交行网银密码错误6次怎么办 宁波新冮厦关门我们的消费卡怎么办 第一天上班别人教的学不会怎么办 老公出轨已经不想跟你爱爱了怎么办 妻子出轨丈夫为了孩子不离婚怎么办 妻子出轨分居死也不愿意离婚怎么办 拼多多个人卖家登录密码忘了怎么办 微信被限制添加更多订阅号了怎么办 知网复制粘贴的论文有格式怎么办