Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1743598
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2015-05-09 12:44:33

List of list presentation:

点击(此处)折叠或打开

  1. def BinaryTree(r):
  2.     return [r,[],[]]


  3. def insertLeft(root,newBranch):
  4.     t=root.pop(1)
  5.     if len(t) > 1:
  6.         root.insert(1,[newBranch,t,[]])
  7.     else:
  8.         root.insert(1,[newBranch,[],[]])
  9.     return root


  10. def insertRight(root,newBranch):
  11.     t=root.pop(2)
  12.     if len(t) > 1:
  13.         root.insert(2,[newBranch,[],t])
  14.     else:
  15.         root.insert(2,[newBranch,[],[]])

  16.     return root

  17. def getRootval(root):
  18.     return root[0]

  19. def setRootVal(root,newVal):
  20.     root[0]=newVal

  21. def getLeftChild(root):
  22.     return root[1]

  23. def getRightChild(root):
  24.     return root[2]

用类的实现

点击(此处)折叠或打开

  1. class BinaryTree:
  2.     def __init__(self,rootObj):
  3.         self.key=rootObj
  4.         self.leftChild=None
  5.         self.rightChild=None

  6.     def insertRight(self,newNode):
  7.         if self.rightChild==None:
  8.             self.rightChild=BinaryTree(newNode)
  9.         else:
  10.             t=BinaryTree(newNode):
  11.             t.rightChild=self.rightChild
  12.             self.rightChild=t

  13.     def insertLeft(self,newNode):
  14.         if self.leftChild==None:
  15.             self.leftChild=BinaryTree(newNode)
  16.         else:
  17.             t=BinaryTree(newNode)
  18.             t.leftChild=self.leftChild
  19.             self.leftChild=t

  20.     def getRightChild(self):
  21.         return self.rightChild
  22.     
  23.     def getLeftChild(self):
  24.         return self.leftChild

  25.     def setRootVal(self,obj):
  26.         self.key=obj

  27.     def getRootVal(self):
  28.         return self.key


阅读(2220) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~