用类实现二叉树

该二叉树:

  

用类的方法进行定义:

#coding=utf-8

class btree(object): def __init__(self,root=None,left=None,right=None): self.root = root self.left = left self.right = right def get_left(self): return self.left def get_right(self): return self.right def get_root(self): return self.root def set_root(self,root): self.root = root def set_right(self,right): self.right = right def set_left(self,left): self.left = left if __name__ == "__main__": tree = btree(root='a',left=btree(root='b',left='d',right='e'),right=btree(root='c',left='f',right='g')) print tree.get_left().get_left()

 

posted @ 2017-11-17 17:06  小步快跑&  阅读(272)  评论(0编辑  收藏  举报