Python实现二叉树
2017-12-17 15:48
309 查看
# -*- coding:utf-8 -*- class Node(object): def __init__(self,index,left=None,right=None,parent=None): self.index=index self.left_child=left self.right_child=right self.parent=parent class BinaryTree(object): def __init__(self): self.root=None def insert(self,node): n=self.root if n==None: self.root=node return while True: if node.index<=n.index: if n.left_child==None: node.parent=n n.left_child=node break else: n=n.left_child if node.index>n.index: if n.right_child==None: node.parent=n n.right_child=node break else: n=n.right_child def pre_pravel(self,node): print(node.index) if node.left_child!=None: self.pre_pravel(node.left_child) if node.right_child!=None: self.pre_pravel(node.right_child) tree=BinaryTree() node_dict={} for i in range(1,10): node_dict[i]=Node(i) tree.insert(node_dict[i]) tree.pre_pravel(tree.root)
相关文章推荐
- 二叉树递归遍历的python实现
- 二叉树的深度[剑指offer]之python实现
- python实现二叉树及其七种遍历方式(递归+非递归)
- python实现二叉树及插入,遍历操作
- 【数据结构】树与二叉树 基础要点+二叉树 Python 实现
- 重建二叉树(C++和Python实现)
- 数据结构 —— 二叉树(Python实现)
- python实现剑指offer系列:重建二叉树
- Python实现打印二叉树某一层的所有节点
- python几种数据结构的实现:栈、队列及二叉树
- python实现二叉树
- 二叉树的前序,中序,后续,递归及非递归遍历的python实现
- python实现树及二叉树的遍历
- python实现二叉树的遍历
- Python实现二叉树及遍历方法
- 「数据结构」二叉树的遍历以及Python实现
- python实现二叉树的遍历
- python实现二叉树实现查找
- python实现二叉树,以及二叉树的遍历
- Python实现二叉树的深度