2013-09-24 2 views
0

내가 이진 검색 트리 라이브러리를 구축하기 위해 노력하고있어 나는 코드의 내 조각에 구문 오류 받고 있어요 :볼 수없는 구문 오류

class Node: 
    """ 
    Tree node: Left and Right child + data which can be any object 
    """ 
    def __init__(self,data): 
     """ 
     Node constructor 
     """ 
     self.left = None 
     self.right = None 
     self.data = data 

def insert(self,data):      # self --> object self.data 
    """ 
    Insert new node with data 
    """ 
    if data < self.data:     # insert data less than current root 
     if self.left is None:    # if left node is empty, 
      self.left = Node(data)   # object left --> Node with new data 
     else: 
      self.left.insert(data)   # if left node is not empty, go insert again 
    else: 
     if self.right is None:    # insert data greater than current node 
      self.right = Node(data)  # object right --> Node with new data 
     else: 
      self.right.insert(data)  # if not empty, run insert again 


def lookup(self, data, parent = None): 
    """ 
    Lookup node containing data 
    """ 
    if data < self.data: 
     if self.left is None: 
      return None, None 
     return self.left.lookup(data,self) 
    elif data > self.data: 
     if self.right is None: 
      return None, None 
     return self.right.lookup(data,self) 
    else: 
     return self, parent 

def delete(self, data): 
    """ 
    delete node containing data 
    """ 

    """ no child """ 
    if children_count == 0: 
     # if node has no children, remove it 
     if parent.left is node:  # if parent is pointing to current node 
      parent.left = None   # set parent pointing left to None 
     else: 
      parent.right = None  # else set parent pointing right to None 
     del node      # del node 

    """ 1 child """  
    elif children_count == 1: 
     # if node has 1 child 
     # replace node by it's child 
     if node.left: 
      n = node.left 
     else: 
      n = node.right 
     if parent: 
      if parent.left is node: 
       parent.left = n 
      else: 
       parent.right = n 
     del node 

    """ 2 children """ 
    else: 
     # if node has 2 children 
     # find its successor 
     parent = node          # parent is equal to current node target of deletion 
     successor = node.right        # successor is right of the node 
     while successor.left:         
      parent = successor        
      successor = successor.left 
     # replace node data by its successor data 
     node.data = successor.data 
     #fix successor's parent's child 
     if parent.left == successor: 
      parent.left = successor.right 
     else: 
      parent.right = successor.right 


def children_count(self): 
    """ Return the number of children """ 
    if node is None: 
     return None 
    cnt = 0 
    if self.left: 
     cnt += 1 
    if self.right: 
     cnt += 1 
    return cnt 


# method to print tree in order. Use recursion inside print_tree() to walk the tree breath-first 

def print_tree(self): 
    if self.left: 
     self.left.print_tree() 
    print self.data, 
    if self.right: 
     self.right.print_tree() 

오류는 다음과 같습니다

Traceback (most recent call last): 
    File "<pyshell#10>", line 1, in <module> 
    import BSTlib 
    File "BSTlib.py", line 78 
    elif children_count == 1: 
    ^

나는 틀린 것을 보지 못합니다. = [누군가 나를 지적하도록 도울 수 있습니까? 고맙습니다!

+2

수 : 당신이 언급해야하는 경우

if False: print 'hi' elif True: print 'sdf' 

, 그럼 그냥 해시 #를 사용

여분의 공백을 문서화 문자열을 제거 (또는 이동) 제거 오류가 발생한 행 위의 코드 상단에 몇 줄을 추가 하시겠습니까? – jedwards

+1

코드 **를 ** line 78까지 공유해야합니다.이 내용을 [Short Self Contained Correct Example] (http://sscce.org/)로 분해 해보십시오. 과정에서 직접 해결하십시오.) – Johnsyweb

+1

상단에 줄이 더 이상 없다면 말할 수는 없지만 엘프 위의 줄에 닫히지 않은 괄호가있는 것처럼 보입니다. 파이썬은 마치 같은 표현식의 일부인 것처럼 다음 줄을 구문 분석하려고 시도 할 때 오류가 발생하고 오류가 발생하기 때문에 일반적으로 이런 종류의 오류가 발생합니다. – reem

답변

2

이것은 문서화 문자열 및 공백 문자와 관련된 오류입니다. 이 코드를 실행하십시오 :

if False: 
    print 'hi' 

"""1 child""" 
elif True: 
    print 'sdf' 

그리고 비슷한 SyntaxError가 나타납니다. 당신에게

if False: 
    print 'hi' 
# 1 child 
elif True: 
    print 'sdf' 
+0

입력 해 주셔서 감사합니다. 죄송합니다. – Liondancer

+1

@Liondancer 예, 지금 오류가 있습니다. 나는 나의 대답을 편집했다 – TerryA

+0

고맙다! 이해 했어! 내 코드가 나에게 잘 어울린다고 생각했기 때문에 나에게 많은 시간을 절약 해 주었다. – Liondancer

관련 문제