Computers and Technology

Exercise 1: BSTree operations For this exercise you'll implement three additional methods in the binary search tree data structure completed in class, so that you have an opportunity to practice both using the recursive pattern covered in class and navigating the binary tree structure.
The methods you'll implement are:
count_less_than: takes an argument x, and returns the number of elements in the tree with values less than x
successor: takes an argument x, and returns the smallest value from the tree that is larger than x (note that x itself does not need to be in the tree); if there are no values larger than x, returns None
descendants: takes an argument x, and returns all descendants of x in the tree (i. e., all values in the subtree rooted at x), ordered by value; if xhas no descendants or does not exist in the tree, returns an empty list
The cell below contains the (read-only) BSTree implementation from lecture. Beneath that is the cell containing the methods you will be implementing, followed by unit test cells.
class BSTree:

class Node:
def __init__(self, val, left=None, right=None):
self. val = val
self. left = left
self. right = right

def __init__(self):
self. size = 0
self. root = None
โ€‹
def __contains__(self, val):
def contains_rec(node):
if not node:
return False
elif val < node. val:
return contains_rec(node. left)
elif val > node. val:
return contains_rec(node. right)
else:
return True
return contains_rec(self. root)
โ€‹
def add(self, val):
assert(val not in self)
def add_rec(node):
if not node:
return BSTree. Node(val)
elif val < node. val:
return BSTree. Node(node. val, left=add_rec(node. left), right=node. right)
else:
return BSTree. Node(node. val, left=node. left, right=add_rec(node. right))
self. root = add_rec(self. root)
self. size += 1

def __delitem__(self, val):
assert(val in self)
def delitem_rec(node):
if val < node. val:
node. left = delitem_rec(node. left)
return node
elif val > node. val:
node. right = delitem_rec(node. right)
return node
else:
if not node. left and not node. right:
return None
elif node. left and not node. right:
return node. left
elif node. right and not node. left:
return node. right
else:
# remove the largest value from the left subtree as a replacement
# for the root value of this tree
t = node. left # refers to the candidate for removal
if not t. right:
node. val = t. val
node. left = t. left
else:
n = t
while n. right. right:
n = n. right
t = n. right
n. right = t. left
node. val = t. val
return node

self. root = delitem_rec(self. root)
self. size -= 1
โ€‹
def __iter__(self):
def iter_rec(node):
if node:
yield from iter_rec(node. left)
yield node. val
yield from iter_rec(node. right)

return iter_rec(self. root)

def __len__(self):
return self. size

def pprint(self, width=64):
"""Attempts to pretty-print this tree's contents."""
height = self. height()
nodes = [(self. root, 0)]
prev_level = 0
repr_str = ''
while nodes:
n, level = nodes. pop(0)
if prev_level != level:
prev_level = level
repr_str += '\n'
if not n:
if level < height-1:
nodes. extend([(None, level+1), (None, level+1)])
repr_str += '{val:^{width}}'.format(val='-', width=width//2**level)
elif n:
if n. left or level < height-1:
nodes. append((n. left, level+1))
if n. right or level < height-1:
nodes. append((n. right, level+1))
repr_str += '{val:^{width}}'.format(val=n. val, width=width//2**level)
print(repr_str)

def height(self):
"""Returns the height of the longest branch of the tree."""
def height_rec(t):
if not t:
return 0
else:
return max(1+height_rec(t. left), 1+height_rec(t. right))
return height_rec(self. root)

answer
Answers: 1

Other questions on the subject: Computers and Technology

image
Computers and Technology, 22.06.2019 03:40, plzhelpmeasap46
Hello my name is mihai and i need your : )i have to do a python project in computer science and iโ€™m really busy with my mocks this period of time besides this iโ€™m not good at coding so could someone pls pls pls sort me out ? i actually beg ; ))
Answers: 1
image
Computers and Technology, 22.06.2019 19:40, rakanmadi87
Solve the following javafx application: write a javafx application that analyzes a word. the user would type the word in a text field, and the application provides three buttons for the following: - one button, when clicked, displays the length of the word.- another button, when clicked, displays the number of vowels in the word.- another button, when clicked, displays the number of uppercase letters in the word(use the gridpane or hbox and vbox to organize the gui controls).
Answers: 1
image
Computers and Technology, 22.06.2019 23:30, TheBurntToast
What is the digital revolution and how did it change society? what are the benefits of digital media?
Answers: 1
image
Computers and Technology, 23.06.2019 06:20, Ab20600
Which text function capitalizes the first letter in a string of text? question 10 options: upper capital first proper
Answers: 1
Do you know the correct answer?
Exercise 1: BSTree operations For this exercise you'll implement three additional methods in the b...

Questions in other subjects:

Konu
Mathematics, 03.12.2020 08:10
Konu
Mathematics, 03.12.2020 08:10
Konu
Mathematics, 03.12.2020 08:10