极客寻找BST高度的极客方法

时间:2016-07-15 16:54:13

标签: java algorithm queue binary-search-tree

我从http://www.geeksforgeeks.org/iterative-method-to-find-height-of-binary-tree/

复制了此代码

我无法理解将当前级别的所有节点出列并将所有下一级节点排队的部分

// An iterative java program to find height of binary tree

import java.util.LinkedList;
import java.util.Queue;

// A binary tree node
class Node {

    int data;
    Node left, right;

    Node(int item) {
        data = item;
        left = right;
    }
}

class BinaryTree {

    static Node root;

    // Iterative method to find height of Bianry Tree
    int treeHeight(Node node) {
        // Base Case
        if (node == null) {
            return 0;
        }

        // Create an empty queue for level order tarversal
        Queue<Node> q = new LinkedList();

        // Enqueue Root and initialize height
        q.add(node);
        int height = 0;

        while (1 == 1) {

            // nodeCount (queue size) indicates number of nodes
            // at current lelvel.
            int nodeCount = q.size();
            if (nodeCount == 0) {
                return height;
            }

            height++;


/* 

This is the part where I'm very much confused , I can understand that the peek out the 1st node in queue to newnode and removes the 1st node in queue .. 


The part I can't understand is why we add nodes to that 1st position and decrease nodeCount at the end of each loop just for running the while loop until queue gets empty ??? 


So won't we have 0 as q.size() value later ??? I'm damn confused guys !!! Help me  !!! 

*/

            // Dequeue all nodes of current level and Enqueue all
            // nodes of next level
            while (nodeCount > 0) {
                Node newnode = q.peek();
                q.remove();
                if (newnode.left != null) {
                    q.add(newnode.left);
                }
                if (newnode.right != null) {
                    q.add(newnode.right);
                }
                nodeCount--;
            }
        }
    }

    // Driver program to test above functions
    public static void main(String args[]) {

        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.left = new Node(4);
        tree.root.left.right = new Node(5);
        System.out.println("Height of tree is " + tree.treeHeight(root));

    }
}

2 个答案:

答案 0 :(得分:0)

想象一下,我们在下一层有一个带n个节点的树。因此nodeCountn

while循环将遍历队列中的第一个n值。回想一下,队列是FIFO,因此只会弹出第一个n个节点。添加的新节点不会在您编写时添加到第一个位置,而是添加到最后一个位置(队列,而不是堆栈)。

因此,循环只会运行n个节点,将剩余的队列留作下一层节点。

例如:假设您有3个节点,如下所示:

Removed: none
Queue: ['level_a_1', 'level_a_2', 'level_a_3']

在这种情况下,nodeCount3。当我们将新元素排入队列时,它们会被添加到最后:

Removed: 'level_a_1'
Queue: ['level_a_2', 'level_a_3', 'level_b_1', 'level_b_2']

但是,正如您所看到的,要删除的其余2元素仍来自上一层。

答案 1 :(得分:0)

查看评论是否清除:

        // Dequeue all nodes of current level and Enqueue all
        // nodes of next level
        while (nodeCount > 0) {

            //make a copy of the node at the top of the queue 
            Node newnode = q.peek(); 

            //remove the node to be checked from the queue so it will not be checked again
            q.remove();

            //since node was removed, update the number of nodes to be checked
            nodeCount--;

            //check top node is connected to other nodes 
            if (newnode.left != null) {
                q.add(newnode.left); //add left node to queue to be checked
            }
            if (newnode.right != null) {
                q.add(newnode.right);  //add right node to queue to be checked
            }
        }