python 将有序数组转换为二叉树的方法


Posted in Python onMarch 26, 2019

题目:将[0,1,2,3,4,5,6,7,8,9,10]存储到二叉树,原数组有序,转换为二叉排序树。

二叉排序树的特点:当前节点的左子树上的所有节点都小于该节点,右子树上的所有节点都小于该节点。

二叉排序也称为二叉查找树。

我的实现思路:

取有序数组的中间节点作为根节点,将数组分为左右两个部分,对左右两个子数组做相同的操作,递归的实现。

图示:

python 将有序数组转换为二叉树的方法

1

python 将有序数组转换为二叉树的方法

2

python 将有序数组转换为二叉树的方法

3

代码实现:

def array_to_bitree(array):
  #判断arr是否为空
  if len(array)==0:
    return BiTNode(array[0])
  mid=len(array)//2 # 有序数组的中间元素的下标
  #print(mid)
  #start=0 # 数组第一个元素的下标
  #end=-1 # 数组最后一个元素的下标
  if len(array)>0:
    #将中间元素作为二叉树的根
    root=BiTNode(array[mid])
    #如果左边的元素个数不为零,则递归调用函数,生成左子树
    if len(array[:mid])>0:
      root.left_child = arrayToBiTree(array[:mid])
    #如果右边的元素个数不为零,则递归调用函数,生成左子树
    if len(array[mid+1:])>0:
      root.right_child = arrayToBiTree(array[mid+1:])
  return root

我们调用前面写的三种遍历方法看一看,我们构造的树是否正确:

#将[0,1,2,3,4,5,6,7,8,9,10]存储到二叉树
if __name__ == '__main__':
  #先构造一个有序数组、链表
  arr=[]
  for i in range(10):
    arr.append(i)
  print(arr)
  #调用函数
  BT=arrayToBiTree(arr)
  #前序遍历二叉树
  print("前序")
  print_tree_pre_order(BT)
  # 中序遍历二叉树
  print("中序")
  print_tree_mid_order(BT)
  # 后序遍历二叉树
  print("后序")
  print_tree_after_order(BT)

输出:

python 将有序数组转换为二叉树的方法

根据这三种遍历结果可以判断出二叉树的结构,结果和前面的是一样的,代码如下:

#定义二叉树结点类型
class BiTNode:
  """docstring for BiTNode"""
  def __init__(self,arg):
    self.data = arg
    self.left_child = None
    self.right_child = None

#前序遍历
def print_tree_pre_order(root):
  #先判断二叉树是否为空
  #if root.left_child is None and root.right_child is None:
  if root is None:
    return root
  #先根
  print(root.data)
  #再左
  if root.left_child is not None:
    print_tree_pre_order(root.left_child)
  #再右
  if root.right_child is not None:
    print_tree_pre_order(root.right_child)

#中序遍历二叉树
def print_tree_mid_order(root):

  #先判断二叉树是否为空,当左右节点都为空时
  if root is None:
    return
  #中序遍历 左根右
  #遍历左子树
  if root.left_child is not None:
    print_tree_mid_order(root.left_child)
  #遍历根节点
  print(root.data)
  #遍历右子树
  if root.right_child is not None:
    print_tree_mid_order(root.right_child)

#后序遍历
def print_tree_after_order(root):
  #先判断二叉树是否为空
  if root is None:
    return root
  #再左
  if root.left_child is not None:
    print_tree_after_order(root.left_child)
  #再右
  if root.right_child is not None:
    print_tree_after_order(root.right_child)
  #先根
  print(root.data)

def array_to_bitree(array):
  #判断arr是否为空
  if len(array)==0:
    return BiTNode(array[0])
  mid=len(array)//2 # 有序数组的中间元素的下标
  #print(mid)
  #start=0 # 数组第一个元素的下标
  #end=-1 # 数组最后一个元素的下标
  if len(array)>0:
    #将中间元素作为二叉树的根
    root=BiTNode(array[mid])
    #如果左边的元素个数不为零,则递归调用函数,生成左子树
    if len(array[:mid])>0:
      root.left_child = array_to_bitree(array[:mid])
    #如果右边的元素个数不为零,则递归调用函数,生成左子树
    if len(array[mid+1:])>0:
      root.right_child = array_to_bitree(array[mid+1:])
  return root


    

