0

How do we determine breadth a of binary tree.

A simple bin tree

            O
           / \
          O   O
           \
            O
             \
              O
               \
                O

Breadth of above tree is 4

4
  • 1
    Are you asking about height, width, or breadth first search? I'm confused as to what breadth is defined as for your question. Commented Jul 13, 2018 at 19:37
  • I'd say it is, starting from root, how far you may reach to the right minus how far you can reach to the left. Just out of curiosity, what is the application? Commented Jul 13, 2018 at 19:51
  • @user58697 I heard of height/depth of the tree. Just out of curiosity i thought of solving this but got stuck Commented Jul 14, 2018 at 6:59
  • @AndrewFan The problem states Breadth clearly. I am not sure why you got confused anyway user58697 explained much more. Commented Jul 14, 2018 at 7:00

1 Answer 1

1

You could use a recursive function that returns two values for a given node: the extent of the subtree at that node towards the left (a negative number or zero), and the extent to the right (zero or positive). So for the example tree given in the question it would return -1, and 3.

To find these extends is easy when you know the extents of the left child and of the right child. And that is where the recursion kicks in, which in fact represents a depth-first traversal.

Here is how that function would look in Python:

def extents(tree):
    if not tree:
        # If a tree with just one node has extents 0 and 0, then "nothing" should
        #  have a negative extent to the right and a positive on the left, 
        #  representing a negative breadth
        return 1, -1
    leftleft, leftright = extents(tree.left)
    rightleft, rightright = extents(tree.right)
    return min(leftleft-1, rightleft+1), max(leftright-1, rightright+1)

The breadth is simply the difference between the two extents returned by the above function, plus 1 (to count for the root node):

def breadth(tree):
    leftextent, rightextent = extents(tree)
    return rightextent-leftextent+1

The complete Python code with the example tree, having 6 nodes, as input:

from collections import namedtuple
Node =  namedtuple('Node', ['left', 'right'])

def extents(tree):
    if not tree:
        return 1, -1
    leftleft, leftright = extents(tree.left)
    rightleft, rightright = extents(tree.right)
    return min(leftleft-1, rightleft+1), max(leftright-1, rightright+1)

def breadth(tree):
    left, right = extents(tree)
    return right-left+1

# example tree as given in question
tree = Node(
    Node(
        None,
        Node(None, Node(None, Node(None, None)))
    ),
    Node(None, None)
)

print(breadth(tree)) # outputs 4
Sign up to request clarification or add additional context in comments.

1 Comment

if not tree: return 1, -1 That was really good thought. Thanks Trin :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.