PYTHON 3 PLEASE FOLLOW INSTRUCTIONS
COMPLETE CODE
Problem : Complete the function predecessor() to take in an arbitrary node of a BST, and return the value of the predecessor of the given node.
Code : class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def predecessor(node):
# TODO
#first of all you need a parent attribute in node
#otherwise you wont be able to compute the predecessor
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
self.parent = None
def predecessor(node):
#if node is None its invalid
if node is None:
return
None
#if it has a left child then the maximum
of the left subtree is the answer
if not(node.left is None):
root =
node.left #going to left subtree
while
not(node.right is None): #finding the maximum value in left
subtree
root = root.right
return
root
#if we dont have a left subtree we
traverse up till we get required answer
#
8
#
/ \
#
6 12 suppose node = 9
#
/ so it need to traverse upto 8 which
is answer
#
11
#
/
#
9
p = node.parent
c = node
while ( not(p is None) and c ==
p.left):
c = p
p =
p.parent
return p
PYTHON 3 PLEASE FOLLOW INSTRUCTIONS COMPLETE CODE Problem : Complete the function predecessor() to take in...
Can you complete this program using same code please. DO not change any code. Thank you In this problem you are given the root of a BST and you want to return the summation of all the values in the BST. Remember that the summation of a BST is the summation of all the nodes in the left subtree + the summation of all the nodes in the right subtree + the value of the current node Expected behavior: root...
Can you help me with python question: Implement the function isBinarySearchTree(root), which returns True if the binary tree passed to it is a valid binary search tree. I've provided a simple TreeNode class, and the beginnings of isBinarySearchTree(root). class TreeNode: '''Node for a simple binary tree structure''' def __init__(self, value, left, right): self.value = value self.left = left self.right = right def isBinarySearchTree(tree):
Python question. How do i make this code work? im trying to print the value of the nodes using the level order trasversal class Node: def __init__(self, x): self.val = x self.left = None self.right = None class BinarySearchTree: def insertNode(self, value): self.root = self._insert(self.root, value) def levelOrderTraversal(self, root): if root is None: return [] result, current = [], [root] while current: next_level, vals = [], [] for node in current: vals.append(node.val) if node.left: next_level.append(node.left) if node.right: next_level.append(node.right) current...
Python Given the root to a binary tree, implement serialize(root), which serializes the tree into a string, and deserialize(s), which deserializes the string back into the tree. For example, given the following Node class class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right The following test should pass: node = Node('root', Node('left', Node('left.left')), Node('right')) assert deserialize(serialize(node)).left.left.val == 'left.left'
Can you complete the following code, please? Thank you. class BinaryNode: def __init__(self, value): self.__value = value self.__left = None self.__right = None self.__parent = None self.__height = 1 def getValue(self): return self.__value def setHeight(self, height): self.__height = height def getHeight(self): return self.__height def setParent(self, node): self.__parent = node def getParent(self): return self.__parent def setLeftChild(self, child): self.__left = child child.setParent(self) def setRightChild(self, child): self.__right = child child.setParent(self) def createLeftChild(self, value): self.__left = BinaryNode(value) def createRightChild(self, value): self.__right = BinaryNode(value) def...
In the following code I give, everytime it will output ")". Input : ((A+B)*((C-D)/(E^F))) Expected output : ((A+B)*((C-D)/(E^F))) A+B*C-D/E^F my output : ) My code : class Et: def __init__(self , value): self.value = value self.left = None self.right = None def isOperator(c): if (c == '+' or c == '-' or c == '*' or c == '/' or c == '^' ): return True else: return False def inorder(t): if t is not None: inorder(t.left) print (t.value) inorder(t.right)...
PYTHON QUESTION... Building a Binary Tree with extended Binary Search Tree and AVL tree. Create a class called MyTree with the methods __init__(x), getLeft(), getRight(), getData(), insert(x) and getHeight(). Each child should itself be a MyTree object. The height of a leaf node should be zero. The insert(x) method should return the node that occupies the original node's position in the tree. Create a class called MyBST that extends MyTree. Override the method insert(x) to meet the definitions of a...
Question 3 (24 points) (0) Given Red-Black RBBST Node, implement insertNode ( ) function class RBBST Node: class RBBST: def init_(self, val, color): self.val val self.left None self.right None self.color color def init_(self) : self.root None def init_rbbst(self, val, color): self.root def insert(self, val): if (self.root is None): self.init_rbbst (val, RED) else: RBBST_Node(val, color) RED True BLACK False self. insertNode (self. root, val) def insertNode(self, current, val): (ii) def rotate_left (self, current) : (ii) def flip_colors (self, current) :
Python Expert, I need your help: I need to write a function(rev_int_rec) that take an integet number(num = 1234567) and retrive the last digit using (lastDigit = num % 10) and add it to a queue. Remove each last digit from num using the % operator Add each last digit to the queue, with each round of recursion reduce num by dividing by 10. Base case is when num is less than 10 # Base case: If num < 10...
Given a binary tree, it is useful to be able to display all of its data values. For this task, define a function called basic_print() which prints out all of the data values in a binary tree. The natural way to solve this problem is to use recursion. The diagram below illustrates a recursive solution to the problem, which consists of three simple steps: Step1: Print the root node Step 3: Print the right sub-tree 10 Step 2: Print the...