Giter Site home page Giter Site logo

binary_tree's Introduction

Binary tree class in python

The tree as an array data structure as described here: https://en.wikipedia.org/w/index.php?title=Binary_tree&oldid=964115444#Arrays

$ ./bintree.py

Adding left child a to parent 0
Adding right child b to parent 0
Adding left child c to parent a
Adding left child d to parent b
Adding right child e to parent b
Adding left child f to parent e
Adding right child g to parent e
       0        
     a    b     
   c  -  d  e   
- - - - - - f g 
Tree array: [0, 'a', 'b', 'c', None, 'd', 'e', None, None, None, None, None, None, 'f', 'g']
Tree depth: 3
Tree leaves: ['c', 'd', 'f', 'g']
Depth of each node: ['node 0: 0', 'node a: 1', 'node b: 1', 'node c: 2', 'node d: 2', 'node e: 2', 'node f: 3', 'node g: 3']
Get nodes at depth 2: ['c', 'd', 'e']
Parent of nodes 'd' and 'e': ('b', 'b')
Leaves from node 'b': ['d', 'f', 'g']
Offspring from node 'b': ['d', 'e', 'f', 'g']
Offspring indices from node 'b': [5, 6, 13, 14]
================================================================================
Swapping branches a and b
       0        
     b    a     
   d  e  c  -   
- - f g - - - - 
Swapping branches b and a
       0        
     a    b     
   c  -  d  e   
- - - - - - f g 
================================================================================
Aligning trees
Tree 1:
       0        
     1    2     
   a  -  3  4   
- - - - c d e f 
Tree 2:
       0        
     1    2     
   3  4  a  -   
f e d c - - - - 
Alignment score: 5
Tree 2 aligned on Tree 1:
       0        
     2    1     
   a  -  4  3   
- - - - c d e f 
--------------------------------------------
Aligning new Tree 2 on Tree 1
Tree 2:
       0        
     1    2     
   3  4  a  -   
c e d f - - - - 
Alignment score: 3
Tree 2 aligned on Tree 1:
       0        
     2    1     
   a  -  3  4   
- - - - c e d f 

binary_tree's People

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.