简体   繁体   中英

build the binary tree algorithm with binary elements

i want to build a binary tree algorithm in python based on a txt file which contains binary element producted randomly that reflects a table of ip addresses, but when i run my code there is an error message :

"D:\Nouveau dossier\projectpy\venv\Scripts\python.exe" C:/Users/balala/.PyCharmCE2018.1/config/scratches/ARB/Binary.py
Traceback (most recent call last):
  File "C:/Users/balala/.PyCharmCE2018.1/config/scratches/ARB/Binary.py", line 66, in <module>
    ab.constructTree(sr)
  File "C:/Users/balala/.PyCharmCE2018.1/config/scratches/ARB/Binary.py", line 35, in constructTree
    self.RC.RC.constructTree(k)
AttributeError: 'NoneType' object has no attribute 'constructTree'

Process finished with exit code 1

my code source :

import os
from random import randrange


class BinTree:
    def __init__(self,val=" "):
        self.RC=None    #le fils droit
        self.LC=None   # le fils gauche
        self.key=val    # la cle

    def getRC(self):
            return self.RC
    def getLC(self):  # retourner le noeud gauche
        return self.LC
    """
    """
    def setkey(self,Val):
        self.key=Val
        """
        """
    def getkey(self):
        return self.key


    def constructTree(self, k=" "):
        if self is None:
            self=BinTree(k)
        elif k == "0":
             if self.LC is None:
                 self.LC=BinTree(k)
             self.LC.LC.constructTree(k)
        elif k=="1":
            if self.RC is None:
               self.RC = BinTree(k)
            self.RC.RC.constructTree(k)

    def printTree(self):
      if self:
          print(self.key,end="")
          if self.LC:
            self.LC.printARB()
          if self.RC:
             self.RC.printARB()

"""racine=BinArb(2)
racine.inserkey(3)
racine.inserkey(1)
racine.inserkey(4)
racine.inserkey(0)
racine.printARB()"""
ab=BinTree(0)
file1=open("bin1.txt", "w")
for i in range(0,6):
 for i in range(0,randrange(1,7)):
    l=str(randrange(0,2))
    s=l+ " "
    file1.writelines(s)
 j= " \n"
 file1.writelines(j)
file1.close()
file2=open("bin1.txt","r")
for line in file2:
    lis=line.split()
    for l in range(0,len(lis)):
        sr=str(lis[l])
        ab.constructTree(sr)

file2.close()
ab.printARB()

You have a typo on line 35:

self.RC.RC.constructTree(k)

This should probably be:

self.RC.constructTree(k)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM