Traversing a tree into an array

只愿长相守 提交于 2019-12-25 01:44:32

问题


I'm supposed to traverse a Binary Seach Tree as preorder, inorder and postorder and insert the values into an Object[] in Java. I honestly have no clue how to do this and I need some advice.

My function:

public Object[] traversePreOrder()

All I need are basically some ideas or hints on how to complete this task. If I traverse a tree as preorder Object[0] is obviously the value of the root. Then I continue in the left tree and insert the most left values in my array. Next I insert the right child of the most left node and continue with the parent of both nodes if the right node has no children.

But my method can't remember which values have already been checked and inserted. I also thought of traversing in postorder, putting each element onto an stack and push each element onto my array, but I've got limits to my knowledge on how to implement this.

Help!

Here's what is in my opinion already correct:

@Override
public Object[] traversePreOrder() {
    Object[] preOrderArray = new Object[elements];
    if (elements == 0) {
        return preOrderArray;
    }



    return preOrderArray;

}

The gap has to be filled obviously.

Additionally I have as basic methods and constructors:

BinarySearchTreeNode(T value, BinarySearchTreeNode<T> left,
        BinarySearchTreeNode<T> right) {
    this.value = value;
    this.left = left;
    this.right = right;
}

BinarySearchTreeNode(T value) {
    this(value, null, null);
}

BinarySearchTreeNode<T> getLeft() {
    return left;
}

void setLeft(BinarySearchTreeNode<T> left) {
    this.left = left;
}

BinarySearchTreeNode<T> getRight() {
    return right;
}

void setRight(BinarySearchTreeNode<T> right) {
    this.right = right;
}

T getValue() {
    return value;
}

void setValue(T value) {
    this.value = value;
}

回答1:


An ideal case for using recursion. Create a method

List<BinarySearchTreeNode> traverse(BinarySearchTreeNode n) {
    // traversal code
}

where the traversal code traverses the left child and the right child if any, and

  • for preorder traversal, concatenates the value, left child's result and the right child's result;
  • for inorder traversal, concatenates the left child's result, the value, and the right child's result;
  • for postorder traversal, concatenates the left child's result, the right child's result and the value.

Call traverse for the root and convert the result into an Object[].




回答2:


As Arend mentioned above, this is the general idea. I'm not sure if you can have it return a list each time, but you might be able to. However, if that implementation doesn't work you could try something like this just to see if everything else is working:

private List nodeValues = new ArrayList();

public void traversePreRecursive(BinarySearchTreeNode node) 
{
    if (node != null)
    {
        nodeValues.add(node.getValue());
        traversePreRecursive(node.getLeft());
        traversePreRecursive(node.getRight());
    }
}


来源:https://stackoverflow.com/questions/16819177/traversing-a-tree-into-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!