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程序设计入门(4)模块和包
Jun 16 Python
Python中暂存上传图片的方法
Feb 18 Python
Python中的with...as用法介绍
May 28 Python
解决python2.7 查询mysql时出现中文乱码
Oct 09 Python
深入解答关于Python的11道基本面试题
Apr 01 Python
详解python里使用正则表达式的全匹配功能
Oct 19 Python
python 实现对文件夹内的文件排序编号
Apr 12 Python
Python通过VGG16模型实现图像风格转换操作详解
Jan 16 Python
Transpose 数组行列转置的限制方式
Feb 11 Python
python绘制封闭多边形教程
Feb 18 Python
使用K.function()调试keras操作
Jun 17 Python
Python Matplotlib绘图基础知识代码解析
Aug 31 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中的cookie
2006/11/26 PHP
php 正则匹配函数体
2009/08/25 PHP
php文件操作相关类实例
2015/06/18 PHP
PHP防止sql注入小技巧之sql预处理原理与实现方法分析
2019/12/13 PHP
为数据添加append,remove功能
2006/10/03 Javascript
仿猪八戒网左下角的文字滚动效果
2011/10/28 Javascript
js时间日期和毫秒的相互转换
2013/02/22 Javascript
Query中click(),bind(),live(),delegate()的区别
2013/11/19 Javascript
js类定义函数时用prototype与不用的区别示例介绍
2014/06/10 Javascript
学习JavaScript设计模式(单例模式)
2015/11/26 Javascript
MVC+jQuery.Ajax异步实现增删改查和分页
2020/12/22 Javascript
D3.js实现折线图的方法详解
2016/09/21 Javascript
简单谈谈ES6的六个小特性
2016/11/18 Javascript
vue.js单页面应用实例的简单实现
2017/04/10 Javascript
Node.js中读取TXT文件内容fs.readFile()用法
2018/10/10 Javascript
JavaScript实现星级评价效果
2019/05/17 Javascript
微信sdk实现禁止微信分享(使用原生php实现)
2019/11/15 Javascript
Element PageHeader页头的使用方法
2020/07/26 Javascript
nodejs+koa2 实现模仿springMVC框架
2020/10/21 NodeJs
基于vue+echarts数据可视化大屏展示的实现
2020/12/25 Vue.js
Eclipse + Python 的安装与配置流程
2013/03/05 Python
django请求返回不同的类型图片json,xml,html的实例
2018/05/22 Python
Python 使用Numpy对矩阵进行转置的方法
2019/01/28 Python
python调用c++传递数组的实例
2019/02/13 Python
pandas factorize实现将字符串特征转化为数字特征
2019/12/19 Python
Python 实现使用空值进行赋值 None
2020/03/12 Python
详解如何使用rem或viewport进行移动端适配
2020/08/14 HTML / CSS
利用异或运算实现两个无符号数的加法运算
2013/12/20 面试题
《散步》教学反思
2014/03/02 职场文书
高中课程设置方案
2014/05/28 职场文书
科学发展观活动总结
2014/08/28 职场文书
实习科室评语
2015/01/04 职场文书
岗位聘任报告
2015/03/02 职场文书
学习师德师风的心得体会(2篇)
2019/10/08 职场文书
SQL Server2019数据库之简单子查询的具有方法
2021/04/27 SQL Server
CSS 左边固定宽右边自适应的6种方法
2022/05/15 HTML / CSS