【问题标题】:Getting nth item of a BST获取 BST 的第 n 项
【发布时间】:2017-02-14 14:50:50
【问题描述】:

我正在尝试返回 BST 的第 n 项保存的数据,我正在尝试使用计数器进行中序遍历,当计数器大于 n 时,返回当前节点。我当前的代码似乎总是返回第一项,我看不出我的逻辑哪里错了。我只写了 nth 和 inOrder 方法,其余的都提供了。我想我经常增加我的计数器,是原因还是我做错了什么。我也会在下面发布我正在测试的主要方法。

import java.util.NoSuchElementException;

public class BST {
    private BTNode<Integer> root;

    public BST() {
        root = null;
    }


    public boolean insert(Integer i) {
        BTNode<Integer> parent = root, child = root;
        boolean goneLeft = false;

        while (child != null && i.compareTo(child.data) != 0) {
            parent = child;
            if (i.compareTo(child.data) < 0) {
                child = child.left;
                goneLeft = true;
            } else {
                child = child.right;
                goneLeft = false;
            }
        }

        if (child != null)
            return false;  // number already present
        else {
            BTNode<Integer> leaf = new BTNode<Integer>(i);
            if (parent == null) // tree was empty
                root = leaf;
            else if (goneLeft)
                parent.left = leaf;
            else
                parent.right = leaf;
            return true;
        }
    }

    public int greater(int n) {
        if (root == null) {
            return 0;
        }
        else {
            return n;
        }
    }

    int c = 0;
    public int nth(int n) throws NoSuchElementException {
        BTNode<Integer> node = null;
        if (root == null) {
            throw new NoSuchElementException("Element " + n + " not found in tree");
        }
        else {
            if (root != null){
                node = inOrder(root, n);
            }
        }
        return node.data;
    }

    public BTNode inOrder(BTNode<Integer> node, int n) {
        c++;
        while (c <= n) {
            if (node.left != null) {
                inOrder(node.left, n);
            }
            c++;
            if (node.right != null) {
                inOrder(node.right, n);
            }
        }
        return node;
    }
}

class BTNode<T> {
    T data;
    BTNode<T> left, right;

    BTNode(T o) {
        data = o;
        left = right = null;
    }
}


public class bstTest {
    public static void main(String[] args) {
        BST tree = new BST();
        tree.insert(2);
        tree.insert(5);
        tree.insert(7);
        tree.insert(4);
        System.out.println(tree.nth(2));
    }
}

【问题讨论】:

  • 每次调用方法时都会修改字段,所以是的。增加过于频繁
  • 我不想增加,直到我在最左边的项目,对吧?然后每次调用该方法时我都想增加?所以我可以添加如果 node.left 为 null 然后增加 c。我去那里的路对吗?

标签: java binary-search-tree


【解决方案1】:

您应该考虑的一个不变量是,当 n = sizeOfLeftSubtree + 1 时,返回该节点。如果 n 小于,则向左走。如果 n 更大,则向右移动并将 n 减少 sizeOfLeftSubtree+1。请注意,我将 n=1 映射到第一个元素(最左边的元素)。

您可以简单地递归计算子树的大小,或者您可以将大小存储在每个根(每个节点都是子树的根)修改您的插入方法(将所有访问的节点保存在堆栈/队列中,如果添加新节点只需将所有大小增加 1)。

如果存储大小,复杂度将为 O(log n)。如果不是,如果可能变成 O(n^2)。

public int nth(int n) throws NoSuchElementException {
if( sizeOfTree(this.root) < n || n < 1)
    throw new NoSuchElementException("Element " + n + " not found in tree");

BTNode<Integer> root = this.root;
boolean found = false;
do{
    int sizeOfLeftSubtree = sizeOfTree(root.left);
    if( sizeOfLeftSubtree + 1 == n ){
    found = true;
    }else if( n < sizeOfLeftSubtree+1 ){
    root = root.left;
    }else if( sizeOfLeftSubtree+1 < n ){
    root = root.right;
    n -= sizeOfLeftSubtree+1;
    }
}while( !found );

return root.data;
}

public int sizeOfTree(BTNode<Integer> root){
if( root == null )
    return 0;
else
    return sizeOfTree(root.left) + 1 + sizeOfTree(root.right);
}

【讨论】:

  • 谢谢,这比我尝试的方法更有效率。
【解决方案2】:

您不会在inOrder 方法中更改node

public BTNode inOrder(BTNode<Integer> node, int n) {
    c++;
    while (c <= n) {
        if (node.left != null) {
            // **** Add this - or something.
            node = inOrder(node.left, n);
        }
        c++;
        if (node.right != null) {
            // **** Add this - or something.
            node = inOrder(node.right, n);
        }
    }
    return node;
}

并不是说这是您要修复的错误,但肯定是代码有问题。

【讨论】:

  • 这样效果更好,但是如果我将 n 传递为 1,它会返回根而不是最左边的项。我需要在其他地方增加 c...
猜你喜欢
  • 1970-01-01
  • 2011-01-01
  • 1970-01-01
  • 2017-11-20
  • 1970-01-01
  • 2015-09-22
  • 2015-11-01
  • 1970-01-01
相关资源
最近更新 更多