对具有二维属性

时间:2015-05-19 15:23:53

标签: performance algorithm sorting binary-tree nodes

这是我的Node类

public class Node
{
    int Data= -1;
    int X;
    int Y;
    Node LeftChild = null;
    Node RightChild = null;
    Node(int i)
    {
        this.Data = i;
    }
}

这是我的Inorder遍历代码:

public static void inorder(Node current, int horiz_dist, int verti_dist)
{
    if(current == null)
        return;
    inorder(current.LeftChild, ++horiz_dist, ++verti_dist);
    horiz_dist--;
    verti_dist--;
    System.out.println("Node "+current.Data+": X = "+horiz_dist+", Y = "+verti_dist);
    current.X = horiz_dist;
    current.Y = verti_dist;
    node_list.add(current);
    inorder(current.RightChild, --horiz_dist, ++verti_dist);
    horiz_dist++;
    verti_dist--;
}

我有一个节点列表,我通过在Inorder遍历中迭代二叉树来获得。以下是遍历的输出:

  

节点18:X = 3,Y = 3

     

节点7:X = 2,Y = 2

     

节点5:X = 1,Y = 1

     

节点12:X = 1,Y = 3

     

节点9:X = 0,Y = 2

     

节点13:X = -1,Y = 3

     

节点6:X = 0,Y = 0

     

节点8:X = -1,Y = 1

     

节点10:X = -2,Y = 2

     

节点15:X = -3,Y = 3

我想首先根据X(减少顺序)然后Y(增加顺序)对所有节点进行排序。其中XY分别与根节点的距离。所以最终输出将是:

  

节点18:X = 3,Y = 3

     

节点7:X = 2,Y = 2

     

节点5:X = 1,Y = 1

     

节点12:X = 1,Y = 3

     

节点6:X = 0,Y = 0

     

节点9:X = 0,Y = 2

     

节点8:X = -1,Y = 1

     

节点13:X = -1,Y = 3

     

节点10:X = -2,Y = 2

     

节点15:X = -3,Y = 3

编辑:这是我的比较器逻辑。我更新了它。现在它起作用了

This is comparator logic I wrote:

`Collections.sort(node_list,new Comparator(){

        public int compare(Node second, Node first)

        {

            if(first.X > second.X)
                return 1;
            else if(first.X < second.X)
                return -1;
            else if(first.X == second.X)
            {
                if(first.Y < second.Y)
                    return 1;
                else if(first.Y > second.Y)
                    return -1;
                else if( first.Y == second.Y)
                    return 0;
            }
            return 0;
        }
    });`

2 个答案:

答案 0 :(得分:0)

您可以将std :: sort与自定义比较功能一起使用。

假设您有一个节点std::vector<Node> vec的向量,那么您可以像这样使用它

std::sort(vec.begin(), vec.end(), compare);

其中compare定义如下

bool compare(Node a, Node b)
{
    if (a.X > a.X) return true;
    if (a.Y > a.Y) return true;
}

此算法的运行时间为O(n*log(n))

答案 1 :(得分:0)

Phew,让我再试一次,因为我最初误解了这个问题。由于您的整个目标是通过树遍历过程分配这些X / Y值,首先是坏消息:

递归算法不在这里工作。

无论您是使用有序还是预订或后序遍历,所有这些算法都会分崩离析,因为他们希望以类似堆栈的LIFO方式(递归)递归地向下挖掘叶子像LIFO结构一样使用调用堆栈。如果你想以一种先排序Y然后排序X的方式输出它们会很好,但不是相反。首先做X是相当棘手的。

伪代码解决方案:

List<Node> current_row;
List<Node> next_row;

root_node.x = 0;
root_node.y = 0;
current_row.push(root_node);

// Right-to-Left, Top-to-Bottom
while (!current_row.empty())
{
    do
    {
        Node node = current_row.pop();
        // output node data

        if (node.right_child)
        {
            node.right_child.x = node.x + 1;
            node.right_child.y = node.y + 1;
            next_row.push(node.right_child);
        }

        if (node.left_child)
        {
            node.left_child.x = node.x - 1;
            node.left_child.y = node.y + 1;
            next_row.push(node.left_child);
        }
    } while (!current_row.empty());
    current_row.swap(next_row);
}

你也可以在这里使用一个队列,但我认为这是基于行的&#39;心态可能会帮助你初步了解发生了什么。

相关问题