Python Forum
Return size of binary tree
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Return size of binary tree
#1
So I am an absolute beginner beginner with python.
I want to write a function that returns the size of a binary tree.
Here's my attempt that don't work:
    def size(self):
        '''
        Returns the number of nodes in the tree.
        '''
        if self.is_empty():
            return 0
        return 1 + self.lc() + self.rc()
Here's some code that works that returns a list of all members in preorder.
  def preorder(self):
        '''
        Returns a list of all members in preorder.
        '''
        if self.is_empty():
            return []
        return [self.value()] + self.lc().preorder() + self.rc().preorder()
Also here's the code that defines the tree:
class BT:
    _value = None
    _left_child = None
    _right_child = None

    def __init__(self, value=None):
        '''
        Initializes an empty tree if `value` is None, else a root with the
        specified `value` and two empty children.
        '''
        self.set_value(value)
        if not self.is_empty():
            self.cons(BT(), BT())

    def cons(self, lc, rc):
        '''
        Constructs a tree rooted at `self` based on new left/right children.
        '''
        self.set_lc(lc)
        self.set_rc(rc)
        return self

    def is_empty(self):
        '''
        Returns true if the tree is empty.
        '''
        return self.value() is None

    def lc(self):
        '''
        Returns a reference to the left child.
        '''
        return self._left_child

    def rc(self):
        '''
        Returns a reference to the right child.
        '''
        return self._right_child

    def value(self):
        '''
        Returns the value of the node rooted as `self`.
        '''
        return self._value

    def set_value(self, value):
        '''
        Sets the value rooted at `self`.
        '''
        self._value = value
        return self

    def set_lc(self, left_child):
        '''
        Sets the left child.
        '''
        self._left_child = left_child
        return self

    def set_rc(self, right_child):
        '''
        Sets the right child
        '''
        self._right_child = right_child
        return self

if __name__ == "__main__":
    log.critical("module contains no main module")
    sys.exit(1)
Reply
#2
You could use preorder, and get the len() of the list returned by preorder. If you don't want to build that whole list, you could write a method to just calculate the number of nodes:

def __len__(self):
    if self.is_empty():
        return 0
    else:
        return 1 + len(self.lc()) + len(self.rc())
Note that by naming it __len__, it will be used by the built-in len() function, which is what I do with the calls to the children on line 5.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Print Binary Tree - Python Daniel94 1 2,668 Jan-08-2020, 09:54 PM
Last Post: ichabod801
  Binary tree for words and simbols OlegBrony 4 4,985 Mar-14-2018, 09:40 AM
Last Post: OlegBrony

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020