joowani / binarytree

Python Library for Studying Binary Trees
http://binarytree.readthedocs.io
MIT License
1.81k stars 171 forks source link

Add is_symmetric property #20

Closed gillespiecd closed 5 years ago

gillespiecd commented 5 years ago

Background

I enjoy using binarytree, especially find printing useful in ipython.

I thought it would be fun to add a new property is_symmetric to properties. This comes up sometimes as an exercise with trees -- is it symmetric (a mirror of itself).

Also wanted to learn more about the doctest and coverage modules 👍

Updates

  1. Add new property and function to calculate if binary tree is symmetric

Steps to Reproduce

>>> from binarytree import Node
>>> a = Node(5)
>>> a.is_symmetric
True
>>> a.left = Node(6)
>>> a.is_symmetric
False
>>> a.right = Node(6)
>>> a.is_symmetric
True
>>> a.right.left = Node(7)
>>> a.is_symmetric
False
>>> a.right.right = Node(8)
>>> a.left.left = Node(8)
>>> a.left.right = Node(7)
>>> print(a)

    __5__
   /     \
  6       6
 / \     / \
8   7   7   8

>>> a.is_symmetric
True
coveralls commented 5 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling 352374976c267762458663d263c418c3c2d5d250 on gillespiecd:symmetric into 23cb6f1e60e66b96133259031e97ec03e932ba13 on joowani:dev.

coveralls commented 5 years ago

Coverage Status

Coverage remained the same at 100.0% when pulling c0a796d9bb02db7f819111ea0c878440b10595d3 on gillespiecd:symmetric into 23cb6f1e60e66b96133259031e97ec03e932ba13 on joowani:dev.

joowani commented 5 years ago

Hi @gillespiecd,

Looks good! Thank you for your interest and contribution :)