Python Forum
AttributeError: 'Node' object has no attribute 'insert'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AttributeError: 'Node' object has no attribute 'insert'
#1
Quote:Get an error while insert a node to implement tree after insert node values what are missing and where
Error:
AttributeError Traceback (most recent call last) <ipython-input-35-5b7f867f337a> in <module> 228 root = Node(10) 229 --> 230 root.insert(5) 231 232 root.insert(30) AttributeError: 'Node' object has no attribute 'insert'
class Node: def __init__(self,insert=None, value=None): self.value = value self.left = None self.right = None def get_value(self): return self.value def set_value(self, value): self.value = value def has_left_child(self): return self.left is not None def get_left_child(self): return self.left def set_left_child(self, node): self.left = node def has_right_child(self): return self.right is not None def get_right_child(self): return self.right def set_right_child(self, node): self.right = node def _height(self, node, height=0): if self.root is None: return 0 return self._height(self.root, 0) if node is None: return height # Increment for this current node. height += 1 # If this node is a leaf, return the current height. if not node.has_left_child() and not node.has_right_child(): return height # To save some steps, we'll only get the child height if there is a child. left_height = 0 right_height = 0 if node.has_left_child(): left_height = self._height(node.get_left_child(), height) if node.has_right_child(): right_height = self._height(node.get_right_child(), height) return max(left_height, right_height) def _diameter(self, node, diameter=0): if self.root is None: return 0 return self._diameter(self.root, 0) if node is None: return diameter left_height = self._height(node.get_left_child()) right_height = self._height(node.get_right_child()) left_diameter = self._diameter(node.get_left_child()) right_diameter = self._diameter(node.get_right_child()) return max(left_height + right_height + 1, max(left_diameter, right_diameter)) tree = Tree() tree.set_root(9) tree.get_root().set_left_child(Node(8)) tree.get_root().set_right_child(Node(10)) tree.get_root().get_left_child().set_left_child(Node(7)) tree.get_root().get_right_child().set_right_child(Node(11)) tree.get_root().get_left_child().get_left_child().set_left_child(Node(6)) tree.get_root().get_left_child().get_left_child().set_right_child(Node(5)) tree.get_root().get_left_child().get_left_child().get_right_child().set_right_child(Node(4)) tree.get_root().get_left_child().get_left_child().get_right_child().get_right_child().set_left_child(Node(3)) tree.get_root().get_left_child().get_left_child().get_right_child().get_right_child().set_right_child(Node(2)) tree.get_root().get_left_child().get_left_child().get_right_child().get_right_child().get_right_child().set_right_child(Node(1)) ht = tree.height() if ht == 7: print('Yes, the height is 7.') else: print('Whoops, wrong height of {}, but should be 7.'.format(ht)) d = tree.diameter() if d == 9: print('Yes, the diameter is 9.') else: print('Whoops, wrong diameter of {}, but should be 9.'.format(ht)) # depth first seach recurion on BST with some application class BTree_struct: def __init__(self, key=None): self.root = None self.key = key self.left = None self.right = None def set_root(self, value,key): self.root = Node(value) self.key = key def get_root(self): return self.root def diameter(self): pass def insert_at_left(self, node): self.left = node def insert_at_right(self, node): self.right = node def search_elem(self, key): if self.key == key: return self if self.left is not None: temp = self.left.search(key) if temp is not None: return temp if self.right is not None: temp = self.right.search(key) return temp return None def dfs(self): print('entering {}...'.format(self.key)) if self.left is not None: self.left.dfs() print('at {}...'.format(self.key)) if self.right is not None: self.right.dfs() print('leaving {}...'.format(self.key)) btree_instance = None print('Menu (no duplicate keys)') print('insert <data> at root') print('insert <data> left of <data>') print('insert <data> right of <data>') print('dfs') print('quit') while True: my_input = input('What would you like to do? ').split() op = my_input[0].strip().lower() if op == 'insert': data = int(my_input[1]) new_node = BTree_struct(data) sub_op = my_input[2].strip().lower() if sub_op == 'at': btree_instance = new_node else: position = my_input[3].strip().lower() key = int(position) ref_node = None if btree_instance is not None: ref_node = btree_instance.search_elem(key) if ref_node is None: print('No such key.') continue if sub_op == 'left': ref_node.insert_at_left(node) elif sub_op == 'right': ref_node.insert_at_right(node) elif op == 'dfs': print('depth-first search traversal:') if btree_instance is not None: btree_instance.dfs() print() elif op == 'quit': break def insert(self, value): self.value = value self.left = None self.right = None if value == self.value: return False if value: if value < self.value: if self.left is None: self.left = Node(value) else: self.left.insert(value) elif value > self.value: if self.right is None: self.right = Node(value) else: self.right.insert(value) else: self.value = value """" 10 / \ 5 30 / \ / \ 8 9 20 40 """ # alternative way insert node leftsubtree as well rightsuntree root = Node(10) root.insert(5) root.insert(30) root.insert(20) root.insert(40) root.insert(8) root.insert(9)
Reply
#2
You have no def for insert in Node Class,

for example

 def insert(self, insert): print(insert) # replace it with your code
Reply
#3
@Axel_Erfurt yea it quit find exactly sure
Reply
#4
@Anldra12: As Axel mentioned: there is no method "insert()" in your class "Node". But there is also a class "BTree_struct" which does have such method.

But it is difficult to understand your code because there are many problems with the indentation. Line 31 (to 33) is not indented so the class definition ends there. But if I try to indent that part there is a mystery about the return statement on line 35. Does it belong to the the method "_height()"? If it does, what about the if statement on line 36 that will never be executed. And there are a lot of these problems. Must "BTree_struct" really by a class within class "Node"?

Please first correct the indentation of your code.
Anldra12 likes this post
Reply
#5
(May-11-2021, 09:03 AM)ibreeden Wrote: @Anldra12: As Axel mentioned: there is no method "insert()" in your class "Node". But there is also a class "BTree_struct" which does have such method.

But it is difficult to understand your code because there are many problems with the indentation. Line 31 (to 33) is not indented so the class definition ends there. But if I try to indent that part there is a mystery about the return statement on line 35. Does it belong to the the method "_height()"? If it does, what about the if statement on line 36 that will never be executed. And there are a lot of these problems. Must "BTree_struct" really by a class within class "Node"?

Please first correct the indentation of your code.

yeah the indentation error will be correct as well the return statement belong to height method 36 will be execute for me the code give me an error however trying to solve it
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  type object 'man' has no attribute 'centerX' Tempo 7 2,266 Mar-07-2025, 03:47 AM
Last Post: deanhystad
  The INSERT permission was denied on the object Steven5055 3 3,813 Jun-12-2024, 08:13 AM
Last Post: GregoryConley
  getpass.getpass() results in AttributeError: module 'os' has no attribute 'O_NOCTTY' EarthAndMoon 4 4,372 Oct-03-2023, 02:00 PM
Last Post: deanhystad
  AttributeError: '_tkinter.tkapp' object has no attribute 'username' Konstantin23 4 8,556 Aug-04-2023, 12:41 PM
Last Post: Konstantin23
  Python: Regex is not good for re.search (AttributeError: 'NoneType' object has no att Melcu54 9 5,130 Jun-28-2023, 11:13 AM
Last Post: Melcu54
  Parallel processing - AttributeError: Can't get attribute 'sktimekmeans' Mohana1983 1 2,977 Jun-22-2023, 02:33 AM
Last Post: woooee
  Python: AttributeError: 'PageObject' object has no attribute 'extract_images' Melcu54 2 8,828 Jun-18-2023, 07:47 PM
Last Post: Melcu54
  Object attribute behavior different in 2 scripts db042190 1 2,754 Jun-14-2023, 12:37 PM
Last Post: deanhystad
  cx_oracle Error - AttributeError: 'function' object has no attribute 'cursor' birajdarmm 1 6,799 Apr-15-2023, 05:17 PM
Last Post: deanhystad
  Pandas AttributeError: 'DataFrame' object has no attribute 'concat' Sameer33 5 12,693 Feb-17-2023, 06:01 PM
Last Post: Sameer33

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.