数据结构——二叉树

二叉链表

class BTnode:
   def __init__(self,sj,lc=None,rc=None):
      self._data=sj
      self._Lchild=lc
      self._Rchild=rc
def createBT():
   ch=eval(input("input a data="))
   if ch==-1:
      return None
   t=BTnode(ch)
   t._Lchild=createBT()
   t._Rchild=createBT()
   return t
def BTleafdisp(t): #t代表根结点标识
   if(t==None):
      return
   if(t._Lchild==None and t._Rchild==None):
      print(t._data)
   BTleafdisp(t._Lchild)
   BTleafdisp(t._Rchild)
T=createBT()
BTleafdisp(T)
二叉树的遍历
class TNode:
   def __init__(self,s,j,Lc=None,Rc=None):
      self._data=sj
      self._Lchild=Lc
      self.Rchild=Rc
   def show(self):
      print(self._data,end="")
def CreateBinTree():
   ch=eval(input("input data="))
   if ch==9:
      return None #读入#时,将相应结点指针置空
   else:
      t=TNode(ch)
      t._Lchild=CteateBinTree()
      t._Rchild=CteateBinTree()
   return t
n1=0 #总结点数
n2=0 #叶子结点总数
def previsit(t):
   global n1
   global n2
   if t==None:
      return
   n1=n1+1
   if(t._Lchild==None and t._Rchild==None):
      print(t._data)
      n2=n2+1
   previsit(t._Lchild)
   previsit(t._Rchild)
t=CreateBinTree()
previsit(t)
print("total code=",n1,"Leaves code=",n2)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值