尝试实现2D BST,但我的add and contains方法无效

时间:2017-04-23 19:11:35

标签: java binary-search-tree

我正在尝试以2D树的形式实现kd树,但是,我的实现失败了。基本上,我正在做的是在树的每个级别,根据其x或y坐标添加项目。添加根节点后,将添加的下一个节点与根节点x坐标进行比较,如果较小,则将其放置在根节点的左侧,如果更大,则放在根节点的右侧。然后,第二个节点iist之后的下一个节点再次基于x坐标添加,就像在BST中一样,每个父节点最多只能添加2个节点。将基于y坐标比较第三节点。

我已经取得了一些进步,以获得我的添加和搜索(包含方法工作),但无济于事。我已经尝试进行条件测试来检查添加的节点是否应该放在根节点或其他节点的左侧(如果它们的x-coord更小)我的add方法似乎不起作用。到目前为止,这是我的整个计划。

import java.util.*;

public class TwoDTree {
    /*************
     * attributes
     ************/

    TwoDTreeNode root;

    /***************
     * constructor
     **************/
    TwoDTree() {
        root = null;

    }

    /**********
     * methods
     *********/
    /**
     * To Do: adds a new node with the given x and y coordinates to the TwoDTree
     * 
     * @param x
     * @param y
     */
    public void add(int x, int y) {

        TwoDTreeNode currentNode = root;
        TwoDTreeNode previousNode = null;
        TwoDTreeNode newNode = new TwoDTreeNode(x,y);
        boolean useXCoordinate = false;
        boolean useLeftSubTree = false;

        while (currentNode != null) {
            useLeftSubTree = useXCoordinate ? (newNode.xCoordinate < currentNode.xCoordinate) : (newNode.yCoordinate < currentNode.yCoordinate);
            useXCoordinate = !useXCoordinate;
        }

        if (root == null) {
            root = new TwoDTreeNode(x, y);
        } else if (useLeftSubTree) {
            previousNode.left = new TwoDTreeNode(x,y);
        } else {
            previousNode.right =new TwoDTreeNode(x,y);
        }
    }


    /**
     * 
     * @param x
     * @param y
     * @returns true if a node with the given x and y coordinates exist in the
     *          tree.
     */
    public boolean contains(int x, int y) {

        TwoDTreeNode currentNode = root;
        TwoDTreeNode newNode = new TwoDTreeNode(x,y);
        boolean useXCoordinate = false;
        boolean useLeftSubTree = false;

        while (currentNode != null) {
            useLeftSubTree = useXCoordinate ? (newNode.xCoordinate < currentNode.xCoordinate) : (newNode.yCoordinate < currentNode.yCoordinate);
            useXCoordinate = !useXCoordinate;

            if ((currentNode.xCoordinate == newNode.xCoordinate) && (currentNode.yCoordinate == newNode.yCoordinate)) {
                return true;
            }
        }


        return false;

    }

    /**
     * A method which prints a level order traversal of the tree
     */
    public void levelOrderPrint() {
        Queue<TwoDTreeNode> queue = new LinkedList<TwoDTreeNode>();
        queue.add(root);
        while (!queue.isEmpty()) {
            TwoDTreeNode node = queue.poll();
            System.out.print("(" + node.xCoordinate + "," + node.yCoordinate + ")");
            if (node.left != null)
                queue.add(node.left);
            if (node.right != null)
                queue.add(node.right);
        }
        System.out.println();
    }

    public static void main (String[] args) {
        System.out.println("Building a tree of nodes: (30,40) (5,25) (10,12) (70,70) (50,30) (35,40)");
        TwoDTree tDTree = new TwoDTree();
        tDTree.add(30, 40);
        tDTree.add(5, 25);
        tDTree.add(10, 12);
        tDTree.add(70, 70);
        tDTree.add(50, 30);
        tDTree.add(35, 40);
        System.out.println("Level order traversal for this tree is:");
        tDTree.levelOrderPrint();
        System.out.println("contains(5,25) returned: " + tDTree.contains(5, 25));
        System.out.println("contains (10,13) returned: " + tDTree.contains(10, 13));
        System.out.println("contains (35,45) returned: " + tDTree.contains(35, 45));
    }


    private static class TwoDTreeNode {
        /*************
         * attributes
         ************/
        int xCoordinate;
        int yCoordinate;
        TwoDTreeNode right;
        TwoDTreeNode left;

        /***************
         * constructors
         **************/
        TwoDTreeNode(int x, int y) {
            xCoordinate = x;
            yCoordinate = y;
        }

        TwoDTreeNode(int x, int y, TwoDTreeNode leftChild, TwoDTreeNode rightChild) {
            xCoordinate = x;
            yCoordinate = y;
            left = leftChild;
            right = rightChild;
        }
    }

}

0 个答案:

没有答案