树和图的数据结构,就很有意思啦。

# 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--使用递归的方式建立二叉树的更多相关文章

  1. UVA 699 The Falling Leaves (递归先序建立二叉树)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/19244 #include <iostream> #include <cstdio> # ...

  2. UVa 839 (递归方式读取二叉树) Not so Mobile

    题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然 ...

  3. 【Python】利用递归函数调用方式,将所输入的字符串,以相反的顺序显示出来

    源代码: """ 利用递归函数调用方式,将所输入的字符串,以相反的顺序显示出来 string_reverse_output():反向输出字符串的自定义函数 pending ...

  4. Python最佳工程实践,建立一个完美的工程项目

    在程序开发时候一套好的开发环境和工具栈,可以帮我们极大的提高开发的效率,避免把大量时间浪费在周边琐事上.本文以Python为例,教大家如何快速打造完美的Python项目开发环境:内容涵盖了模块依赖管理 ...

  5. [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 ...

  6. python 解决递归调用栈溢出

    递归函数 2578次阅读 在函数内部,可以调用其他函数.如果一个函数在内部调用自身本身,这个函数就是递归函数. 举个例子,我们来计算阶乘n! = 1 x 2 x 3 x ... x n,用函数fact ...

  7. 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 ...

  8. 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 ...

  9. python通过get,post方式发送http请求和接收http响应的方法,pythonget

    python通过get,post方式发送http请求和接收http响应的方法,pythonget 本文实例讲述了python通过get,post方式发送http请求和接收http响应的方法.分享给大家 ...

随机推荐

  1. Python3-join()和split()

    Python join()方法 Python join()方法用于将序列中的元素以指定的字符连接生成一个新的字符串. #例如:列表 -- 字符串 str.join(sequence) 参数: sequ ...

  2. Linker Scripts3--SECTIONS Command

    1.前言 SECTIONS命令告诉链接器如何映射输入段到输出段以及在内存中如何放置输出段,SECTIONS命令的格式如下: SECTIONS { sections-command sections-c ...

  3. Linux系统启动那些事—基于Linux 3.10内核【转】

    转自:https://blog.csdn.net/shichaog/article/details/40218763 Linux系统启动那些事—基于Linux 3.10内核 csdn 我的空间的下载地 ...

  4. 无线桥接(WDS)如何设置?

    一.WDS使用介绍 无线桥接(WDS)可以将多台无线路由器通过无线方式互联,从而将无线信号扩展放大.无线终端在移动过程中可以自动切换较好的信号,实现无线漫游. 本文指导将TL-WR740N当作副路由器 ...

  5. hibernate框架学习第五天:数据查询、投影等

    复习day1环境搭建CRUD操作DB6个核心的APIday2TO PO DO 及其状态切换OID 自然主键 代理主键(uuid)一级缓存 Session绑定 load/get关系1对1 1对多(重点) ...

  6. xclip for windows

    下载源码和可执行文件 xclip.7z // The MIT License (MIT) // Copyright (c) 2014 Rapptz // Permission is hereby gr ...

  7. $Django 中间件 csrf

     中间件  -中间件是什么?请求和响应之间的一道屏障  -中间件作用:控制请求和响应  -django中内置几个中间件   process_request(self,request)   proces ...

  8. nginx或者squid正向代理实现受限网站的访问

    项目背景:公司商务同事需要操作合作方的后台,但是这个后台做了限制(安全考虑只对指定IP放行),刚好公司是adsl拨号,经常更换IP 需求:让商务同事不要经常给IP到合作方去添加白名单 于是想到了做正向 ...

  9. Light Oj 1003

    题意 : 给你m个二元关系, 问是否可以确定各个节点的先后关系: 思路: 拓扑排序, 判断是否有环: #include<bits/stdc++.h> using namespace std ...

  10. python 前面几个单词含义

    切片 str[start:end:step] start:从xxx开始    (startswith) end:切到xxx为止  (endswith) 不包括 字符串操作 .capitalize()# ...