首页 > 其他 > 详细

二叉树的中序遍历的思想

时间:2021-01-18 11:24:45      阅读:40      评论:0      收藏:0      [点我收藏+]

中序遍历就是将一个二叉树的所有结点按照顺序打印出来(假定这个数的构造是左节点小于父节点,右节点大于父节点)

private void print(Node x) {
   if (x == null) return;
   print(x.left);
   StdOut.println(x.key);
   print(x.right);
}

 

二叉树查找指定范围的操作

    public Iterable<Key> keys() {
        if (isEmpty()) return new Queue<Key>();
        return keys(min(), max());
    }    
    public Iterable<Key> keys(Key lo, Key hi) {
        Queue<Key> queue = new Queue<Key>();
        keys(root, queue, lo, hi);
        return queue;
    } 

    private void keys(Node x, Queue<Key> queue, Key lo, Key hi) { 
        if (x == null) return; 
        int cmplo = lo.compareTo(x.key); 
        int cmphi = hi.compareTo(x.key); 
        if (cmplo < 0) keys(x.left, queue, lo, hi); 
        if (cmplo <= 0 && cmphi >= 0) queue.enqueue(x.key); 
        if (cmphi > 0) keys(x.right, queue, lo, hi); 
    } 

 

二叉树的中序遍历的思想

原文:https://www.cnblogs.com/hequnwang/p/14291482.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!