2

我正在尝试在 java 中创建树链表并按度量空间中的球级别打印树,但我没有成功。

创建班级球:

 public class Ball {

    private double Point;
    private double Radius;

    public Ball(double Point, double Radius) {
        this.Point = Point;
        this.Radius = Radius;
    }

    public double getPoint() {
        return Point;
    }

    public double getRadius() {
        return Radius;
    }

    public void setPoint(double p)
    {
        this.Point=p;
    }

    public void setRadius(double r)
    {
        this.Radius=r;
    }

    public String toString(Ball b)
    {
        return b.Point+ "  " + b.Radius;
    }
}

并创建类 TreeNode

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

public class TreeNode<T> {

    T data;
    TreeNode<T> parent;
    LinkedList<TreeNode<T>> children;
    TreeNode next;

    public TreeNode(T data ) {
        this.data = data;
        this.children = new LinkedList<TreeNode<T>>();
        this.parent = null;
    }

    public TreeNode(T data , TreeNode<T> parent)
    {
        this.data=data;
        this.parent=parent;
        this.children = new LinkedList<TreeNode<T>>();
    }

    public TreeNode<T> addChild(T child)
    {

        TreeNode<T> childNode = new TreeNode<T>(child);
        childNode.parent = this;
        this.children.add(childNode);
        return childNode;
    }

    public void setNext(TreeNode e)
    {
        this.next=e;
    }


    public TreeNode getNext()
    {
        return this.next;
    }


    public TreeNode <T> Insert(TreeNode<T> pos, T x)
    {
        TreeNode <T> tmp = new TreeNode <T>(x);
        if(pos == null)
        {
            tmp.setNext(this.parent);   
            this.parent= tmp;
        }
        else{
            tmp.setNext(pos.getNext());
        }
        return tmp;

    }

    public TreeNode<T> getParent() {
        return parent;
    }

    public T getData() {
        return data;
    }

    public LinkedList<TreeNode<T>> getChild ()
    {
        return children;
    }

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

    public void setParent(TreeNode<T> getParent)
    {
        this.parent=parent;
    }
    public String toString()
    {

        return this.data.toString() ;
    }   
}

另外创建类覆盖树级别我的问题是将元素插入树

public class CoverTreeLevels {

    static final int LEVELS = 25;
    public static  Data d;

    public static void Insert(TreeNode node, TreeNode newNode)
    {
        newNode.parent=node.parent;
        node.parent=newNode.parent;
    }

    public static void buildTree(double [][] data)
    {
        double rootRadius =d.Find_Max_Radiues(d.data);
        Ball rootBall = new Ball(data[0][0], rootRadius);
        TreeNode root = new TreeNode<Ball>(rootBall);

        TreeNode last = root;

        for (double i = 1, lastRadius = rootRadius / 2; i < LEVELS - 1; i++, lastRadius /= 2) {
            Ball ball = new Ball( data[0][0] , lastRadius);
            last = last.addChild(ball);         

            for (int j = 1; j < data.length; j++) {
                TreeNode<Ball> n =  last;
                while (true)
                {               
                    if(d.dist(j, 0)> lastRadius)
                    {
                        Ball newBall = new Ball(data[j][0], lastRadius);
                        n.addChild(newBall) ;
                    }

                    n.getParent();  
                }
            }
        }
    }

和数据类,其中包括度量空间中的数据。

我是否朝着正确的方向前进?

4

1 回答 1

0

一方面,您在此代码中的访问修饰符似乎存在一些问题。

在这里,您在不使用您指定的 getter/setter 的情况下引用这些对象的私有属性。这将是一个编译器错误。

其次,您的 insert 方法似乎将 node.parent 设置为自身。

public static void Insert(TreeNode node, TreeNode newNode)
{
    newNode.parent=node.parent;   // new node's parent is being set to current node's parent.
    node.parent=newNode.parent;   // current node's parent being set to newNode's parent, which you just assigned to node.parent above
}

要正确插入,您需要将 newNode 本身插入等式中:

public static void Insert(TreeNode node, TreeNode newNode)
{
    if ( node != null && newNode != null) {
        TreeNode temp;
        temp = node.getParent(); // get the current node's parent
        node.setParent(newNode); // set the new parent of current node to newNode
        newNode.setParent(temp); // set the new node's parent to current node's old parent   
    }
}

现在你应该有一个正常工作的插入。

于 2013-11-19T21:01:05.413 回答