作者: RalapHao | 来源:发表于2019-06-14 15:10 被阅读0次
  1. 一般二叉树
    普通二叉树,前、中、后序遍历以及搜索
public class BinaryTree {

    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        TreeNode root = new TreeNode(1, "Ralap01");
        TreeNode ralap02 = new TreeNode(2, "Ralap02");
        TreeNode ralap03 = new TreeNode(3, "Ralap03");
        TreeNode ralap04 = new TreeNode(4, "Ralap04");
        TreeNode ralap05 = new TreeNode(5, "Ralap05");
        root.left = ralap02;
        root.right = ralap03;
        ralap03.left = ralap05;
        ralap03.right = ralap04;
        bt.setRoot(root);
        System.out.println("前序");
        bt.preOrder();
        System.out.println("中序");
        bt.infixOrder();
        System.out.println("后续");
        bt.laterOrder();
        System.out.println("==================================================");
        int serach = 5;
        TreeNode treeNode = bt.preSerach(serach);
        System.out.println(treeNode);
        treeNode = bt.infixSerach(serach);
        System.out.println(treeNode);
        treeNode = bt.laterSerach(serach);
        System.out.println(treeNode);
        System.out.println("=====================");
        TreeNode del = bt.del(5);
        System.out.println(del);
        System.out.println("-----------");
        bt.preOrder();

    }


    private TreeNode root;

    public BinaryTree setRoot(TreeNode root) {
        this.root = root;
        return this;
    }

    public void preOrder() {
        if (root != null) {
            root.preOrder();
        }
    }

    public void infixOrder() {
        if (root != null) {
            root.infixOrder();
        }
    }

    public void laterOrder() {
        if (root != null) {
            root.laterOrder();
        }
    }

    public TreeNode preSerach(int no) {
        return root.preSerach(no);
    }

    public TreeNode infixSerach(int no) {
        return root.infixSerach(no);
    }

    public TreeNode laterSerach(int no) {
        return root.laterSerach(no);
    }

public void dfsOrderDG(TreeNode node) {
        if (node == null) {
            return;
        }
        System.out.println(node.no);
        dfsOrderDG(node.left);
        dfsOrderDG(node.right);
    }

    public void dfsOrder() {
        List<TreeNode> nodeList = new ArrayList<>();
        if (root == null) {
            return;
        }
        Stack<TreeNode> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()) {
            TreeNode node = stack.pop();
            if (node.right != null) {
                stack.push(node.right);
            }
            if (node.left != null) {
                stack.push(node.left);
            }
            System.out.println(node.no);
        }
    }

    public void dfsOrder(TreeNode node) {
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(node);
        while (!queue.isEmpty()) {
            TreeNode poll = queue.poll();

            if (poll.left != null) {
                queue.offer(poll.left);
            }

            if (poll.right != null) {
                queue.offer(poll.right);
            }
            System.out.println(poll.val);

        }
    }
    public TreeNode del(int no) {
        TreeNode delNode = null;
        if (root != null) {
            if (root.no != no) {
                delNode = root.del(no);
            } else {
                delNode = root;
                root = null;
            }
        } else {
            System.out.println("数不能为空");
        }
        return delNode;
    }
}

class TreeNode {

    public int no;
    public String name;
    public TreeNode left;
    public TreeNode right;

    public TreeNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("TreeNode{");
        sb.append("no=").append(no);
        sb.append(", name='").append(name).append('\'');
        sb.append('}');
        return sb.toString();
    }

    public void preOrder() {
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    public void laterOrder() {
        if (this.left != null) {
            this.left.laterOrder();
        }
        if (this.right != null) {
            this.right.laterOrder();
        }
        System.out.println(this);
    }

    public TreeNode preSerach(int no) {
        TreeNode resNode = null;
        System.out.println("一一一一一一一一一");
        if (this.no == no) {
            return this;
        }
        if (this.left != null) {
            resNode = this.left.preSerach(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preSerach(no);
        }
        return resNode;
    }

    public TreeNode infixSerach(int no) {
        TreeNode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixSerach(no);
        }

        if (resNode != null) {
            return resNode;
        }
        System.out.println("二二二二二二二二");

        if (this.no == no) {
            return this;
        }
        if (this.right != null) {
            resNode = this.right.infixSerach(no);
        }
        return resNode;
    }

    public TreeNode laterSerach(int no) {
        TreeNode resNode = null;
        if (this.left != null) {
            resNode = this.left.laterSerach(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.laterSerach(no);
        }
        if (resNode != null) {
            return resNode;
        }
        System.out.println("三三三三三三");
        if (this.no == no) {
            return this;
        }
        return resNode;
    }

    TreeNode delNode = null;
    public TreeNode del(int no) {
        if (this.left != null) {
            if (this.left.no == no) {
                delNode = this.left;
                this.left = null;
            } else {
                delNode = this.left.del(no);
            }
        }
        if (delNode != null) {
            return delNode;
        }
        if (this.right != null) {
            if (this.right.no == no) {
                delNode = this.right;
                this.right = null;
            } else {
                delNode = this.right.del(no);
            }
        }
        return delNode;
    }
}
  1. 顺序存储二叉树
    将数组以树的思想标识,包括前、中、后续遍历
public class BinaryOrderTree {

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7};
        BinaryOrderTree bot = new BinaryOrderTree(array);
        bot.preOrder(0);
        System.out.println("-----------------------");
        bot.infixOrder(0);
        System.out.println("-----------------------");
        bot.laterOrder(0);
    }
    
    private int[] array;
    public BinaryOrderTree(int[] array) {
        this.array = array;
    }

    public void preOrder(int index) {
        if (array == null || array.length <= 0) {
            System.out.println("数组为空");
            return;
        }
        System.out.println(array[index]);
        if (index * 2 + 1 < array.length) {
            preOrder(index * 2 + 1);
        }

        if (index * 2 + 2 < array.length) {
            preOrder(index * 2 + 2);
        }
    }

    public void infixOrder(int index) {
        if (array == null || array.length <= 0) {
            System.out.println("数组为空");
            return;
        }
        if (index * 2 + 1 < array.length) {
            infixOrder(index * 2 + 1);
        }
        System.out.println(array[index]);
        if (index * 2 + 2 < array.length) {
            infixOrder(index * 2 + 2);
        }
    }

    public void laterOrder(int index) {
        if (array == null || array.length <= 0) {
            System.out.println("数组为空");
            return;
        }
        if (index * 2 + 1 < array.length) {
            laterOrder(index * 2 + 1);
        }

        if (index * 2 + 2 < array.length) {
            laterOrder(index * 2 + 2);
        }
        System.out.println(array[index]);
    }
}
  1. 线索化二叉树
    将普通二叉树中,左右节点为空的节点,赋值上前驱和后继节点
