Previous code:
class BinarySearchTree:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def search(self, find_data):
if self.data == find_data:
return self
elif find_data < self.data and self.left != None:
return self.left.search(find_data)
elif find_data > self.data and self.right != None:
return self.right.search(find_data)
else:
return None
def get_left(self):
return self.left
def get_right(self):
return self.right
def set_left(self, tree):
self.left = tree
def set_right(self, tree):
self.right = tree
def set_data(self, data):
self.data = data
def get_data(self):
return self.data
def traverse(root,order):
if order=="in":
return Inorder(root)
elif order=="pre":
return Preorder(root)
elif order=="post":
return Postorder(root)
else:
return ("Not a correct input")
def insert(root, data):
if not root:
return BinarySearchTree(data)
elif root.data == data:
root.count += 1
elif data < root.data:
root.left = insert(root.left, data)
else:
root.right = insert(root.right, data)
return root
def create_new_bst(seq):
root = None
for number in seq:
root = insert(root, number)
return root
def findSum(root):
if root is None:
return 0
else:
return
root.data+findSum(root.left)+findSum(root.right)
def sum_beneath(root,data):
node=root.search(data)
if node is None:
return 0
total=0
total+=findSum(node.left)
total+=findSum(node.right)
return total
def Inorder(root):
path=[]
if root:
path.extend(Inorder(root.left))
path.append(root.data),
path.extend(Inorder(root.right))
return path
def Postorder(root):
path=[]
if root:
path.extend(Postorder(root.left) )
path.extend(Postorder(root.right) )
path.append(root.data)
return path
def Preorder(root):
path=[]
if root:
path.append(root.data)
path.extend(Preorder(root.left))
path.extend(Preorder(root.right))
return path
def traverse(root,order):
if order=="in":
return Inorder(root)
elif order=="pre":
return Preorder(root)
elif order=="post":
return Postorder(root)
else:
return ("Not a correct input")
def breadth_first(root):
if root is None:
return
q = []
ans = []
q.append(root)
while(len(q) > 0):
ans.append(q[0].data)
node = q.pop(0)
if node.left is not None:
q.append(node.left)
if node.right is not None:
q.append(node.right)
return ans
===================================
SEE OUTPUT
Thanks, PLEASE COMMENT if there is any concern.
Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None de...
[Python] Construct Tree Using Inorder and Preorder Given Preorder and Inorder traversal of a binary tree, create the binary tree associated with the traversals.You just need to construct the tree and return the root. Note: Assume binary tree contains only unique elements. Input format : Line 1 : n (Total number of nodes in binary tree) Line 2 : Pre order traversal Line 3 : Inorder Traversal Output Format : Elements are printed level wise, each level in new line...
python pls and noticed the output added "" One way to represent a binary tree is using the nested list format Consider the following binary tree: 24 72 78 8 51 25 This binary tree could be represented using a nested list as follows [55, [24, [8, None, None], [51, [25, None, None], None]], [72, None, [78, None, None ]]] The nested list format always uses a list of length three to represent a binary tree. The first item in...
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...
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...
class BinaryTree: def __init__(self, data, left=None, right=None): self.__data = data self.__left = left self.__right = right def insert_left(self, new_data): if self.__left == None: self.__left = BinaryTree(new_data) else: t = BinaryTree(new_data, left=self.__left) self.__left = t def insert_right(self, new_data): if self.__right == None: self.__right = BinaryTree(new_data) else: t = BinaryTree(new_data, right=self.__right) self.__right = t def get_left(self): return self.__left def get_right(self): return self.__right def set_data(self, data): self.__data = data def get_data(self): return self.__data def set_left(self, left): self.__left = left def set_right(self, right): self.__right...
PYTHON -------------------------------------------------------- class LinkedList: def __init__(self): self.__head = None self.__tail = None self.__size = 0 # Return the head element in the list def getFirst(self): if self.__size == 0: return None else: return self.__head.element # Return the last element in the list def getLast(self): if self.__size == 0: return None else: return self.__tail.element # Add an element to the beginning of the list def addFirst(self, e): newNode = Node(e) # Create a new node newNode.next = self.__head # link...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
COMPLETE THE _CONSTRUCT() AND CONSTRUCTTREE() FUNCTIONS class expressionTree: class treeNode: def __init__(self, value, lchild, rchild): self.value, self.lchild, self.rchild = value, lchild, rchild def __init__(self): self.treeRoot = None #utility functions for constructTree def mask(self, s): nestLevel = 0 masked = list(s) for i in range(len(s)): if s[i]==")": nestLevel -=1 elif s[i]=="(": nestLevel += 1 if nestLevel>0 and not (s[i]=="(" and nestLevel==1): masked[i]=" " return "".join(masked) def isNumber(self, expr): mys=s.strip() if len(mys)==0 or not isinstance(mys, str): print("type mismatch error: isNumber")...
write a new test program called RemoveDuplicates.java. The program reads text input from keyboard or a text file and adds the words to a BST. The program then traverses the BST and prints out the words in order (based on ASCII/UNICODE order) on the screen (or to output text file). Note that you may need to make some changes to BST.java. Sample test: ----jGRASP exec: java -ea removeDuplicates Original Text: a B 2 n w C q K l 0...
class Node(object): def __init__(self, data, next=None): self.data = data self.next = next class List(object): def __init__(self): self.head = None self.tail = None Implement the following functions for Linked List in Python and use the constructors above : Copy(lList) Builds and returns a copy of list ItemAt(List,i) Returns the data item at position i in list Pop(List,i=0) Remove item at position i in list. If i is not specified, it removes the first item in list Count(List,x) Returns the number...