Both file is placed into same directory queueLinkedList.py and r6.py
queueLinkedList.py
class Empty(Exception):
pass
class LinkedQueue:
class _node: #which is nested class
__slots__ = '_element','_next'
def __init__(self,element,next):
self._element = element
self._next = next
def __init__(self):
self._head = None
self._tail = None
self._size = 0
def enqueue(self,element):
newest = self._node(element,None)
if self.is_empty():
self._head = newest
else:
self._tail._next = newest
self._tail = newest
self._size += 1
and
r6.py
from queueLinkedList import LinkedQueue,Empty
class LinkedQueue1(LinkedQueue):
def rotate(self):
if self._size > 1:
self._tail._next ,self._tail = self._head , self._head
p = self._head._next
self._tail._next = None
self._head = p
a = LinkedQueue1()
for i in range(5):
a.enqueue(i)
a.rotate()
I get this type of error:
Traceback (most recent call last):
File "c:/Users/ATMAN/Desktop/python2/Exercise_of_ds/chapter7/exercise/r7.py", line 28, in <module>
a.enqueue(i)
File "c:\Users\ATMAN\Desktop\python2\Exercise_of_ds\chapter7\exercise\queueLinkedList.py", line 31, in enqueue
newest = self._node(element,None)
File "c:\Users\ATMAN\Desktop\python2\Exercise_of_ds\chapter7\exercise\queueLinkedList.py", line 13, in __init__
self._element = element
AttributeError: '_node' object has no attribute '_element
why i can not use element of the nested class _node into inheritance class
if should i declare init() method in child class then tell how should i declare ?
Related
I can't figure out the type of error that I'm getting.
Traceback (most recent call last):
File "C:/Users/Eli/.PyCharmCE2019.2/config/scratches/BinarySearchTree.py", line 43, in
bt.printTreePreOrder(bt.Root())
TypeError: 'NoneType' object is not callable
class BinaryTreeNode:
def __init__(self,key, data = None):
self.key = key
self.value = data
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.Root = None
self.size = 0
def __len__(self):
return self.size
def root (self):
return self.Root
def insert(self,key,data):
new_node = BinaryTreeNode(key,data)
x = self.Root
y = None
while x != None:
y = x
if key < y.key:
x = x.left
else:
x = x.right
if y == None:
self. key < y.key
y.left = new_node
else:
y.right = new_node
self.size +=1
def printTreePreOrder(self,node):
print(node.key)
if node.left:
self.printTreePreOrder(node.left)
if node.right:
self.printTreePreOrder(node.right)
if __name__ == "__main__":
bt = BinarySearchTree()
bt.insert(12,'bill')
bt.insert(6,'Tom')
bt.insert(14,'jill')
bt.insert(3,'guy')
bt.printTreePreOrder(bt.Root())
You're attempting to call Root as if it were a function, but it's only a member variable of class BinaryTreeNode, therefore is not callable.
Furthermore, it is returning as NoneType because you initialize it to None and it is never assigned to any other value.
The way you have it implemented, you should do bt.printTreePreOrder(bt.Root)
The code is written is meant to calculate the number of nodes in a tree. However, I am getting a simple type error that keeps referring back to my TreeNode class. I have checked for spellings and typos, but I still can't find the error. Please help
Here is the error
Traceback (most recent call last):
File "C:/Users/Eli/.PyCharmCE2019.2/config/scratches/Tree.py", line 30, in <module>
t1.addChild(t1.Root(),6)
TypeError: 'TreeNode' object is not callable
class TreeNode():
def __init__(self, data= None, parent = None):
self.data = data
self.children = []
self.parent = parent
class Tree():
def __init__(self):
self.Root = None
self.size = 0
def __len__(self):
return self.size
def root (self):
return self.Root
def children (self, node):
return node.children()
def addChild (self,parent,data):
t = TreeNode (data,parent)
if parent == None:
self.Root = t
else:
parent.children.append(t)
self.size +=1
def printTreePreOrder(self,node):
print(node.data)
for n in node.children:
self.printTreePreOrder(n)
if __name__ == "__main__":
t1 = Tree()
t1.addChild(None,5)
t1.addChild(t1.Root(),6)
cl = t1.children(t1.Root())
for n in cl:
t1.addChild(n,3)
t1.addChild(n,9)
t1.printTreePr
t1.Root() should probably be t1.Root because your root is a TreeNode object instead of a function to find the root.
You will likely hit a similar wall when accessing node.children()
I'm a python/programming newb, and I am self-learning python. Unfortunately I'm really stuck! I'm in chapter 19 of "How to Think Like a Computer Scientist".
The object here is to write an implementation of a Priority Queue ADT, using a linked list.
I'm getting an error at lines 22 and 32. My understanding of this error, is that one or more of my node objects do not have the attribute "cargo". I don't understand this, as all of my node objects are assigned a cargo of "None", or some number, when they are created. The node object is written as it was given in the book.
I should mention, that LINE 31 works FINE. I only have a problem when I add LINE 32, which calls LINE 22 (which is apparently where the problem is hiding) that I get an error.
Any insight would be helpful. Thank you so much!!!
I reproduced the problem on a smaller scale:
class Node:
def __init__(self, cargo= None, next = None):
self.cargo = cargo
self.next = next
class Priority_Queue_For_Linked():
def __init__ (self):
self.max = None
self.nextmax = None
self.min = None
self.length = 0
def insert (self, cargo):
node = Node(cargo)
if self.length == 0:
node = self.max = self.min
self.length = 1
return
if node.cargo > self.max.cargo: # <--LINE 22
notmaxanymore = self.max
notmaxanymore.next = node
node = self.max
self.nextmax = notmaxanymore
self.length += 1
return
testlist = Priority_Queue_For_Linked()
testlist.insert(12)
testlist.insert(22) # <--LINE 32
And here is the error:
=== RESTART: /Users/Desktop/Programming Career/Untitled6.py ===
Traceback (most recent call last):
File "/Users/Desktop/Programming Career/Untitled6.py", line 31, in <module>
testlist.insert(22)# <--line 32
File "/Users/Desktop/Programming Career/Untitled6.py", line 21, in insert
if node.cargo > self.max.cargo:# <--line 22
AttributeError: 'NoneType' object has no attribute 'cargo'
I doubt you need more context, but in case it helps, here is all of the code:
class Node:
def __init__(self, cargo= None, next = None):
self.cargo = cargo
self.next = next
def __str__(self):
return str(self.cargo)
def print_backward (self):
if self.next != None:
tail = self.next
tail.print_backward()
print self.cargo,
def print_list(node):
while node:
print node,
node = node.next
print
class Priority_Queue_For_Linked():
def __init__ (self):
self.max = None
self.nextmax = None
self.min = None
self.length = 0
def is_empty (self):
if self.length == 0:
print "List is empty."
def insert (self, cargo):
node = Node(cargo)
if self.length == 0:
node = self.max = self.min
self.length = 1
return
if node.cargo > self.max.cargo:########40
notmaxanymore = self.max
notmaxanymore.next = node
node = self.max
self.nextmax = notmaxanymore
self.length += 1
return
if node.cargo < self.min.cargo:
notminanymore = self.min
node.next = notminanymore
node = self.min
self.length += 1
return
else:
comparisonnode = self.min.next
comparisonprev = self.min
for i in range (2, self.length):
if node.cargo < comparisonnode.cargo:
node.next = comparisonnode
comparisonprev.next = node
self.length += 1
break
comparisonnode = comparisonnode.next
comparisonprev = comparisonprev.next
def remove (self):
maxtoremove = self.max
self.max = self.nextmax
self.max.next = None
length -=1
return maxtoremove.cargo
def print_list(self):
tempnode = self.min
print "[",
while not tempnode == None:
print tempnode,
tempnode = tempnode.next
print "]"
testlist = Priority_Queue_For_Linked()
testlist.insert(12)
testlist.insert(22)#######86
When you insert your first item you have a problem in your assignments:
if self.length == 0:
node = self.max = self.min
self.length = 1
return
This will not change the value of self.max or self.min and they will stay as None, which of course won't have cargo. You also lose the node you are trying to add. I assume you wanted to do this:
self.max = self.min = node
The max attribute of the Priority_Queue_For_Linked class is defined as None in the __init__ method, and isn't changed before that if statement.
I presume you intended to assign a Node as the value of that attribute at some point, but you haven't done so.
You code tries to do self.max.cargo. In your __init__, you set self.max to None, and you never change this value anywhere else, so self.max is None. If you want to be able to use self.max, you need to set it to a real node value at some point.
I have the following three classes:
class DLLNode(object):
def __init__ (self, data, prev_node=None, next_node=None):
self.data=data
self.prev_node=prev_node
self.next_node=next_node
def __str__ (self):
return str(self.data)
class DLList(object):
def __init__(self):
self.head=None
self.tail=None
def add_to_head(self,add_obj):
newNode=DLLNode(add_obj)
if self.head==None:
self.head=self.tail=newNode
self.head.prev_node=self.tail.next_node=None
else:
self.head.prev_node=newNode
newNode.next_node=self.head
self.head=newNode
self.head.prev_node=None
def add_to_tail(self, add_obj):
newNode=DLLNode(add_obj)
if self.head==None:
self.head=self.tail=newNode
self.head.prev_node=self.tail.next_node=None
else:
self.tail.next_node=newNode
newNode.prev_node=self.tail
self.tail=newNode
self.tail.next_node=None
def remove_head(self):
if self.head==self.tail:
self.prev_node=self.next_node=self.head=self.tail=None
return
if self.head != self.tail:
self.head=self.head.next_node
self.head.prev_node=None
return self.head
def remove_tail(self):
if self.head==self.tail:
self.prev_node=self.next_node=self.head=self.tail=None
return
if self.head != self.tail:
self.tail=self.tail.prev_node
self.tail.next_node=None
return self.tail
def search (self, element):
current=self.head
if current == None:
return -1
else:
while current != None:
if current == None:
return -1
else:
if current.data==element:
return current.position
else:
current=current.next_node
class SortedList(object):
def __init__(self):
self.head=None
self.tail=None
def add (self, add_obj):
newNode=DLLNode(add_obj)
current=self.head
if current==None:
self.head=self.tail=newNode
else:
while add_obj>current.data:
current=current.next_node
newNode.next_node=current
newNode.prev_node=current.prev_node
current.prev_node.next_node=newNode
current.prev_node=newNode
def remove (self, element):
current=self.head
while element != current.data:
current=current.next_node
current.next_node.prev_node=current.prev_node
current.prev_node.next_node=current.next_node
current=None
def middle (self):
length=0
current=self.head
while current != None:
current=current.next_node
length =+ 1
headNode=self.head
tailNode=self.tail
if length/2%1:
while headNode != tailNode:
headNode=headNode.next_node
tailNode=tailNode.prev_node
return headNode
elif length/2%0:
tailnode=tailNode.prev_node
while headNode != tailNode:
headNode=headNode.next_node
tailNode=tailNode.prev_node
return headNode
I tried to add object to DLList and tried to search for it. And it brings me the follow error:
Traceback (most recent call last):
File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver\_sandbox.py", line 1, in <module>
# Used internally for debug sandbox under external interpreter
File "C:\Program Files (x86)\Wing IDE 101 5.1\src\debug\tserver\_sandbox.py", line 65, in search
builtins.AttributeError: 'DLLNode' object has no attribute 'position'
Your DLLNode class has no position attribute. Yet, near the end of your search function, you have the line return current.position. Python doesn't know what to do with that since there is no definition for it. You may wish to add some sort of counter to the function and increment it as you iterate and then return that.
I'm trying to create my own list type with a new class called TZList,
I tried to create it with a recursive __init__ func but it won't work,
here's the code:
class TZList:
def __init__(self, *args):
numargs = len(args)
self.value = None
self.next = None
if numargs == 0:
self.value = None
self.next = None
elif numargs == 1:
self.value = args[0]
self.next = None
else:
self.value = args[0]
numargs -= 1
args = args[1:]
self.next = TZList(args)
when i try to get the data like this:
t = TZList(1,2,3)
print(t.value)
print(t.next.value)
print(t.next.next.value)
i get a weird print:
Traceback (most recent call last):
1
(2, 3)
File "C:\****\a3.py", line 79, in <module>
print(t.next.next.value)
AttributeError: 'NoneType' object has no attribute 'value'
and i have no idea why, hope you could help me.
The reason is the way you are repassing args into TZList. You are passing it as a tuple. Instead of self.next = TZList(args) do self.next = TZList(*args)