-1

This error is occurring when we call the lookup method. Can anyone say how can it be rectified? I am unable to debug it using the available documents online. This is an implementation of a binary tree class. I know it is something related to the equivalence problem.

import deque

class Node:    
    def __init__(self, data):
        self.left = None
        self.right = None
        self.data = data

    def insert(self, data):    
        if data < self.data:
            if self.left is None:
                self.left = Node(data)
            else:
                self.left.insert(data)
        else:
            if self.right is None:
                self.right = Node(data)
            else:
                self.right.insert(data)  

    def lookup(self, data, parent=None):    
        if self.data == data:
            return self, parent
        if data < self.data:
            if self.left is None:
                return None
            else:
                return self.left.lookup(data, parent=self)
        else:
            if self.right is None:
                return None
            else:
                return self.right.lookup(data, parent=self)

    # aka bfs traversal
    def level_traversal(self):    
        root = self
        dq = deque()
        dq.append(root)
        while dq:
            root = dq.popleft()
            if root.left:
                dq.append(root.left)
            if root.right:
                dq.append(root.right)
            print (root.data)



    def delete(self, data):
        node, parent = self.lookup(data)
        if node.children_count() == 0:
            if parent.left == node:
                parent.left = None
            else:
                parent.right = None
            del node
        elif node.children_count() == 1:
            if node.left:
                n = node.left
            else:
                n = node.right
            if parent:
                if parent.left == node:
                    parent.left = n
                else:
                    parent.right = n
            del node
        else:
            # find the successor
            parent = node
            successor = node.right
            while successor.left:
                parent = successor
                successor = successor.left

            node.data = successor.data
            if parent.left == successor:
                parent.left = successor.right
            else:
                parent.right = successor.right

    def inorder(self):    
        if self.left:
            self.left.inorder()

        print (self.data)

        if self.right:
            self.right.inorder()

    def preorder(self):    
        print (self.data)
        if self.left:
            self.left.preorder()
        if self.right:
            self.right.preorder()

    def postorder(self):    
        if self.left:
            self.left.postorder()
        if self.right:
            self.right.postorder()
        print (self.data)


root = Node(8)
root.insert(3)
root.insert(10)
root.insert(1)
root.insert(6)
root.insert(4)
root.insert(7)
root.insert(14)
root.insert(13)

# look up
print (root.lookup(6))
# level traversal
root.level_traversal()
#mirror image
#root.mirror_image()
#root.delete(3)
#root.level_traversal()

# inorder
#root.inorder()
# pre order
#root.preorder()
# postorder
#root.postorder()
# size
#root.size()
#root.dfs()
#print root.height()
PM 2Ring
  • 54,345
  • 6
  • 82
  • 182
Hai Hai
  • 23
  • 7

2 Answers2

3

This is not an error at all. This happens because you are returning a tuple of objects from your lookup method, and this is just how objects are represented when you print them out. If you don't like this, you can overwrite the __repr__() method.

b_pcakes
  • 2,452
  • 3
  • 28
  • 45
  • I have edited my answer to include a link to the Python docs for __repr__(). Read it and see if you can figure it out yourself. It's always a good idea to try it yourself first before asking for help :) – b_pcakes Aug 29 '15 at 04:37
  • bro i am running out of time.at least please show me some examples so that i can easily understand – Hai Hai Aug 29 '15 at 04:40
  • @HaiHai: Please take a look at one of the _many_ existing questions on this topic, eg [Purpose of Python's `__repr__`](http://stackoverflow.com/q/1984162/4014959) – PM 2Ring Aug 29 '15 at 04:43
  • 1
    @HaiHai The documentation is pretty clear, is it not? You must return a string from the `__repr__()` method. For example, `return str(self.data)` – b_pcakes Aug 29 '15 at 04:50
  • @HaiHai: Take a look at [this example](http://stackoverflow.com/a/28605377/4014959) I wrote a while ago, which has examples of `__str__()` and `__repr__()`. Or you might like to try this, which I use in my own binary tree class: `def __repr__(self):` `return '(%s, %r, %r)' % (self.data, self.left, self.right)` – PM 2Ring Aug 29 '15 at 05:00
  • i already told you that i am a beginner in python.just studying the concepts of class – Hai Hai Aug 29 '15 at 05:06
  • Yes, we understand that you're a beginner. So just paste that `__repr__(self)` function from my last comment into your Node class _and see what happens_ when you run the code! – PM 2Ring Aug 29 '15 at 05:11
0

Try this in your class definition.

Definition for a binary Tree Node

class TreeNode:
  def __init__(self, val = 0, left = None, right = None):
    self.val = val
    self.left = left
    self.right = right

  ## print TreeNode Object
  def __repr__(self) -> str:
      return '[%s, %r, %r]' % (self.val, self.left, self.right)
SL4566
  • 132
  • 1
  • 7