python--使用递归的方式建立二叉树
树和图的数据结构,就很有意思啦。

# coding = utf-8
class BinaryTree:
def __init__(self, root_obj):
self.key = root_obj
self.left_child = None
self.right_child = None
def insert_left(self, new_node):
node = BinaryTree(new_node)
if self.left_child is None:
self.left_child = node
else:
node.left_child = self.left_child
self.left_child = node
def insert_right(self, new_node):
node = BinaryTree(new_node)
if self.right_child is None:
self.right_child = node
else:
node.right_child = self.right_child
self.right_child = node
def get_right_child(self):
return self.right_child
def get_left_child(self):
return self.left_child
def set_root_val(self, obj):
self.key = obj
def get_root_val(self):
return self.key
root = BinaryTree('a')
print(root.get_root_val())
print(root.get_left_child())
root.insert_left('b')
print(root.get_left_child())
print(root.get_left_child().get_root_val())
root.insert_right('c')
print(root.get_right_child())
print(root.get_right_child().get_root_val())
root.get_right_child().set_root_val('hello')
print(root.get_right_child().get_root_val())
C:\Users\Sahara\.virtualenvs\test\Scripts\python.exe C:/Users/Sahara/PycharmProjects/test/python_search.py a None <__main__.BinaryTree object at 0x00000000024139B0> b <__main__.BinaryTree object at 0x00000000024139E8> c hello Process finished with exit code 0
python--使用递归的方式建立二叉树的更多相关文章
- UVA 699 The Falling Leaves (递归先序建立二叉树)
题目链接:http://acm.hust.edu.cn/vjudge/problem/19244 #include <iostream> #include <cstdio> # ...
- UVa 839 (递归方式读取二叉树) Not so Mobile
题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然 ...
- 【Python】利用递归函数调用方式,将所输入的字符串,以相反的顺序显示出来
源代码: """ 利用递归函数调用方式,将所输入的字符串,以相反的顺序显示出来 string_reverse_output():反向输出字符串的自定义函数 pending ...
- Python最佳工程实践,建立一个完美的工程项目
在程序开发时候一套好的开发环境和工具栈,可以帮我们极大的提高开发的效率,避免把大量时间浪费在周边琐事上.本文以Python为例,教大家如何快速打造完美的Python项目开发环境:内容涵盖了模块依赖管理 ...
- [LeetCode] Construct Binary Tree from Preorder and Inorder Traversal 由先序和中序遍历建立二叉树
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- python 解决递归调用栈溢出
递归函数 2578次阅读 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact ...
- LeetCode 105. Construct Binary Tree from Preorder and Inorder Traversal (用先序和中序树遍历来建立二叉树)
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- LeetCode 106. Construct Binary Tree from Inorder and Postorder Traversal 由中序和后序遍历建立二叉树 C++
Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- python通过get,post方式发送http请求和接收http响应的方法,pythonget
python通过get,post方式发送http请求和接收http响应的方法,pythonget 本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法.分享给大家 ...
随机推荐
- 即速应用_微信小程序开发 http://www.jisuapp.cn/
即速应用_微信小程序开发 http://www.jisuapp.cn/
- PEP525--异步生成器
[译] PEP 525--异步生成器 PEP原文:https://github.com/python/peps/blob/master/pep-0525.txt 创建日期:2016-07-18 译者 ...
- 关于在Fragment中设置toolbar及菜单的方法
在NoActionBar的主题中onCreateOptionsMenu方法不会运行,这里就需要将toolbar强制转换为ActionBar 在加入toolbar的监听之类后需要在onCreateVie ...
- /var/run/yum.pid 已被锁定,PID 为 2925 的另一个程序正在运行
解决办法:直接在终端运行 rm -f /var/run/yum.pid 将该文件删除,然后再次运行yum.
- C/C++ 获取文件大小
在C语言中测试文件的大小,主要使用二个标准函数. 1.fseek 函数原型:int fseek ( FILE * stream, long int offset, int origin ); 参数说明 ...
- python-基于UDP通信的套接字,socketserver模块的使用
一.基于UDP协议通信的套接字 udp是没有链接的,所以先启动哪一端都不会报错 import socket server=socket.socket(socket.AF_INET,socket.SOC ...
- Eclipse中设置Java代码格式化
一.自定义 Java 代码格式化 [Java-Code-Formatting.xml 下载],下载完毕以后,打开 Eclipse 找到如下图界面,点击 Import 导入即可.
- Confluence 6 邮件队列
需要发送的电子邮件将会在邮件队列中进行等待,Confluence 的邮件队列每分钟刷新一次.Confluence 的管理员也可以手动的刷新邮件队列中等待发送的消息. 如果在发送的时候出现了错误,那么出 ...
- Confluence 6 附件存储选项
在早期的 Confluence 版本中,我们允许存储附件到 WebDav 或者 Confluence 数据库中.针对新的 Confluence 安装,我们不再支持这 2 种存储了. 本地文件系统 在默 ...
- Confluence 6 那些文件需要备份
备份整个 home 目录是最安全的选项.但是,有很多目录是在 Confluence 启动的时候创建的并且也是可以忽略的.不管那些文件夹可以忽略,下面的文件夹必须进行备份才能回复: <conf-h ...