简体   繁体   English

二叉树Sorrt Java递归

[英]Binary Tree Sorrt Java Recursive

i've got some troubles with the Binary Tree Sort in Java. 我在Java中使用Binary Tree Sort遇到了一些麻烦。 I need to create the following method, but i don't get it, how to implement the position (pos) properly. 我需要创建以下方法,但我不知道如何正确实现位置(pos)。 The excercise is to implement the Binary Tree Sort with the given signature. 练习是用给定的签名实现二叉树排序。 We then should initialize an int[] which size has to be at least the size of the Nodes in the tree. 然后,我们应该初始化一个int [],其大小必须至少是树中Node的大小。 The method 方法

int binSort(Node node, int[] a, int pos){...} int binSort(节点node,int [] a,int pos){...}

should place the values of each node/leaf into the array a, at position pos. 应该将每个节点/叶子的值放在位置pos的数组a中。 I am not allowed to use global variables! 我不允许使用全局变量! And as you can see, we need to implement the inOrder traversal 如您所见,我们需要实现inOrder遍历

public class BinaryTree {

Node root;
int elements;

public BinaryTree() {
    this.elements = 0;
}

public void addNode(int key, String name) {
    Node newNode = new Node(key, name);
    if (root == null) {
        root = newNode;
    } else {
        Node focusNode = root;
        Node parent;

        while (true) {
            parent = focusNode;
            if (key < focusNode.getPriority()) {
                focusNode = focusNode.getLeftChild();
                if (focusNode == null) {
                    parent.setLeftChild(newNode);
                    return;
                }
            } else {
                focusNode = focusNode.getRightChild();
                if (focusNode == null) {
                    parent.setRightChild(newNode);
                    return;
                }
            }
        }
    }
}

public int binSort(Node focusNode, int[] a, int pos) {
    int tmp = pos++;
    if (focusNode != null) {
        if (focusNode.getLeftChild() != null) {
            binSort(focusNode.getLeftChild(), a, tmp);
        }

            System.out.println(focusNode.toString() + " - " + tmp++);
        if (focusNode.getRightChild() != null) {
            binSort(focusNode.getRightChild(), a, tmp);
        }
        return focusNode.getPriority();
    }
    return -1;
}

public static void main(String[] args) {
    BinaryTree tree = new BinaryTree();
    tree.addNode(50, "Boss");
    tree.addNode(30, "Boss");
    tree.addNode(10, "Boss");
    tree.addNode(70, "Boss");
    tree.addNode(9, "Boss");
    tree.addNode(15, "Boss");
    tree.addNode(78, "Boss");
    tree.addNode(36, "Boss");

    int[] a = new int[8];
    tree.binSort(tree.root, a, 0);
    System.out.println(tree.root.getPriority());
    System.out.println("");

    System.out.println(Arrays.toString(a));

}

} }

My Output: Boss has a key 9 - 0 我的输出:老板有一把钥匙9-0

Boss has a key 10 - 0 老板有一把钥匙10-0

Boss has a key 15 - 1 老板有一把钥匙15-1

Boss has a key 30 - 0 老板有一把钥匙30-0

Boss has a key 36 - 1 老板有一把钥匙36-1

Boss has a key 50 - 0 老板有一把钥匙50-0

Boss has a key 70 - 1 老板有一把钥匙70-1

Boss has a key 78 - 2 老板有一把钥匙78-2

just ignore "Boss" (it is just a useless value at the moment!) the important part is that the specific values which should be placed into the array are perfectly ordered (9,10,15,30,..,78), but the positions are not! 只需忽略“ Boss”(此刻这只是一个无用的值!),重要的部分是应放入数组中的特定值是有序排列的(9,10,15,30,..,78),但是职位不是! (0,0,1,0,1,0,1,2) I have no idea how to fix this. (0,0,1,0,1,0,1,2)我不知道该如何解决。

Btw. 顺便说一句。 the class "Node": 类“节点”:

String val;
int priority;

Node leftChild;
Node rightChild;

public Node(int priority, String val) {
    this.priority = priority;
    this.val = val;
}

public int getPriority() {
    return this.priority;
}

public String getVal() {
    return this.val;
}

public Node getLeftChild() {
    return leftChild;
}

public Node getRightChild() {
    return rightChild;
}

public void setLeftChild(Node child) {
    this.leftChild = child;
}

public void setRightChild(Node child) {
    this.rightChild = child;
}

public String toString() {
    return val + " has a key " + priority;
}

I hope that your are able to help me solving this problem. 希望您能帮助我解决这个问题。

ok, i found the solution on my own :) 好的,我自己找到了解决方案:)

public int binSort(BinTreeNode nnode, int[] a, int pos) {
    if (nnode != null) {
        if (nnode.getLeftChild() != null) {
            pos = binSort(nnode.getLeftChild(), a, pos);
        }
        a[pos] = nnode.getValue();
        pos++;
        if (nnode.getRightChild() != null) {
            pos = binSort(nnode.getRightChild(), a, pos);
        }
        return pos;
    }
    return -1;
}

I needed to return pos, instead of focusNode.getPriority() and I only have to increment pos by 1, after I added the value of the current node! 我需要返回pos,而不是focusNode.getPriority(),在添加当前节点的值后,我只需要将pos加1!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM