Question: *can you fix this error of my python code! * import random class Node: Tree node: left and right child + data which can
*can you fix this error of my python code! *
import random
class Node:
""" Tree node: left and right child + data which can be any object """ def __init__(self, data): """ Node constructor @param data node data object """ self.left = None self.right = None self.data = data
def insert(self, data): """ Insert new node with data @param data node data object to insert """ if self.data: if data < self.data: if self.left is None: self.left = Node(data) else: self.left.insert(data) elif data > self.data: if self.right is None: self.right = Node(data) else: self.right.insert(data) else: self.data = data
def lookup(self, data, parent=None): """ Lookup node containing data @param data node data object to look up @param parent node's parent @returns node and node's parent if found or None, None """ 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 children_count(self): """ Returns the number of children @returns number of children: 0, 1, 2 """ cnt = 0 if self.left: cnt += 1 if self.right: cnt += 1 return cnt
def delete(self, data): """ Delete node containing data @param data node's content to delete """ # get node containing data node, parent = self.lookup(data) if node is not None: children_count = node.children_count() if children_count == 0: # if node has no children, just remove it if parent: if parent.left is node: parent.left = None else: parent.right = None del node else: self.data = None elif children_count == 1: # if node has 1 child # replace node with its 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 else: self.left = n.left self.right = n.right self.data = n.data else: # if node has 2 children # find its successor parent = node successor = node.right 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 print_tree(self): """ Print tree content inorder """ if self.left: self.left.print_tree() print self.data if self.right: self.right.print_tree()
def main(): root = Node(8);# root node for the binary search tree. for i in range(0,31): r=random.randint(1,101); #each time generating a random number between 1,101. (it includes 100.) #and inserting it to the tree root.insert(r); print "Total Tree is " root.print_tree(); search_for = random.randint(1,101);#search for a randomly generated number #search_for = int(input("Enter to search : ")); searching for manually print " searching for node :",search_for node,parent= root.lookup(search_for); #lookup will return the node and it's parent if found, and returns None,None if not found. if (node==None): print " Node not found!"; else: print " node Found!" main();
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
