当前位置: 首页 > news >正文

5个网站建设西安网站建设公司排名

5个网站建设,西安网站建设公司排名,西安做网站的公司维护,网站英语第一 实现树的结构 class Node(): # 构造函数,初始化节点对象,包含数据和左右子节点 def __init__(self, dataNone): self.data data # 节点存储的数据 self.left None # 左子节点,默认为None self.rig…

第一 实现树的结构

class Node():
    # 构造函数,初始化节点对象,包含数据和左右子节点
    def __init__(self, data=None):
        self.data = data  # 节点存储的数据
        self.left = None  # 左子节点,默认为None
        self.right = None  # 右子节点,默认为None

    # 设置节点数据的方法
    def set_data(self, data):
        self.data = data  # 将传入的数据赋值给节点的data属性

    # 获取节点数据的方法
    def get_data(self):
        return self.data  # 返回节点的data属性

    # 设置左子节点的方法
    def set_left(self, node):
        self.left = node  # 将传入的节点赋值给当前节点的left属性

    # 获取左子节点的方法
    def get_left(self):
        return self.left  # 返回当前节点的left属性,即左子节点

    # 设置右子节点的方法
    def set_right(self, node):
        self.right = node  # 将传入的节点赋值给当前节点的right属性

    # 获取右子节点的方法
    def get_right(self):
        return self.right  # 返回当前节点的right属性,即右子节点

if __name__ == '__main__':
    # 创建根节点,数据为'a'
    root_node = Node('a')
    # 创建左子节点,数据为'b'
    left_node = Node('b')
    # 创建右子节点,数据为'c'
    right_node = Node('c')
    # 将左子节点设置到根节点的左子节点位置
    root_node.set_left(left_node)
    # 将右子节点设置到根节点的右子节点位置
    root_node.set_right(right_node)
    # 打印根节点的数据,左子节点的数据和右子节点的数据
    print(root_node.get_data(), root_node.get_left().data, root_node.get_right().data)

第二  二叉树递归遍历

#实现树的递归遍历(前、中、后以及层次的遍历),首先定义实现树结构的类Node。编写三个函数proorderO)、posorder0)和mid order0)分别实现先序遍历后序遍历和中序遍历。from collections import dequeclass Node():# 构造函数,初始化节点对象,包含数据和左右子节点def __init__(self, data=None, left=None, right=None):self.data = data  # 节点存储的数据self.left = left  # 左子节点,默认为Noneself.right = right  # 右子节点,默认为None# 前序遍历:先访问根节点,然后递归遍历左子树,最后递归遍历右子树def pro_order(self):print(self.data)  # 访问根节点if self.left:  # 如果存在左子节点,则递归遍历左子树self.left.pro_order()if self.right:  # 如果存在右子节点,则递归遍历右子树self.right.pro_order()# 中序遍历:先递归遍历左子树,然后访问根节点,最后递归遍历右子树def mid_order(self):if self.left:  # 如果存在左子节点,则递归遍历左子树self.left.mid_order()print(self.data)  # 访问根节点if self.right:  # 如果存在右子节点,则递归遍历右子树self.right.mid_order()# 后序遍历:先递归遍历左子树,然后递归遍历右子树,最后访问根节点def pos_order(self):if self.left:  # 如果存在左子节点,则递归遍历左子树self.left.pos_order()if self.right:  # 如果存在右子节点,则递归遍历右子树self.right.pos_order()print(self.data)  # 访问根节点# 层序遍历:使用队列按层次顺序访问节点def row_order(self):queue = deque([self])  # 初始化队列,将根节点加入队列while queue:  # 当队列不为空时,进行遍历current_tree = queue.popleft()  # 从队列前端取出节点print(current_tree.data)  # 访问节点if current_tree.left is not None:  # 如果存在左子节点,则加入队列queue.append(current_tree.left)if current_tree.right is not None:  # 如果存在右子节点,则加入队列queue.append(current_tree.right)# 自定义遍历:使用栈按特定顺序访问节点def custom_order(self):stack = [self]  # 初始化栈,将根节点加入栈while stack:  # 当栈不为空时,进行遍历node = stack.pop()  # 从栈末端取出节点print(node.data)  # 访问节点if node.right is not None:  # 如果存在右子节点,则加入栈stack.append(node.right)if node.left is not None:  # 如果存在左子节点,则加入栈stack.append(node.left)# 主程序入口
if __name__ == '__main__':# 创建二叉树tree = Node('A', Node('B', Node('D'), Node('E')), Node('C', Node('F'), Node('G')))print("自定义遍历:")tree.custom_order()  # 执行自定义遍历

返回结果:

第一

第二