public class ThreadBinaryOrder {

    public static void main(String[] args) {

        ThreadBinaryOrder tbo = new ThreadBinaryOrder();
        ThreadTreeNode root = new ThreadTreeNode(1, "Ralap01");
        ThreadTreeNode ralap03 = new ThreadTreeNode(3, "Ralap03");
        ThreadTreeNode ralap06 = new ThreadTreeNode(6, "Ralap06");
        root.left = ralap03;
        root.right = ralap06;

        ThreadTreeNode ralap08 = new ThreadTreeNode(8, "ralap08");
        ThreadTreeNode ralap10 = new ThreadTreeNode(10, "ralap10");
        ThreadTreeNode ralap14 = new ThreadTreeNode(14, "ralap14");
        ralap03.left = ralap08;
        ralap03.right = ralap10;
        ralap06.left = ralap14;
        tbo.setRoot(root);

        tbo.thread();
        System.out.println(ralap08.left);
        System.out.println(ralap08.right);
        tbo.preThreadOrder(root);


    }

    private ThreadTreeNode root;
    private ThreadTreeNode pre;

    public ThreadBinaryOrder setRoot(ThreadTreeNode root) {
        this.root = root;
        return this;
    }

    public void thread() {
        thread(root);
    }

    public void thread(ThreadTreeNode node) {
        if (node == null) {
            return;
        }
        thread(node.left);
        if (node.left == null) {
            node.left = pre;
            node.leftType = 1;
        }
        if (pre != null && pre.right == null) {
            pre.right = node;
            pre.rightType = 1;
        }
        pre = node;
        thread(node.right);
    }

    public void preThreadOrder(ThreadTreeNode node) {
        while (node != null) {
            while (node.leftType == 0) {
                node = node.left;
            }
            System.out.println(node);

            while (node.rightType == 1) {
                node = node.right;
                System.out.println(node);
            }
            node = node.right;
        }
    }
}

class ThreadTreeNode {

    public int no;
    public String name;
    public ThreadTreeNode left;
    public ThreadTreeNode right;
    public int leftType;
    public int rightType;

    public ThreadTreeNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("ThreadTreeNode{");
        sb.append("no=").append(no);
        sb.append(", name='").append(name).append('\'');
        sb.append(", leftType=").append(leftType);
        sb.append(", rightType=").append(rightType);
        sb.append('}');
        return sb.toString();
    }
}

相关文章

  • 水彩过程 | 树树树树

    练习了一下树的画法,用毛笔勾树干,扇形笔画树叶还是又快又方便的。因为不是写实风格,只要把树的意象画出来就可以,所以...

  • 树·树

    ​如果有来生,要做一棵树,站成永恒,没有悲欢姿势,一半在尘土里安详。一半在风里飞扬,一半洒落阴凉,一半沐浴阳光。 ...

  • 树,树……

    树,树…… ——洛尔迦 树,树, 枯了又绿。 脸蛋美丽的姑娘 在那里摘橄榄。 风,塔楼上的求爱者, 拦腰把她...

  • 橄榄树树树

    在建班级群的时候,我顺手打了三个树——橄榄树树树。是的,这是橄榄树第三次起航。 第一次,在北京,我说,我愿意在无人...

  • 树,与树

    (第一次学着简书里文友看图写诗,2020的图,各位讲究着看吧) 文/三少爷的糖 一颗树站在山头 遥望着远方,朦胧中...

  • 树,与树

    我不是一个很喜欢女生哭闹的人。 哭闹,意味着理智被情感摧毁。 理智没了,沟通渠道也就关闭了。 没有了沟通,剩下的就...

  • 树和树

    我的家门前有一棵柏树,不是什么稀罕的树,但它却挺直腰杆儿,坚定的伫立在我家门前,望着远方,似乎在等什么人又不像在等...

  • 树树秋声

    余秋雨说:生命,是一树花开,或安静或热烈,或寂寞或璀璨。日子,就在岁月的年轮中渐次厚重,那些天真的、跃动的、抑或沉...

  • 短篇‖树树

    这是一条幽静的古道,两旁尽是残垣断壁,竟也有一些台阶通向几栋还算有顶篷的石质的建筑物。我和我的伙伴着级上了一段...

  • 树树夜夜

    长夜唧唧夏虫前 长街相对两树闲 冠盖接云皆无语 此缘如可问苍天

网友评论

      本文标题:

      本文链接:https://www.haomeiwen.com/subject/ckqpfctx.html