【问题标题】:Incrementing Height Variable in Binary Search Tree在二叉搜索树中增加高度变量
【发布时间】:2021-11-29 18:21:39
【问题描述】:

我正在尝试使用迭代方法来计算我的二叉搜索树的高度,但我无法确定何时将 1 添加到我的高度变量,也就是当有新级别的节点时。这是我的插入方法:

public Node insertRec(Node x, String key) {
        if (x == null)
        {
            x = new Node(key);              // if bst is empty, add new node and return
            height++;
            return x;
        }

        if (key.compareTo(x.key) < 0) {         
            x.left = insertRec(x.left, key);    // recursive calls to find correct spot to insert in the tree
        }
        else if (key.compareTo(x.key) > 0) {
            x.right = insertRec(x.right, key);
        }   
        return x;                                       // returns unchanged node
    }

如您所见,现在我只是在添加新节点时将高度加 1,然后我使用另一种方法仅返回该变量,但这不是高度的正确数字。我相信我需要某种测试,以便如果有新级别的节点,则将 1 添加到高度,但我不确定从哪里开始。

【问题讨论】:

  • 不要增加高度,计算高度根据子节点的高度。

标签: java binary-search-tree nodes increment


【解决方案1】:

一个简单的解决方案是在你的函数中添加一个height 变量,然后你可以在创建节点后计算 maxHeight:



public Node insertRec(Node x, String key, int newHeight) {
        if (x == null)
        {
            x = new Node(key);              
            if (newHeight > maxHeight) 
                maxHeight = newHeight;

            return x;
        }

        if (key.compareTo(x.key) < 0) {         
            x.left = insertRec(x.left, key, ++newHeight);    // recursive calls to find correct spot to insert in the tree
        }
        else if (key.compareTo(x.key) > 0) {
            x.right = insertRec(x.right, key, ++newHeight);
        }   
        return x;                                       // returns unchanged node
    }

【讨论】:

    【解决方案2】:

    如果你使用递归调用,这很容易:

    public int getHeight(Node x)
    {
        int height = 0;
        if(x.left != null)
            height = Math.max(height, getHeight(x.left);
        if(x.right != null)
            height = Math.max(height, getHeight(x.right);
        
        return height + 1;
    }
    

    但是由于你想要一个迭代方法,你需要自己实现一个堆栈并保留当前值的映射:

    public int getHeight(Node root)
    {
        HashMap<Node, Integer> heights = new HashMap<>();
        Stack<Node> toVisit = new Stack<>();
        toVisit.push(root);
        
        while(!toVisit.isEmpty())
        {
            Node x = toVisit.peek(); // Before removing the node from the stack, check that his children have been already visited
            
            if(x.left != null && !heights.containsKey(x.left))
            {
                //Left node has not been visited yet
                toVisit.push(x.left);
                continue;
            }
            if(x.right != null && !heights.containsKey(x.right))
            {
                //Right node has not been visited yet
                toVisit.push(x.right);
                continue;
            }
            int height = 0;
            if(x.left != null)
                height = Math.max(height, heights.get(x.left));
            if(x.right != null)
                height = Math.max(height, heights.get(x.right));
            
            heights.put(x, height + 1);
            toVisit.pop(); //Remove node x from the stack, it has been successfully visited
         }
    
         return heights.get(root);
    }
    

    注意:我没有测试代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-02-05
      • 2013-10-19
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多