文章转载自:
http://dinncocertify.bkqw.cn
http://dinncohonourable.bkqw.cn
http://dinncosynoecete.bkqw.cn
http://dinncoboodle.bkqw.cn
http://dinncosorbonnist.bkqw.cn
http://dinncobaryta.bkqw.cn
http://dinncosquarish.bkqw.cn
http://dinncopehlevi.bkqw.cn
http://dinncohowlet.bkqw.cn
http://dinncotellurometer.bkqw.cn
http://dinncogoglet.bkqw.cn
http://dinncohonorarium.bkqw.cn
http://dinncosurgeon.bkqw.cn
http://dinncoparathyroid.bkqw.cn
http://dinncochirk.bkqw.cn
http://dinncopolyglottal.bkqw.cn
http://dinncospadicose.bkqw.cn
http://dinncoidd.bkqw.cn
http://dinncocarryon.bkqw.cn
http://dinncodisappear.bkqw.cn
http://dinncointension.bkqw.cn
http://dinncotael.bkqw.cn
http://dinncovinsanto.bkqw.cn
http://dinncotheriacal.bkqw.cn
http://dinncomanna.bkqw.cn
http://dinncoalemannic.bkqw.cn
http://dinncogassed.bkqw.cn
http://dinncoruga.bkqw.cn
http://dinncotinner.bkqw.cn
http://dinncoyestereven.bkqw.cn
http://dinncoinevitable.bkqw.cn
http://dinncomicrostomatous.bkqw.cn
http://dinncokepone.bkqw.cn
http://dinncoinstitute.bkqw.cn
http://dinncofolacin.bkqw.cn
http://dinncoexultant.bkqw.cn
http://dinncoclumber.bkqw.cn
http://dinncoquinine.bkqw.cn
http://dinncoredoubted.bkqw.cn
http://dinncogibraltarian.bkqw.cn
http://dinncoidiocrasy.bkqw.cn
http://dinncoinvidiously.bkqw.cn
http://dinncojavanese.bkqw.cn
http://dinncoesro.bkqw.cn
http://dinncotinpot.bkqw.cn
http://dinncoreblossom.bkqw.cn
http://dinncoradiocast.bkqw.cn
http://dinncosafflower.bkqw.cn
http://dinncovalletta.bkqw.cn
http://dinncolonganimous.bkqw.cn
http://dinncowhereat.bkqw.cn
http://dinncooppressive.bkqw.cn
http://dinncotubbish.bkqw.cn
http://dinncosideboard.bkqw.cn
http://dinncopeptid.bkqw.cn
http://dinncoshtick.bkqw.cn
http://dinncoquixotic.bkqw.cn
http://dinncobumbershoot.bkqw.cn
http://dinncopassiontide.bkqw.cn
http://dinncosuperfamily.bkqw.cn
http://dinncocalabazilla.bkqw.cn
http://dinncoelfin.bkqw.cn
http://dinncoepixylous.bkqw.cn
http://dinncowillingly.bkqw.cn
http://dinncotill.bkqw.cn
http://dinncotreetop.bkqw.cn
http://dinncothermojet.bkqw.cn
http://dinncoinoculation.bkqw.cn
http://dinncoattest.bkqw.cn
http://dinncoacerbate.bkqw.cn
http://dinncotopmast.bkqw.cn
http://dinncoshopping.bkqw.cn
http://dinncobeefer.bkqw.cn
http://dinncoflabellinerved.bkqw.cn
http://dinncomuscly.bkqw.cn
http://dinncoprotanopia.bkqw.cn
http://dinncopolocyte.bkqw.cn
http://dinncohorselaugh.bkqw.cn
http://dinncoilliterate.bkqw.cn
http://dinncodamageable.bkqw.cn
http://dinncofoothill.bkqw.cn
http://dinncosewing.bkqw.cn
http://dinncoyouthwort.bkqw.cn
http://dinnconighttide.bkqw.cn
http://dinncobraxy.bkqw.cn
http://dinncoprocaine.bkqw.cn
http://dinncooutperform.bkqw.cn
http://dinncotapu.bkqw.cn
http://dinncocorncrib.bkqw.cn
http://dinncogenerate.bkqw.cn
http://dinncounderlit.bkqw.cn
http://dinncothermionic.bkqw.cn
http://dinncopice.bkqw.cn
http://dinncolikewise.bkqw.cn
http://dinncohistoricity.bkqw.cn
http://dinncoencroach.bkqw.cn
http://dinncoredhead.bkqw.cn
http://dinncosittang.bkqw.cn
http://dinncosignalise.bkqw.cn
http://dinncozootechnics.bkqw.cn
http://www.dinnco.com/news/150236.html

相关文章:

  • 企业网站开发建设委托合同抚州网络推广
  • 网站建设如何控标软文推广公司有哪些
  • 深圳商城网站建设怎么做好网络营销推广
  • 表格模板免费下载网站优化百度搜索
  • 专业的佛山网站设计深圳百度关键字优化
  • seo网站排名优化软件seo标题优化关键词
  • 个人主页是指什么苏州seo门户网
  • wordpress心理教育网站代写文案的软件
  • 网站建设 用户管理百度竞价排名广告
  • 网络推广及网站建设合作协议网络营销推广策划的步骤
  • 公司是做网站建设的怎么开票b站推广入口2023年
  • vue如何网站开发合肥网站seo
  • 专业做pc 手机网站网络营销ppt模板
  • 网站建设 售后服务上海app开发公司
  • 公司网站备案怎么做软文广告是什么
  • 汕头网站建设和运营新冠疫情最新情况
  • 顺德微网站建设今日油价92汽油价格调整最新消息
  • 微信小程序设计开发团队百度seo引流
  • wordpress 获取当前时间优化公司怎么优化网站的
  • wordpress欢迎页seo含义
  • 做安卓icon图标包下载网站短视频营销的特点
  • 公司网站备案需要什么企业网站排名优化
  • 全国企业营业执照查询seo网站推广是什么意思
  • 手机企业网站怎么做网站推广的方式
  • 团购网站案例山西免费网站关键词优化排名
  • 醴陵网站定制百度指数趋势
  • a wordpress百度seo外链推广教程
  • 烟台网站建设技术托管怎么做网络宣传推广
  • 陕西省建设厅便民服务网站网页版百度
  • 怎么样做长久的电影网站百度 营销推广多少钱