python3 二叉树的存储和三种遍历
2017-09-14 10:13
288 查看
#coding:utf-8 class node(): def __init__(self,k=None,l=None,r=None): self.key=k; self.left=l; self.right=r; def create(root): a=input('enter a key:'); if a is '#': root=None; else: root=node(k=a); root.left=create(root.left); root.right=create(root.right); return root; def preorder(root): #前序遍历 if root is None: return ; else : print(root.key,end=''); preorder(root.left); preorder(root.right); def inorder(root): #中序遍历 if root is None: return ; else: inorder(root.left); print(root.key,end=''); inorder(root.right); def postorder(root): # 后序遍历 if root is None: return ; else : postorder(root.left); postorder(root.right); print(root.key,end=''); root=None; # 测试代码 root=create(root); preorder(root); print('\n') inorder(root); print('\n') postorder(root);
相关文章推荐
- 二叉树的基本性质、存储方式与遍历(Python实现)
- python 用栈和队列实现二叉树的深度优先遍历(三种)和广度优先遍历
- 二叉树的顺序存储和三种遍历(二)
- Python实现二叉树的存储和遍历
- 二叉树的链式存储和三种遍历(一)
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- 二叉树实现数据存储和遍历(Python)
- 二叉树用数组顺序存储(完全二叉树)并实现三种遍历,代码详解!!!
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- 二叉树的存储方式以及递归和非递归的三种遍历方式
- 【python中二叉树的实现】python中二叉树的创建、三种方式递归遍历和非递归遍历
- 【转】二叉树的三种遍历的相互转化——已知先序中序求后序
- python数组遍历三种实用方法
- 二叉树的三种非递归遍历方式。
- 详细讲解二叉树三种遍历方式的递归与非递归实现
- 初识树结构,简单模拟无规律的二叉树,实现二叉树的构建,计算树的深度以及三种遍历方式以及搜索删除,销毁整个树
- 二叉树的建立、三种(递归、非递归)遍历方法
- 二叉树的三种遍历练习题