【问题标题】:Interval search in Binary Tree二叉树中的区间搜索
【发布时间】:2020-04-30 01:01:41
【问题描述】:

谁能帮我在二叉树中进行区间搜索。 我知道如何检查树的左侧,但我在小树的右侧遇到了麻烦。 这是我现在的代码。

private boolean search(BSTNode r, int from,int till){

             boolean found = false;
             int arr[];
             arr=new int[10];
             int i=0;
             while (r != null)

             {
                int rval = r.getData();
                if (from < rval && till >rval) {
                     r = r.getLeft();
                     arr[i]=rval;
                     i++;
                }else 
                    r=r.getRight(); 
             }
             return found;
         } 

这是完整的 BSTNode 类。 From 和 until 它是区间的范围(从

     class BSTNode
     {
         BSTNode left, right;
         int data;
         /* Constructor */
         public BSTNode()
         {
             left = null;

             right = null;

             data = 0;
         }
         /* Constructor */
         public BSTNode(int n)
         {
             left = null;

             right = null;

             data = n;
         }
         /* Function to set left node */
         public void setLeft(BSTNode n)
         {
             left = n;
         }
         /* Function to set right node */ 
         public void setRight(BSTNode n)
         {
             right = n;
         }
         /* Function to get left node */

         public BSTNode getLeft()
         {
             return left;
         }
         /* Function to get right node */

         public BSTNode getRight()
         {
             return right;
         }

【问题讨论】:

  • 请输入BSTNode 的代码以及从哪里到哪里?
  • @am0awad 我已经更新了代码。 Soo from 和 until 基本上是由 user(sc.nextInt) 导入的整数
  • 还有样本输入,以跟踪

标签: java tree binary


【解决方案1】:

您可以像这样使用queue 进行搜索:

        public boolean search(Integer from, Integer till) {
            return search(root, from, till);
        }

        private boolean search(BSTNode root, Integer from, Integer till) {
            boolean found = false;
            Queue<BSTNode> queue = new ArrayDeque<>();
            queue.add(root);
            List<Integer> list = new ArrayList<>();
            while (!queue.isEmpty()) {
                BSTNode node = queue.poll();
                int data = node.getData();
                if (from < data && till > data) {
                    found = true;
                    list.add(data);
                }
                if (node.getLeft() != null)
                    queue.add(node.getLeft());
                if (node.getRight() != null)
                    queue.add(node.getRight());
            }
            System.out.println(list);
            return found;
        }

,完整代码


    public static void main(String[] args) {
        BinarySearchTree tree = new BinarySearchTree();

        // Tree building...
        tree.insert(50);
        tree.insert(40);
        tree.insert(20);
        tree.insert(10);
        tree.insert(50);

        System.out.println(tree.search(10, 30));
    }

    static class BinarySearchTree {

        private BSTNode root;

        public void insert(Integer item) {
            root = insert(root, item);
        }

        private BSTNode insert(BSTNode node, Integer item) {
            if (node == null) {
                return new BSTNode(item);
            } else if (item.compareTo(node.data) == 0) {
                return node;
            } else if (item.compareTo(node.data) < 0) {
                node.setRight(insert(node.r, item));
                return node;
            } else {
                node.setLeft(insert(node.l, item));
                return node;
            }
        }

        public Integer find(Integer target) {
            return find(root, target);
        }

        private Integer find(BSTNode node, Integer target) {
            if (node == null) {
                return null;
            }
            Integer cmd = target.compareTo(node.data);
            if (cmd == 0) {
                return node.data;
            } else if (cmd < 0) {
                return find(node.getRight(), target);
            } else {
                return find(node.getLeft(), target);
            }
        }

        public boolean search(Integer from, Integer till) {
            return search(root, from, till);
        }

        private boolean search(BSTNode root, Integer from, Integer till) {
            boolean found = false;
            Queue<BSTNode> queue = new ArrayDeque<>();
            queue.add(root);
            List<Integer> list = new ArrayList<>();
            while (!queue.isEmpty()) {
                BSTNode node = queue.poll();
                int data = node.getData();
                if (from < data && till > data) {
                    found = true;
                    list.add(data);
                }
                if (node.getLeft() != null)
                    queue.add(node.getLeft());
                if (node.getRight() != null)
                    queue.add(node.getRight());
            }
            System.out.println(list);
            return found;
        }
    }

    static class BSTNode {
        Integer data;
        BSTNode l = null;
        BSTNode r = null;

        public BSTNode(Integer data) {
            super();
            this.data = data;
        }

        public Integer getData() {
            return data;
        }

        public void setData(int data) {
            this.data = data;
        }

        public BSTNode getLeft() {
            return l;
        }

        public void setLeft(BSTNode l) {
            this.l = l;
        }

        public BSTNode getRight() {
            return r;
        }

        public void setRight(BSTNode r) {
            this.r = r;
        }

        @Override
        public String toString() {
            return "BSTNode [data=" + data + ", l=" + l + ", r=" + r + "]";
        }
    }

,输出

[20]
true

【讨论】:

  • 谢谢,但我必须将我的节点与 2 个参数(从和直到)进行比较例如我在树中有这样的数据:50;40;20;10;50 我必须从插入和直到值(通过使用scan.nenxtInt)。例如,我们有10(从)和30(直到)。现在我应该从10到30(从