#将[0,1,2,3,4,5,6,7,8,9,10]存储到二叉树
if __name__ == '__main__':
  #先构造一个有序数组、链表
  arr=[]
  for i in range(9):
    arr.append(i)
  print(arr)
  #调用函数
  BT=array_to_bitree(arr)
  #前序遍历二叉树
  print("前序")
  print_tree_pre_order(BT)
  # 中序遍历二叉树
  print("中序")
  print_tree_mid_order(BT)
  # 后序遍历二叉树
  print("后序")
  print_tree_after_order(BT)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python中的异常处理学习笔记
Jan 28 Python
用Python制作检测Linux运行信息的工具的教程
Apr 01 Python
Python解惑之整数比较详解
Apr 24 Python
用python统计代码行的示例(包括空行和注释)
Jul 24 Python
dataframe 按条件替换某一列中的值方法
Jan 29 Python
Scrapy框架爬取西刺代理网免费高匿代理的实现代码
Feb 22 Python
如何使用 Python 读取文件和照片的创建日期
Sep 05 Python
Python安装第三方库攻略(pip和Anaconda)
Oct 15 Python
如何向scrapy中的spider传递参数的几种方法
Nov 18 Python
Python爬虫实战之爬取京东商品数据并实实现数据可视化
Jun 07 Python
图文详解matlab原始处理图像几何变换
Jul 09 Python
Python使用psutil库对系统数据进行采集监控的方法
Aug 23 Python
浅谈Python爬虫基本套路
Mar 25 #Python
我用Python抓取了7000 多本电子书案例详解
Mar 25 #Python
详解python:time模块用法
Mar 25 #Python
Python minidom模块用法示例【DOM写入和解析XML】
Mar 25 #Python
Python实例方法、类方法、静态方法的区别与作用详解
Mar 25 #Python
详解Python装饰器
Mar 25 #Python
详解用python自制微信机器人,定时发送天气预报
Mar 25 #Python
You might like
PHP的Yii框架中使用数据库的配置和SQL操作实例教程
2016/03/17 PHP
PHP中静态变量的使用方法实例分析
2016/12/01 PHP
PHP数据库编程之MySQL优化策略概述
2017/08/16 PHP
jquery indexOf使用方法
2013/08/19 Javascript
jquery学习总结(超级详细)
2014/09/04 Javascript
JavaScript 实现完美兼容多浏览器的复制功能代码
2015/04/28 Javascript
非常实用的12个jquery代码片段
2015/11/02 Javascript
jQuery实现带有动画效果的回到顶部和底部代码
2015/11/04 Javascript
对jQuary选择器的全面总结
2016/06/20 Javascript
微信小程序页面滑动屏幕加载数据效果
2020/11/16 Javascript
详细分析JS函数去抖和节流
2017/12/05 Javascript
详解通过源码解析Node.js中cluster模块的主要功能实现
2018/05/16 Javascript
JavaScript中 ES6变量的结构赋值
2018/07/10 Javascript
小程序文字跑马灯效果
2018/12/28 Javascript
[03:55]2014DOTA2国际邀请赛 Fnatic经理采访赢DK在情理之中
2014/07/10 DOTA
在python的类中动态添加属性与生成对象
2016/09/17 Python
Python脚本实现自动将数据库备份到 Dropbox
2017/02/06 Python
python用pickle模块实现“增删改查”的简易功能
2017/06/07 Python
python 返回一个列表中第二大的数方法
2019/07/09 Python
python 解决flask uwsgi 获取不到全局变量的问题
2019/12/22 Python
Keras自定义实现带masking的meanpooling层方式
2020/06/16 Python
pycharm激活码2020最新分享适用pycharm2020最新版亲测可用
2020/11/22 Python
html5手机端页面可以向右滑动导致样式受影响的问题
2018/06/20 HTML / CSS
娇韵诗加拿大官网:Clarins加拿大
2017/11/20 全球购物
软件配置管理有什么好处
2015/04/15 面试题
高级销售员求职信
2013/10/25 职场文书
经济学博士求职自荐信范文
2013/11/23 职场文书
小学生综合素质评语
2014/04/23 职场文书
教师竞聘演讲稿
2014/05/16 职场文书
厉行勤俭节约倡议书
2014/05/16 职场文书
计算机专业毕业生自荐书
2014/06/02 职场文书
硕士学位论文评语
2014/12/31 职场文书
小学生反邪教心得体会
2016/01/15 职场文书
实习报告怎么写
2019/06/20 职场文书
浅谈Python实现opencv之图片色素的数值运算和逻辑运算
2021/06/23 Python
python实现层次聚类的方法
2021/11/01 Python