1

a[0]=1...a[25]=26 の順序で 1 ~ 26 の 26 個の整数の配列があります。しばらくこのコードをいじっていましたが、現在のコードが正しく機能しない理由を特定できないようです。これは私のビルド方法です:

public static binaryNode buildBalanced(int[] a, int low, int high)
{
    if(low > high)
    {
        return null;
    }
double mid = Math.floor((low + high)/2);
int iMid = (int)mid;
binaryNode node = new binaryNode(a[(int)iMid]);
node.setLeftChild(buildBalanced(a, low, (int)(iMid-1)));
node.setRightChild(buildBalanced(a, (int)(iMid+1), high));
return node;
}

binaryNode は、右の子、左の子、および情報を持つノードです。

ここで、3 つのトラバーサル (インオーダー、プレオーダー、ポストオーダー) を出力しようとすると、次のようになります。

順序: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26

プレオーダー: 13 1 2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26

ポストオーダー: 1 2 3 4 5 6 7 8 9 10 11 12 14 15 16 17 18 19 20 21 22 23 24 25 26 13

このコードは正しく動作していないようです。それとも、注文中、注文前、注文後の方法が間違っているのでしょうか?

これらは、私が使用している 3 つの印刷方法です。

public static void printInOrder(binaryNode current, Queue<binaryNode> queue)
{

    if(current == null)
    {
        queue.add(current);
        return;
    }
    if (current.getLeftChild() != null)
    {
        printInOrder(current.getLeftChild(), queue);
    }
    queue.add(current);
    if(current.getRightChild() != null)
    {
        printInOrder(current.getRightChild(), queue);
    }
    if(current.getParent() == null)
    {
        while(!queue.isEmpty())
        {
            System.out.print(queue.remove().getInfo() + " ");               
        }
    }
}

予約注文:

public static void printPreOrder(binaryNode current, Queue<binaryNode> queue)
{
     if(current == null)
        {
            queue.add(current);
            return;
        }
        queue.add(current);
        if (current.getLeftChild() != null)
        {
            printInOrder(current.getLeftChild(), queue);
        }
        if(current.getRightChild() != null)
        {
            printInOrder(current.getRightChild(), queue);
        }
        if(current.getParent() == null)
        {
            while(!queue.isEmpty())
            {
                System.out.print(queue.remove().getInfo() + " ");               
            }
        }
}

ポストオーダー:

public static void printPostOrder(binaryNode current, Queue<binaryNode> queue)
{
    if(current == null)
    {
        queue.add(current);
        return;
    }
    if (current.getLeftChild() != null)
    {
        printInOrder(current.getLeftChild(), queue);
    }
    if(current.getRightChild() != null)
    {
        printInOrder(current.getRightChild(), queue);
    }
    queue.add(current);
    if(current.getParent() == null)
    {
        while(!queue.isEmpty())
        {
            System.out.print(queue.remove().getInfo() + " ");               
        }
    }

}

あなたが提供できるどんな助けも大歓迎です!ありがとう!

4

1 に答える 1

0

ビルドステップは問題ないようです。ただし、トラバーサルは必要以上に複雑です。再帰を使用する代わりに反復トラバーサルを実行する場合にのみ、キューが必要です。

再帰を使用しているため、トラバーサルにはキューは必要ありません。

public static void printInOrder(binaryNode current)
{

    if(current == null)
    {
        return;
    }
    printInOrder(current.getLeftChild());
    System.out.print(current.getInfo() + " ");
    printInOrder(current.getRightChild());
}
于 2012-11-02T22:17:39.663 に答える