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 相关文章推荐
使用pyecharts在jupyter notebook上绘图
Apr 23 Python
Pandas 同元素多列去重的实例
Jul 03 Python
Python批处理更改文件名os.rename的方法
Oct 26 Python
Python实现Linux监控的方法
May 16 Python
Django模型序列化返回自然主键值示例代码
Jun 12 Python
Python代理IP爬虫的新手使用教程
Sep 05 Python
Python序列对象与String类型内置方法详解
Oct 22 Python
python默认参数调用方法解析
Feb 09 Python
PyTorch实现重写/改写Dataset并载入Dataloader
Jul 14 Python
基于python实现生成指定大小txt文档
Jul 20 Python
PyCharm配置KBEngine快速处理代码提示冲突、配置命令问题
Apr 03 Python
PyMongo 查询数据的实现
Jun 28 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
德生1994机评
2021/03/02 无线电
基于curl数据采集之单页面并行采集函数get_htmls的使用
2013/04/28 PHP
php中url传递中文字符,特殊危险字符的解决方法
2013/08/17 PHP
php计算税后工资的方法
2015/07/28 PHP
PHP类与对象后期静态绑定操作实例详解
2018/12/20 PHP
php语法检查的方法总结
2019/01/21 PHP
JavaScript读取中文cookie时的乱码问题的解决方法
2009/10/14 Javascript
获取select元素被选中的文本内容的js代码
2014/01/29 Javascript
javascript学习笔记(七)Ajax和Http状态码
2014/10/08 Javascript
JS+CSS实现另类带提示效果的竖向导航菜单
2015/10/15 Javascript
基于jquery实现即时检查格式是否正确的表单
2016/05/06 Javascript
图文详解JavaScript的原型对象及原型链
2016/08/02 Javascript
JavaScript实现刷新不重记的倒计时
2016/08/10 Javascript
jQuery Form表单取值的方法
2017/01/11 Javascript
ES6新特性之Symbol类型用法分析
2017/03/31 Javascript
Vue入门之数据绑定(小结)
2018/01/08 Javascript
Vue+ElementUI实现表单动态渲染、可视化配置的方法
2018/03/07 Javascript
JS实现提示效果弹出及延迟隐藏的功能
2019/08/26 Javascript
Vue中使用Lodop插件实现打印功能的简单方法
2019/12/19 Javascript
vue如何使用外部特殊字体的操作
2020/07/30 Javascript
js实现简单抽奖功能
2020/11/24 Javascript
实例讲解Python编程中@property装饰器的用法
2016/06/20 Python
python生成圆形图片的方法
2020/03/25 Python
python实现决策树、随机森林的简单原理
2018/03/26 Python
python脚本生成caffe train_list.txt的方法
2018/04/27 Python
python多进程实现文件下载传输功能
2018/07/28 Python
Python OpenCV之图片缩放的实现(cv2.resize)
2019/06/28 Python
python输出决策树图形的例子
2019/08/09 Python
利用指针变量实现队列的入队操作
2012/04/07 面试题
女大学生个人求职信
2013/12/09 职场文书
党员承诺书怎么写
2014/05/20 职场文书
军训拉歌口号
2014/06/13 职场文书
入党积极分子培养人意见
2015/06/02 职场文书
Golang 获取文件md5校验的方法以及效率对比
2021/05/08 Golang
TV动画《政宗君的复仇》第二季制作决定PV公布
2022/04/02 日漫
Python开发五子棋小游戏
2022/04/28 Python