简体   繁体   中英

Binary Tree Sorrt Java Recursive

i've got some troubles with the Binary Tree Sort in Java. I need to create the following method, but i don't get it, how to implement the position (pos) properly. 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. The method

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

should place the values of each node/leaf into the array a, at position pos. I am not allowed to use global variables! And as you can see, we need to implement the inOrder traversal

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

Boss has a key 10 - 0

Boss has a key 15 - 1

Boss has a key 30 - 0

Boss has a key 36 - 1

Boss has a key 50 - 0

Boss has a key 70 - 1

Boss has a key 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! (0,0,1,0,1,0,1,2) I have no idea how to fix this.

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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