第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

java中二叉搜索樹中的層序遍歷

java中二叉搜索樹中的層序遍歷

寶慕林4294392 2021-10-13 10:46:24
我為我的二叉搜索樹做了 4 次不同的遍歷。我被困在最后一個是級別順序遍歷,我似乎無法找到如何正確執(zhí)行它。主要問題是我不知道如何一次只搜索一個級別,我只能弄清楚如何搜索整個左子樹或整個右子樹。private void preOrder(BinaryNode<AnyType> t )    {        if(isEmpty()){            System.out.println("Empty");        }        if(t != null) {            System.out.println(t.element);            preOrder(t.left);            preOrder(t.right);        }    }    private void postOrder(BinaryNode<AnyType> t){        if(isEmpty()){            System.out.println("Empty");        }        if (t != null) {            postOrder(t.left);            postOrder(t.right);            System.out.println(t.element);        }    }    private void inOrder(BinaryNode<AnyType> t)    {        if(isEmpty()){            System.out.println("Empty");        }        if (t != null) {            inOrder(t.left);            System.out.println(t.element);            inOrder(t.right);        }    }    private void levelOrder(BinaryNode<AnyType> t, int level)    {        if(isEmpty()){            System.out.println("Empty");        }        if(height(t) == 2) {            System.out.println(t.element);        }else if(height(t) > 1){            levelOrder(t.left, level );            levelOrder(t.right, level );        }    }
查看完整描述

3 回答

?
汪汪一只貓

TA貢獻1898條經(jīng)驗 獲得超8個贊

我就是這樣做的。


private void levelOrder(BinaryNode root) {

        if (root == null) {

            return;

        }


        Queue<BinaryNode> q = new LinkedList<>();


        // Pushing root node into the queue.

        q.add(root);


        // Executing loop till queue becomes

        // empty

        while (!q.isEmpty()) {


            BinaryNode curr = q.poll();

            System.out.print(curr.element + " ");


            // Pushing left child current node

                if (curr.left != null) {

                    q.add(curr.left);

                }


                // Pushing right child current node

                if (curr.right != null) {

                    q.add(curr.right);

                }

            }

    }


查看完整回答
反對 回復(fù) 2021-10-13
?
互換的青春

TA貢獻1797條經(jīng)驗 獲得超6個贊

您的方法看起來像 DFS 方法它可能不遵循該方法。嘗試在此處使用 Queue 它將幫助您正確遍歷。因為它將遵循 BFS 方法,以便您可以逐層遍歷。添加第一個左節(jié)點,然后添加右節(jié)點并按照以下步驟。


查看完整回答
反對 回復(fù) 2021-10-13
?
慕標琳琳

TA貢獻1830條經(jīng)驗 獲得超9個贊

將整個搜索視為一系列“輪”,每個級別一個“輪”。


在每一輪中:


- initialize a "next round" list of nodes to empty

- process a prepared list of nodes (the ones that are at that level and thus to be searched in that round) whereby for each node :

  - do the actual comparison

  - add all the node's child nodes to the "next round" list

使用僅填充根節(jié)點的“下一輪”列表開始該過程。


重復(fù)直到“下一輪”節(jié)點列表變?yōu)榭栈蛘吣业搅四檎业膬?nèi)容。


查看完整回答
反對 回復(fù) 2021-10-13
  • 3 回答
  • 0 關(guān)注
  • 159 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號