Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Height of binary tree

I'm trying to implement a recursive method to calculate the height of a binary tree. Here is the "height"-code:

def height(self):
    if self.root==None:
        return 0
    return max(height(self.root.left), height(self.root.right))+1

When I try to call the function, I get the following error msg:

NameError: name 'height' is not defined

Does anybody see the problem?

like image 400
Lozansky Avatar asked Apr 08 '16 13:04

Lozansky


People also ask

How do you calculate the height of a tree?

The stick is held pointing straight up, at 90 degrees to your outstretched, straight arm. Carefully walk backwards until the top of the tree lines up with the top of your stick. Mark where your feet are. The distance between your feet and the tree is roughly equivalent to the height of the tree.

Why height of binary tree is log N?

In summary, in a complete binary tree with n nodes: the height is h = log2(n + 1), i.e. h is O(log n) • the number of leaves is lh = (n + 1)/2, i.e. roughly half of the nodes are at the leaves.

How do you find the height of a tree node?

Height of a node K (of a Binary Tree) = Number of edges in the longest path connecting K to any leaf node.

What is the height of AVL tree?

If there are n nodes in AVL tree, maximum height can't exceed 1.44*log2n. If height of AVL tree is h, maximum number of nodes can be 2h+1 – 1. Minimum number of nodes in a tree with height h can be represented as: N(h) = N(h-1) + N(h-2) + 1 for n>2 where N(0) = 1 and N(1) = 2.


1 Answers

This is a method of your class, hence you must call it from an instance (self) or the class itself. Though it won't work as you think, unless you define it as a staticmethod or change your call, e.g.

def height(self):
    return 1 + max(self.left.height() if self.left is not None else 0, 
                   self.right.height() if self.right is not None else 0) 

or

@staticmethod
def height(self):
    return 1 + max(self.height(self.left) if self.left is not None else 0,
                   self.height(self.right) if self.right is not None else 0)

Notice, that you shouldn't use == to compare with None (kudos to timgeb). And you must check whether child-nodes exist, too. And your algorithm doesn't work, so I've changed it slightly.

Example:

class Node:
    def __init__(self, root=None, left=None, right=None):
        self.root = root
        self.left = left
        self.right = right

    def height(self):
        return 1 + max(self.left.height() if self.left is not None else 0, 
                       self.right.height() if self.right is not None else 0)


# Create a binary tree of height 4 using the binary-heap property
tree = [Node() for _ in range(10)]
root = tree[0]

for i in range(len(tree)):
    l_child_idx, r_child_idx = (i + 1) * 2 - 1, (i + 1) * 2
    root_idx = (i + 1) // 2
    if root_idx: 
        tree[i].root = tree[root_idx]
    if l_child_idx < len(tree):
        tree[i].left = tree[l_child_idx]
    if r_child_idx < len(tree):
        tree[i].right = tree[r_child_idx]

print(root.height())  # -> 4
like image 76
Eli Korvigo Avatar answered Sep 28 '22 22:09

Eli Korvigo