Python实现二叉树的左中右序遍历

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2018/3/18 12:31
# @Author : baoshan
# @Site :
# @File : binarytree.py
# @Software: PyCharm Community Edition # python 实现二叉树的左中右序遍历 class Node(object):
def __init__(self, index):
self.index = index
self.left_child = None
self.right_child = None class BinaryTree(Node):
def __init__(self, root):
self.root = root def pre_travel(self, node):
if not node:
return
print(node.index)
self.pre_travel(node.left_child)
self.pre_travel(node.right_child) def mid_travel(self, node):
if not node:
return
self.mid_travel(node.left_child)
print(node.index)
self.mid_travel(node.right_child) def suf_travel(self, node):
if not node:
return
self.suf_travel(node.left_child)
self.suf_travel(node.right_child)
print(node.index) node_dict = {}
for i in range(1, 10):
node_dict[i] = Node(i) node_dict[1].left_child = node_dict[2]
node_dict[1].right_child = node_dict[3]
node_dict[2].left_child = node_dict[5]
node_dict[2].right_child = node_dict[6]
node_dict[3].left_child = node_dict[7]
node_dict[7].left_child = node_dict[8]
node_dict[7].right_child = node_dict[9] tree = BinaryTree(node_dict[1])
print('---左序遍历---')
tree.pre_travel(tree.root)
print('---中序遍历---')
tree.mid_travel(tree.root)
print('---右序遍历---')
tree.suf_travel(tree.root)
输出结果:
/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/baoshan/PycharmProjects/myProject/python_weixin_study/binarytree.py
---左序遍历---
1
2
5
6
3
7
8
9
---中序遍历---
5
2
6
1
8
7
9
3
---右序遍历---
5
6
2
8
9
7
3
1 Process finished with exit code 0
请各位大虾指教!
Python实现二叉树的左中右序遍历的更多相关文章
- 【LeetCode-面试算法经典-Java实现】【145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)】
[145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a bin ...
- [Swift]LeetCode971.翻转二叉树以匹配先序遍历 | Flip Binary Tree To Match Preorder Traversal
Given a binary tree with N nodes, each node has a different value from {1, ..., N}. A node in this b ...
- 二叉树 排序二叉树-可以通过中序遍历得到排序的数据 二叉排序树时间复杂度O(logn),
二叉树是一种非常重要的数据结构,它同时具有数组和链表各自的特点:它可以像数组一样快速查找,也可以像链表一样快速添加.但是他也有自己的缺点:删除操作复杂. 虽然二叉排序树的最坏效率是O(n),但它支持动 ...
- 数据结构-用C++实现一个二叉树,递归方法中序遍历
1:二叉排序树,又称二叉树.其定义为:二叉排序树或者空树,或者是满足如下性质的二叉树. (1)若它的左子树非空,则左子树上所有节点的值均小于根节点的值. (2)若它的右子树非空,则右子树上所有节点的值 ...
- 翻转二叉树(深搜-先序遍历-交换Node)
题目:翻转二叉树,例如 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 已知二叉树的节点定义如下: class TreeNode { in ...
- C++二叉树前中后序遍历(递归&非递归)统一代码格式
统一下二叉树的代码格式,递归和非递归都统一格式,方便记忆管理. 三种递归格式: 前序遍历: void PreOrder(TreeNode* root, vector<int>&pa ...
- Leetcode 94. 二叉树的中序遍历
1.问题描述 给定一个二叉树,返回它的中序 遍历. 示例: 输入: [1,null,2,3] 1 \ 2 / 3 输出: [1,3,2] 进阶: 递归算法很简单,你可以通过迭代算法完成吗? 2.解法一 ...
- PTA L2-006 树的遍历-二叉树的后序遍历+中序遍历,输出层序遍历 团体程序设计天梯赛-练习集
L2-006 树的遍历(25 分) 给定一棵二叉树的后序遍历和中序遍历,请你输出其层序遍历的序列.这里假设键值都是互不相等的正整数. 输入格式: 输入第一行给出一个正整数N(≤),是二叉树中结点的 ...
- LeetCode 145. Binary Tree Postorder Traversal二叉树的后序遍历 (C++)
题目: Given a binary tree, return the postorder traversal of its nodes' values. Example: Input: [1,nul ...
随机推荐
- YAML中重复的KEY的判断
package com.test.util; import java.io.BufferedReader; import java.io.FileInputStream; import java.io ...
- ubuntu(14.04) 网路管理
网络五元素: MAC地址 IP地址 网络掩码 网关 DNS:将ip地址转换成域名 ping ifconfig route /etc/resolv.conf netstat ip nmap cat /e ...
- httpd: Could not reliably determine the server's fully qualified domain name(转)
ttpd: Could not reliably determine the server's fully qualified domain name, using 127.0.0.1 for Ser ...
- 【Linux】参数代换命令xargs
xargs 是在做什么的呢?就以字面上的意义来看,x 是加减乘除的乘号,args 则是 arguments (参数) 的意思,所以说,这个玩意儿就是在产生某个命令的参数的意思! xargs 可以读入 ...
- [AaronYang风格]微软Unity2.X系统学习笔记,记录
读者约定: Unity我直接简写U了 Unity Dependency Injection(DI) 欢迎学习Unity,通过学完下面的几个流程的引导,你应该就可以很顺利的应用Unity到你的项目中去了 ...
- mongodb c++ driver 2.0编译使用
安装boost1.48.0 在boost的官网下载boost1.48.0,链接例如以下: http://sourceforge.net/projects/boost/files/boost/1.48. ...
- CListCtrl选中某行
原文链接: http://blog.csdn.net/wxq1987525/article/details/7461461 1.设置CListCtrl选中行 m_list.SetItemState( ...
- HANA初印象
今天手懒,看了一些SAP HANA 的一些外文介绍,不翻译了,直接剽窃过来,供参考. 1. HANA 是什么东西? “HANA doesn't actually mean anything, but ...
- Android下的联网下载的操作
一:从网络下载图片 MainActivity: NetService 1.由路径获取Url 2.使用url打开HttpURLConnection连接 3.根据路径查找本地sd卡是否有缓存文件,如果文件 ...
- ROC 曲线简要解释
阳性 (P, positive)阴性 (N, Negative)真阳性 (TP, true positive):正确的肯定.又称:命中 (hit)真阴性 (TN, true negative):正确的 ...