简体   繁体   中英

How can you calculate depth of a binary tree with less complexity?

Given a binary search tree t, it is rather easy to get its depth using recursion, as the following:

def node_height(t):     
    if t.left.value == None and t.right.value == None:
        return 1
    else:
        height_left = t.left.node_height()
        height_right = t.right.node_height()
        return ( 1 + max(height_left,height_right) )

However, I noticed that its complexity increases exponentially, and thus should perform very badly when we have a deep tree. Is there any faster algorithm for doing this?

If you store the height as a field in the Node object, you can add 1 as you add nodes to the tree (and subtracting during remove).

That'll make the operation constant time for getting the height of any node, but it adds some additional complexity into the add/remove operations.

This kind of extends from what @cricket_007 mentioned in his answer.

So, if you do a ( 1 + max(height_left,height_right) ) , you end up having to visit every node, which is essentially an O(N) operation. For an average case with a balanced tree, you would be looking at something like T(n) = 2T(n/2) + Θ(1) .

Now, this can be improved to a time of O(1) if you can store the height of a certain node. In that case, the height of the tree would be equal to the height of the root. So, the modification you would need to make would be to your insert(value) method. At the beginning, the root is given a default height of 0. The node to be added is assigned a height of 0. For every node you encounter while trying to add this new node, increase node.height by 1 if needed, and ensure it is set to 1 + max(left child's height, right child's height). So, the height function will simply return node.height, hence allowing for constant time . The time complexity for the insert will also not change; we just need some extra space to store n integer values, where n is the number of nodes.

The following is shown to give an understanding of what I am trying to say.

          5 [0]

- insert 2 [increase height of root by 1]

          5 [1]
         / 
        /   
   [0] 2 

- insert 1 [increase height of node 2 by 1, increase height of node 5 by 1]

          5 [2]
         / 
        /   
   [1] 2    
      / 
     /   
[0] 1  

- insert 3 [new height of node 2 = 1 + max(height of node 1, height of node 3) 
                                 = 1 + 0 = 1; height of node 5 also does not change]

          5 [2]
         / 
        /   
   [1] 2     
      / \
     /   \
[0] 1     3 [0]

- insert 6 [new height of node 5 = 1 + max(height of node 2, height of node 6) 
                                 = 1 + 1 = 2]

          5 [2]
         / \
        /   \
   [1] 2     6 [0]
      / \
     /   \
[0] 1     3 [0]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM