How to call a class method using an instance of a different class in Python? - python

I am a trying to implement a binary tree using two classes - Node and Binary Tree. When I am inserting the nodes (left or right), I am using the methods insert_left_node and insert_right_node which are class BinaryTree's methods, but I am also using class Node to create a node. After every node insertion, the current object is returned.
Now, how do I call class BinaryTree's insertion methods using the returned object - current. E.g. In the second last line of the code, statement n3 = n1.insert_left_node(33) fails with AttributeError: 'Node' object has no attribute 'insert_left_node'
I need an alternative way to achieve this.
Code:
class Node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BinaryTree(object):
def __init__(self, root=None):
self.root = Node(root)
def insert_left_node(self, data):
if not self.root:
self.root = Node(data)
else:
current = self.root
while True:
if current.left:
current = current.left
else:
current.left = Node(data)
break
return current
def insert_right_node(self, data):
if not self.root:
self.root = Node(data)
else:
current = self.root
while True:
if current.right:
current = current.right
else:
current.right = Node(data)
break
return current
if __name__ == '__main__':
r = BinaryTree(34) # root
n1 = r.insert_left_node(22)
n2 = r.insert_right_node(45)
n3 = n1.insert_left_node(33) # Fails
print n3

Your request literally doesn't make any sense. To achieve what you want you should just add the needed methods to the class you want to use. Try something similar to the following:
class Node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def insert_left_node(self, data):
self.left = Node(data)
def insert_right_node(self, data):
self.right = Node(data)

Related

binary search tree missing 1 required positional argument:

Trying to construct the Binary search tree here
class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BSTree():
def __init__(self, rootdata):
self.root = Node(rootdata)
def insert(self, data, cur_node):
if data < cur_node.data:
if cur_node.left == None:
cur_node.left = Node(data)
else:
self.insert(data, cur_node.left)
elif data > cur_node.data:
if cur_node.right == None:
cur_node.right = Node(data)
else:
self.insert(data, cur_node.right)
else:
print("Duplicate value!")
def find(self, data, cur_node):
if data < cur_node.data and cur_node.left:
return self.find(data, cur_node.left)
elif data > cur_node.data and cur_node.right:
return self.find(data, cur_node.right)
if data == cur_node.data:
return True
return False
def PreOder(self,root):
if root == None:
pass
else:
print(root.data)
self.PreOrder(root.left)
self.PreOrder(root.right)
a = BSTree()
a.insert(3)
a.insert(4)
a.insert(7)
a.insert(34)
a.insert(24)
a.insert(2)
a.insert(49)
print(a.find(3))
print(a.PreOrder(3))
I am getting an error message: init() missing 1 required positional argument: 'rootdata'
How to fix and print the binary search tree?
Also, what I have up there is just the random number I am try to construct the binary tree out from the list I have
mylist = [1,3,2,4,12,14,23,43,23,44,34,43]
Here is a working and properly formatted update of your code. Not sure what it's doing exactly, but it should give you some clues to solve your task. Maybe you could use an IDE like Visual Studio Code or Pycharm to help you out with python specific stuff.
class Node:
# Constructor to create a new node
def __init__(self, data):
self.data = data
self.left = None
self.right = None
class BSTree:
def __init__(self, rootdata):
self.root = Node(rootdata)
def insert(self, data, cur_node):
if data < cur_node.data:
if cur_node.left is None:
cur_node.left = Node(data)
else:
self.insert(data, cur_node.left)
elif data > cur_node.data:
if cur_node.right is None:
cur_node.right = Node(data)
else:
self.insert(data, cur_node.right)
else:
print("Duplicate value!")
def find(self, data, cur_node):
if data < cur_node.data and cur_node.left:
return self.find(data, cur_node.left)
elif data > cur_node.data and cur_node.right:
return self.find(data, cur_node.right)
if data == cur_node.data:
return True
return False
def pre_order(self, root):
if root is None:
pass
else:
print(root.data)
self.pre_order(root.left)
self.pre_order(root.right)
a = BSTree(3)
a.insert(4, a.root)
a.insert(7, a.root)
a.insert(34, a.root)
a.insert(24, a.root)
a.insert(2, a.root)
a.insert(49, a.root)
print(a.find(3, a.root),)
print(a.pre_order(a.root))
Some issues:
Your BSTree constructor does not create an emtpy tree, but a tree with already one node, and so it expects the data for that node as argument. This design is not good, because it does not support the concept of an empty tree. So the constructor should change and just set self.root to None.
insert takes also more arguments than expected: it does so because it uses that argument for implementing recursion. There are many ways to make it work, but I prefer that the recursive call is made in OOP-style, i.e. the recursive insert method should be placed in the Node class, and act on self instead of on an extra argument. The insert method on the BSTree class can then just be a wrapper around that method, where it gets called on the root node.
A similar issue occurs with find. The solution can be the same as for insert.
And again, the issue occurs also with preOrder: it takes a node as argument. Same solution as discussed above. I would also avoid printing in a method. Instead yield the values.
So here is how that would look:
class Node:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def insert(self, data):
if data < self.data:
if not self.left:
self.left = Node(data)
else:
self.left.insert(data)
elif data > self.data:
if not self.right:
self.right = Node(data)
else:
self.right.insert(data)
else:
print("Duplicate value!")
def find(self, data):
if data < self.data and self.left:
return self.left.find(data)
elif data > self.data and self.right:
return self.right.find(data)
return data == self.data
def preOrder(self):
yield self.data
if self.left:
yield from self.left.preOrder()
if self.right:
yield from self.right.preOrder()
class BSTree():
def __init__(self):
self.root = None
def insert(self, data):
if not self.root:
self.root = Node(data)
else:
self.root.insert(data)
def find(self, data):
return self.root.find(data) if self.root else False
def preOrder(self):
if self.root:
yield from self.root.preOrder()
a = BSTree()
a.insert(3)
a.insert(4)
a.insert(7)
a.insert(34)
a.insert(24)
a.insert(2)
a.insert(49)
print(a.find(3))
print(*a.preOrder()) # With * we splash every yielded value as argument to print().

Python - Removing a node from a linked-list at beginning, in between and at end

I learned about linked-lists today. I learned how to insert and remove nodes from them. In the following code, it teaches me how to insert nodes with three distinct functions: at beginning (the head node), in between and at the end. However, they teach me how to remove the node in and single function. I don't find the code in the remove function to be very clear. Can anyone help make an easier to understand code under the remove function? Is there a way to make it into three functions just like the inserting ones? I'm open to any suggestion or explanation. Thanks in advance.
Here's my code:
class Node:
def __init__(self, data=None):
self.data = data
self.nextnode = None
class LinkedList:
def __init__(self):
self.headnode = None
def printlist(self):
node = self.headnode
while node is not None:
print (node.data)
node = node.nextnode
def atbegining(self,new_node):
new_node.nextnode = self.headnode
self.headnode = new_node
# Function to add newnode
def AtEnd(self, newnode):
if self.headnode is None:
self.headnode = newnode
return
node = self.headnode
while(node.nextnode):
node = node.nextnode
node.nextnode=newnode
# Function to add node
def Inbetween(self,preNode,newNode):
if preNode is None:
print("The mentioned node is absent")
return
newNode.nextnode = preNode.nextnode
preNode.nextnode = newNode
# Function to remove node
def RemoveNode(self, RemoveVal):
node = self.headnode
if (node is not None):
if (node.data == RemoveVal):
self.headnode = node.nextnode
node = None
return
while (node is not None):
if node.data == RemoveVal:
break
prevnode = node
node = node.nextnode
if (node == None):
return
prevnode.nextnode = node.nextnode
node = None
list1 = LinkedList()
list1.headnode = Node("Mon")
n2 = Node("Tue")
n3 = Node("Wed")
# Link first Node to second node
list1.headnode.nextnode = n2
# Link second Node to third node
n2.nextnode = n3
n4 = Node("Sun")
n5 = Node("Tur")
n6 = Node("Newdate")
list1.atbegining(n4)
list1.AtEnd(n5)
list1.Inbetween(list1.headnode,n6)
list1.RemoveNode("Newdate")
list1.printlist()
RemoveNode is complicated by the fact that there are two structurally distinct kinds of LinkedLists: one whose head is None, and one whose head is not None. You can fix this by making sure every LinkedList contains at least one node. This is typically referred to as a dummy node, and you can use this node to store metadata (such as the length of the list).
The Node class itself does not change.
class Node:
def __init__(self, data=None):
self.data = data
self.nextnode = None
The LinkedList, however, simplifies by creating a dummy node. This provides
the guarantee that every node that stores real data is point to by another node.
class LinkedList:
def __init__(self):
self.headnode = Node(0)
def insert(self, preNode, newNode):
newNode.nextnode = preNode.nextnode
preNode.nextnode = newNode
self.headnode.data += 1
def append(self, newNode):
curr = self.headnode
while curr.nextNode is not None:
curr = curr.nextNode
self.insert(curr, newNode)
def prepend(self, newNode):
self.insert(self.headnode, newNode)
def _find_before(self, val):
pre = self.headnode
while pre.nextnode is not None:
curr = pre.nextnode
if curr.data == val:
return pre
pre = curr
def remove(self, RemoveVal):
pre = self._find_before(RemoveVal)
if pre is None:
return
pre.nextnode = pre.nextnode.nextnode
self.headnode.data -= 1
This simplifies all three insertions. The general case can always apply, since there is always a node that comes before the node you insert. append and prepend are simple wrappers that find the appropriate node to pass to insert.
Likewise, remove simply finds the node before the given value, and if the search succeeds, handles updating the prior node's nextnode attribute.
insert and remove also update the size of the list stored in the dummy node.
A find method becomes a simple wrapper around _find_before; if you find a node before the value you are looking for, just return the node that follows it.
I think that an alternative design will make the code much clearer. Consider for example the following:
class Node:
def __init__(self, data=None):
self.data = data
self.nextnode = None
def printlist(self):
print(self.data)
if self.nextnode is not None:
self.nextnode.printlist()
def push(self, node):
node.nextnode = self
return node
def insertafter(self, node):
node.nextnode = self.nextnode
self.nextnode = node
return self
def append(self, node):
lastnode = self
while lastnode.nextnode is not None:
lastnode = lastnode.nextnode
lastnode.nextnode = node
return self
def remove(self, value):
prev = None
walk = self
while walk is not None:
if walk.data == value:
if prev is None:
return walk.nextnode
else:
prev.nextnode = walk.nextnode
return self
else:
prev = walk
walk = walk.nextnode
return self
list1 = Node("Mon")
n2 = Node("Tue")
n3 = Node("Wed")
# Link first Node to second node
list1 = list1.insertafter(n2)
# Link second Node to third node
n2 = n2.insertafter(n3)
n4 = Node("Sun")
n5 = Node("Tur")
n6 = Node("Newdate")
list1 = list1.push(n4)
list1 = list1.append(n5)
list1 = list1.insertafter(n6)
list1 = list1.remove("Newdate")
list1.printlist()
The main idea is that a Node is the linked list. As long as you have the head of the list kept in a variable, you can have access to the entire list, without the need for a separate data structure.

How to get the length of a tree in python binary search

class Node:
def __init__(self,data=None):
self.data=data
self.left_child=None
self.right_child=None
self.parent=None
self.root = None
class BinarySearchTree:
def __init__(self):
self.root=None
def add(self, data):
if self.root == None:
self.root = Node(data)
else:
self.add_helper(data, self.root)
def add_helper(self, data, cur_node):
if data < cur_node.data:
if cur_node.left_child == None:
cur_node.left_child = Node(data)
cur_node.left_child.parent = cur_node # set parent
else:
self.add_helper(data, cur_node.left_child)
elif data > cur_node.data:
if cur_node.right_child == None:
cur_node.right_child = Node(data)
cur_node.right_child.parent = cur_node # set parent
else:
self.add_helper(data, cur_node.right_child)
else:
print("data already in tree!")
def __len__(self):
if self.root is None:
return 0
else:
return (self.__len__(self.left_child) + 1 +self. __len__(self.right_child))
So i am trying to return the length of the binary search tree list, so i tried using the len method for my binary search tree class. However, this is not working correctly. I need it to be able to not take in any parameters, and just return an integer for the length of the binary search tree list. What am i missing and what am i doing wrong here?
You will need a helper function that takes a Node argument. Then do the recursion on the left and right of the node.
def __len__(self):
return self.tree_len(self.root)
def tree_len(self, node):
if node is None:
return 0
else:
return 1 + max(self.tree_len(node.right_child), self.tree_len(node.left_child))

NameError When Implementing Inorder Traversal

I'm trying to implement a Binary Search Tree in Python, and I'm having difficulty in understanding the NameError I get when I call the inorder traveral method on my tree. I have already created a BST instance, and I've defined the in_order_traversal() method before I call it on the instance, so why am I getting a NameError?
class BSTNode:
def __init__(self,data):
self.data = data
self.left = None
self.right = None
class BST:
def __init__(self):
self.root = None
def insert(self,data):
new_node = BSTNode(data)
if self.root == None:
self.root = new_node
else:
ptr = self.root
prev = None
while ptr != None:
prev = ptr
if data <= ptr.data:
ptr = ptr.left
else:
ptr = ptr.right
if data <= prev.data:
prev.left = new_node
else:
prev.right = new_node
def in_order_traversal(self):
if self.root != None:
in_order_traversal(self.root.left)
print(root.data)
in_order_traversal(self.root.right)
bst = BST()
bst.insert(50)
bst.insert(25)
bst.insert(100)
bst.insert(80)
bst.insert(30)
bst.in_order_traversal()
You forgot the self.
def in_order_traversal(self):
if self.root != None:
self.in_order_traversal(self.root.left)
print(root.data)
self.in_order_traversal(self.root.right)

Binary search tree insert method not working

class Node:
def __init__(self,value = None):
self.value = value
self.left = None
self.right = None
class binary_search_tree:
def __init__(self):
self.root = None
def insert(self,current,value):
if current == None:
current = Node(value)
elif value < current.value:
self.insert(current.left,value)
elif value > current.value:
self.insert(current.right,value)
tree = binary_search_tree()
for i in (12,5,18,3,4,6):
tree.insert(tree.root,i)
The insert method for my binary search tree is not working can someone help me out.
(Post is mostly code, please add more details.)

Categories