Python二叉搜索树与双向链表转换实现方法


Posted in Python onApril 29, 2016

本文实例讲述了Python二叉搜索树与双向链表实现方法。分享给大家供大家参考,具体如下:

# encoding=utf8
'''
题目:输入一棵二叉搜索树,将该二叉搜索树转换成一个排序的双向链表。
要求不能创建任何新的结点,只能调整树中结点指针的指向。
'''
class BinaryTreeNode():
  def __init__(self, value, left = None, right = None):
    self.value = value
    self.left = left
    self.right = right
def create_a_tree():
  node_4 = BinaryTreeNode(4)
  node_8 = BinaryTreeNode(8)
  node_6 = BinaryTreeNode(6, node_4, node_8)
  node_12 = BinaryTreeNode(12)
  node_16 = BinaryTreeNode(16)
  node_14 = BinaryTreeNode(14, node_12, node_16)
  node_10 = BinaryTreeNode(10, node_6, node_14)
  return node_10
def print_a_tree(root):
  if root is None:return
  print_a_tree(root.left)
  print root.value, ' ',
  print_a_tree(root.right)
def print_a_linked_list(head):
  print 'linked_list:'
  while head is not None:
    print head.value, ' ',
    head = head.right
  print ''
def create_linked_list(root):
  '''构造树的双向链表,返回这个双向链表的最左结点和最右结点的指针'''
  if root is None:
    return (None, None)
  # 递归构造出左子树的双向链表
  (l_1, r_1) = create_linked_list(root.left)
  left_most = l_1 if l_1 is not None else root
  (l_2, r_2) = create_linked_list(root.right)
  right_most = r_2 if r_2 is not None else root
  # 将整理好的左右子树和root连接起来
  root.left = r_1
  if r_1 is not None:r_1.right = root
  root.right = l_2
  if l_2 is not None:l_2.left = root
  # 由于是双向链表,返回给上层最左边的结点和最右边的结点指针
  return (left_most, right_most)
if __name__ == '__main__':
  tree_1 = create_a_tree()
  print_a_tree(tree_1)
  (left_most, right_most) = create_linked_list(tree_1)
  print_a_linked_list(left_most)
  pass

更多关于Python相关内容可查看本站专题:《Python正则表达式用法总结》、《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

Python 相关文章推荐
Python进行数据科学工作的简单入门教程
Apr 01 Python
Python进行数据提取的方法总结
Aug 22 Python
python3编码问题汇总
Sep 06 Python
Python实现判断一个字符串是否包含子串的方法总结
Nov 21 Python
Python排序搜索基本算法之归并排序实例分析
Dec 08 Python
Python使用numpy实现BP神经网络
Mar 10 Python
python实现将多个文件分配到多个文件夹的方法
Jan 07 Python
从0开始的Python学习016异常
Apr 08 Python
Python异常处理例题整理
Jul 07 Python
python3的url编码和解码,自定义gbk、utf-8的例子
Aug 22 Python
Python lxml库的简单介绍及基本使用讲解
Dec 22 Python
VSCode中autopep8无法运行问题解决方案(提示Error: Command failed,usage)
Mar 02 Python
Python实现简单字典树的方法
Apr 29 #Python
Python中操作符重载用法分析
Apr 29 #Python
Python中绑定与未绑定的类方法用法分析
Apr 29 #Python
Python过滤列表用法实例分析
Apr 29 #Python
Python松散正则表达式用法分析
Apr 29 #Python
python中私有函数调用方法解密
Apr 29 #Python
简单学习Python time模块
Apr 29 #Python
You might like
php获取网页标题和内容函数(不包含html标签)
2014/02/03 PHP
使用ThinkPHP生成缩略图及显示
2017/04/27 PHP
IE6与IE7中,innerHTML获取param的区别
2009/03/15 Javascript
Jquery replace 字符替换实现代码
2010/12/02 Javascript
node.js正则表达式获取网页中所有链接的代码实例
2014/06/03 Javascript
js控制网页背景音乐播放与停止的方法
2015/02/06 Javascript
javascript实现简单计算器效果【推荐】
2016/04/19 Javascript
js简单获取表单中单选按钮值的方法
2016/08/23 Javascript
微信小程序城市定位的实现实例(获取当前所在国家城市信息)
2017/05/17 Javascript
浅谈vue实现数据监听的函数 Object.defineProperty
2017/06/08 Javascript
jQuery实现锚点向下平滑滚动特效示例
2017/08/29 jQuery
Vue ElementUI实现:限制输入框只能输入正整数的问题
2020/07/31 Javascript
Python中index()和seek()的用法(详解)
2017/04/27 Python
解决Linux系统中python matplotlib画图的中文显示问题
2017/06/15 Python
Python tkinter实现的图片移动碰撞动画效果【附源码下载】
2018/01/04 Python
Python及Django框架生成二维码的方法分析
2018/01/31 Python
Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】
2018/07/12 Python
django富文本编辑器的实现示例
2019/04/10 Python
关于python多重赋值的小问题
2019/04/17 Python
python提取log文件内容并画出图表
2019/07/08 Python
Python实现的远程文件自动打包并下载功能示例
2019/07/12 Python
基于python调用psutil模块过程解析
2019/12/20 Python
Python concurrent.futures模块使用实例
2019/12/24 Python
Python实现分数序列求和
2020/02/25 Python
Python3安装模块报错Microsoft Visual C++ 14.0 is required的解决方法
2020/07/28 Python
Kate Spade美国官网:纽约新兴时尚品牌,以包包闻名于世
2017/11/09 全球购物
泰国的头号网上婴儿用品店:Motherhood.co.th
2019/04/09 全球购物
2014年机关植树节活动方案
2014/02/27 职场文书
2014感恩节演讲稿大全
2014/10/11 职场文书
民事起诉书范本
2015/05/19 职场文书
2015年小学数学教师个人工作总结
2015/05/25 职场文书
2015入党个人自传范文
2015/06/26 职场文书
2015七夕情人节宣传语
2015/07/14 职场文书
大学迎新生欢迎词
2015/09/29 职场文书
pytorch 如何使用amp进行混合精度训练
2021/05/24 Python
vue+elementUI实现表格列的显示与隐藏
2022/04/13 Vue.js