How to find a value from a Binary Search Tree in python? - python-3.x

I want to create a interactive Binary Search Tree(BST). So I created the BST using the following code as
class BTreeNode(object):
def __init__(self, data):
self.data = data
self.rChild = None
self.lChild = None
def __str__(self):
return (self.lChild.__str__() + '<-' if self.lChild != None
else '') + self.data.__str__() + (
'->' + self.rChild.__str__() if self.rChild != None else '')
# Insert method to create nodes
def insert(self, btreeNode):
if self.data > btreeNode.data: # insert left
if self.lChild == None:
self.lChild = btreeNode
else:
self.lChild.insert(btreeNode)
else: # insert right
if self.rChild == None:
self.rChild = btreeNode
else:
self.rChild.insert(btreeNode)
# Insert method to create nodes
# findval method to compare the value with nodes
def findval(self, lkpval):
if lkpval < self.data:
if self.lChild.data is None:
return str(lkpval)+" Not Found"
return self.lChild.findval(lkpval)
elif lkpval > self.data:
if self.rChild.data is None:
return str(lkpval)+" Not Found"
return self.rChild.findval(lkpval)
else:
print(str(self.data) + ' is found')
# findval method to compare the value with nodes
def display():
btreeRoot = BTreeNode(5)
print('inserted %s:' % 5, btreeRoot)
btreeRoot.insert(BTreeNode(7))
print('inserted %s:' % 7, btreeRoot)
btreeRoot.insert(BTreeNode(3))
print('inserted %s:' % 3, btreeRoot)
btreeRoot.insert(BTreeNode(1))
print('inserted %s:' % 1, btreeRoot)
# print(btreeRoot.findval(3))
print(display())
If I execute the above code I will get the following interactive output as
inserted 5: 5
inserted 7: 5->7
inserted 3: 3<-5->7
inserted 1: 1<-3<-5->7
This is the my expected output and I got it. Also, I want to find a value from the BST. So I used the following code in the display function as
# print(btreeRoot.findval(3))
If I un comment the code I will get error. So, how can I modify my code to display whether 3 is present in the BST or not? Thanks..

The problem occurs when you perform checks if self.lChild.data is None and if self.rChild.data is None. These lines should be replaced by if self.lChild is None and if self.rChild is None respectively, as your class implies those attributes being None's, not their data attributes.
Also consider using is None instead == None. This is not a big problem in this case, but may cause troubles in other contexts. Look here for details: Python None comparison: should I use "is" or ==?

Related

Switching between classes and passing results

I am trying to add different PlugLead's to the PlugBoard and extract the combination. In the first class I am extracting the one letter from the list should they match or return the input. e.g.
lead = PlugLead("AG")
assert(lead.encode("A") == "G")
class PlugLead:
def __init__(self, c):
self.c = c
def encode(self, l):
c0 = self.c[0]
c1 = self.c[1]
if len(l) == 1 and c0 == l:
return c1
elif len(l) == 1 and c1 == l:
return c0
else:
return l
class Plugboard:
def __init__(self):
self.__head = 0
self.leads = []
self.c = []
def add(self, item):
if self.__head >= 10:
print("leads already got 10 items")
elif item in self.leads:
print(f"leads already have this item: {item}")
else:
self.leads.append(item)
self.__head += 1
return self.leads
def encode(self)
lines = plugboard.leads
for word in lines:
word = word.split(",")
PlugLead.encode(word)
In the second class I am trying to add multiple combinations and then at the end pass the one letter to see what its match is in the Class PlugLead however am not able to switch between the two. In the class PlugLead I have a add function that allows the different combinations to be added up to 10 and then I would like to encode from this list the combination of the pairs. e.g.
plugboard = Plugboard()
plugboard.add(PlugLead("SZ"))
plugboard.add(PlugLead("GT"))
plugboard.add(PlugLead("DV"))
plugboard.add(PlugLead("KU"))
assert(plugboard.encode("K") == "U")
assert(plugboard.encode("A") == "A")
if you want to use PlugLead("{balabala}"),you need use __new__ to return a dict when you create an instance, not __init__.
you want a key-value pair in Plugboard, it should be a dict not list
fix some other typo and bugs.
code:
class PlugLead:
def __new__(self, c):
return {c[0]:c[1]}
class Plugboard:
def __init__(self):
self.__head = 0
self.leads = {}
def add(self, item):
if self.__head >= 10:
print("leads already got 10 items")
elif list(item.keys())[0] in self.leads.keys():
print(f"leads already have this item: {item}")
else:
self.leads.update(item)
self.__head += 1
return self.leads
def encode(self,key):
if key in self.leads:
return self.leads[key]
elif key in self.leads.values():
return list(self.leads.keys())[list(self.leads.values()).index(key)]
return key
plugboard = Plugboard()
plugboard.add(PlugLead("SZ"))
plugboard.add(PlugLead("GT"))
plugboard.add(PlugLead("DV"))
plugboard.add(PlugLead("KU"))
plugboard.add(PlugLead("KU"))
assert(plugboard.encode("K") == "U")
assert(plugboard.encode("U") == "K")
assert(plugboard.encode("A") == "A")
print(plugboard.encode("K"))
print(plugboard.encode("U"))
print(plugboard.encode("A"))
result:
leads already have this item: {'K': 'U'}
U
K
A

How to fix TypeError in Python when comparing values of two nodes of a Linked List

I am trying to compare the value of 2 nodes of 2 separate linked lists (the values will always be int), but it keeps throwing TypeError despite my best efforts to get around it.
Python version 3.7.4 on VS Code for Mac 1.37.0
This is code I wrote to merge two Linked Lists with values as int (which should be pre-sorted before being passed as arguments to the 'merge' method). The output needs to be a single sorted Linked List. I understand TypeError gets thrown if 2 values are being operated on while they are of incompatible type (e.g. str + int or int < object etc.). To get around it, I tried forced type-casting, but that then creates AttributionError, since object at one point is 'None' and it doesn't get typecast to int.
######## Definition of 'Node', 'LinkedList' #######
class Node:
def __init__(self, value):
self.value = value
self.next = None
def __repr__(self):
return str(self.value)
class LinkedList:
def __init__(self, head):
self.head = head
def append(self, value):
if self.head is None:
self.head = Node(value)
return
node = self.head
while node.next is not None:
node = node.next
node.next = Node(value)
#### Where I need help #####
def merge(list1, list2):
"""
Merge and sort two linked lists
Args:
list1, list2: two linked lists that need to be merged. They need to be pre-sorted before being passed as a argument.
Returns:
linked-list: Merged and sorted linked-list, a combination of list1 and list2
"""
merged = LinkedList(None) # create an empty linked list
if list1 is None:
return list2
if list2 is None:
return list1
list1_elt = list1.head # start with heads of the two lists
list2_elt = list2.head
while list1_elt is not None or list2_elt is not None:
# val1 = int(str(list1_elt.value))
# val2 = int(str(list2_elt.value))
# condition = val1 < val2
# print("List1 value: {} and List2 value: {}".format(list1_elt.value, list2_elt.value))
if list1_elt is None:
# print("List2 value: {}".format(list2_elt.value))
merged.append(list2_elt)
list2_elt = list2_elt.next
elif list2_elt is None:
# print("List1 value: {}".format(list1_elt.value))
merged.append(list1_elt)
list1_elt = list1_elt.next
elif list1_elt.value <= list2_elt.value:
# elif val1 <= val2:
# elif condition:
# print("List1 value: {}".format(list1_elt.value))
merged.append(list1_elt)
list1_elt = list1_elt.next
else:
# print("List2 value: {}".format(list2_elt.value))
merged.append(list2_elt)
list2_elt = list2_elt.next
return merged
##### TEST CODE TO CHECK #######
# First Test scenario
linked_list = LinkedList(Node(1))
linked_list.append(Node(3))
linked_list.append(Node(5))
second_linked_list = LinkedList(Node(2))
second_linked_list.append(4)
merged = merge(linked_list, second_linked_list)
node = merged.head
while node is not None:
#This should print 1 2 3 4 5
print(node.value)
node = node.next
In the merge method definition, I keep getting an error at the part:
elif list1_elt.value <= list2_elt.value:
I've tried to store the values in two separate variables with forced typecasting, but I get other errors. I am at my wit's end to understand what I am doing wrong here. This is the error I get:
File "filename.py", line 58, in merge
elif list1_elt.value <= list2_elt.value:
TypeError: '<=' not supported between instances of 'Node' and 'int'
You are setting value with a Node rather than int, at
self.head = Node(value)
node.next = Node(value)
Either you can send int to append function as :
linked_list.append(5)
or directly assign the Node
self.head = value
node.next = value
I would suggest the first method as you should send the value which you want to insert into the list and your append function is also expecting a value.

How to search in an id tree in the python program

I'm a student and working on a small assignment where I need to collect inputs from the student on factors like kind of books they like to issue from the library. I've been provided id_tree class which I need to search using. As you can see I'm getting inputs from the console and I like to use that as the search criteria and get the recommendation from the id tree.
Just for testing purpose, I'm using out.py, but that needs to be replaced with id_tree search logic for which I'm struggling.
# k-Nearest Neighbors and Identification Trees
#api.py
import os
from copy import deepcopy
from functools import reduce
################################################################################
############################# IDENTIFICATION TREES #############################
################################################################################
class Classifier :
def __init__(self, name, classify_fn) :
self.name = str(name)
self._classify_fn = classify_fn
def classify(self, point):
try:
return self._classify_fn(point)
except KeyError as key:
raise ClassifierError("point has no attribute " + str(key) + ": " + str(point))
def copy(self):
return deepcopy(self)
def __eq__(self, other):
try:
return (self.name == other.name
and self._classify_fn.__code__.co_code == other._classify_fn.__code__.co_code)
except:
return False
def __str__(self):
return "Classifier<" + str(self.name) + ">"
__repr__ = __str__
## HELPER FUNCTIONS FOR CREATING CLASSIFIERS
def maybe_number(x) :
try :
return float(x)
except (ValueError, TypeError) :
return x
def feature_test(key) :
return Classifier(key, lambda pt : maybe_number(pt[key]))
def threshold_test(feature, threshold) :
return Classifier(feature + " > " + str(threshold),
lambda pt: "Yes" if (maybe_number(pt.get(feature)) > threshold) else "No")
## CUSTOM ERROR CLASSES
class NoGoodClassifiersError(ValueError):
def __init__(self, value=""):
self.value = value
def __str__(self):
return repr(self.value)
class ClassifierError(RuntimeError):
def __init__(self, value=""):
self.value = value
def __str__(self):
return repr(self.value)
class IdentificationTreeNode:
def __init__(self, target_classifier, parent_branch_name=None):
self.target_classifier = target_classifier
self._parent_branch_name = parent_branch_name
self._classification = None #value, if leaf node
self._classifier = None #Classifier, if tree continues
self._children = {} #dict mapping feature to node, if tree continues
self._data = [] #only used temporarily for printing with data
def get_parent_branch_name(self):
return self._parent_branch_name if self._parent_branch_name else "(Root node: no parent branch)"
def is_leaf(self):
return not self._classifier
def set_node_classification(self, classification):
self._classification = classification
if self._classifier:
print("Warning: Setting the classification", classification, "converts this node from a subtree to a leaf, overwriting its previous classifier:", self._classifier)
self._classifier = None
self._children = {}
return self
def get_node_classification(self):
return self._classification
def set_classifier_and_expand(self, classifier, features):
if classifier is None:
raise TypeError("Cannot set classifier to None")
if not isinstance_Classifier(classifier):
raise TypeError("classifier must be Classifier-type object: " + str(classifier))
self._classifier = classifier
try:
self._children = {feature:IdentificationTreeNode(self.target_classifier, parent_branch_name=str(feature))
for feature in features}
except TypeError:
raise TypeError("Expected list of feature names, got: " + str(features))
if len(self._children) == 1:
print("Warning: The classifier", classifier.name, "has only one relevant feature, which means it's not a useful test!")
if self._classification:
print("Warning: Setting the classifier", classifier.name, "converts this node from a leaf to a subtree, overwriting its previous classification:", self._classification)
self._classification = None
return self
def get_classifier(self):
return self._classifier
def apply_classifier(self, point):
if self._classifier is None:
raise ClassifierError("Cannot apply classifier at leaf node")
return self._children[self._classifier.classify(point)]
def get_branches(self):
return self._children
def copy(self):
return deepcopy(self)
def print_with_data(self, data):
tree = self.copy()
tree._assign_data(data)
print(tree.__str__(with_data=True))
def _assign_data(self, data):
if not self._classifier:
self._data = deepcopy(data)
return self
try:
pairs = list(self._soc(data, self._classifier).items())
except KeyError: #one of the points is missing a feature
raise ClassifierError("One or more points cannot be classified by " + str(self._classifier))
for (feature, branch_data) in pairs:
if feature in self._children:
self._children[feature]._assign_data(branch_data)
else: #feature branch doesn't exist
self._data.extend(branch_data)
return self
_ssc=lambda self,c,d:self.set_classifier_and_expand(c,self._soc(d,c))
_soc=lambda self,d,c:reduce(lambda b,p:b.__setitem__(c.classify(p),b.get(c.classify(p),[])+[p]) or b,d,{})
def __eq__(self, other):
try:
return (self.target_classifier == other.target_classifier
and self._parent_branch_name == other._parent_branch_name
and self._classification == other._classification
and self._classifier == other._classifier
and self._children == other._children
and self._data == other._data)
except:
return False
def __str__(self, indent=0, with_data=False):
newline = os.linesep
ret = ''
if indent == 0:
ret += (newline + "IdentificationTreeNode classifying by "
+ self.target_classifier.name + ":" + newline)
ret += " "*indent + (self._parent_branch_name + ": " if self._parent_branch_name else '')
if self._classifier:
ret += self._classifier.name
if with_data and self._data:
ret += self._render_points()
for (feature, node) in sorted(self._children.items()):
ret += newline + node.__str__(indent+1, with_data)
else: #leaf
ret += str(self._classification)
if with_data and self._data:
ret += self._render_points()
return ret
def _render_points(self):
ret = ' ('
first_point = True
for point in self._data:
if first_point:
first_point = False
else:
ret += ', '
ret += str(point.get("name","datapoint")) + ": "
try:
ret += str(self.target_classifier.classify(point))
except ClassifierError:
ret += '(unknown)'
ret += ')'
return ret
################################################################################
############################# k-NEAREST NEIGHBORS ##############################
################################################################################
class Point(object):
"""A Point has a name and a list or tuple of coordinates, and optionally a
classification, and/or alpha value."""
def __init__(self, coords, classification=None, name=None):
self.name = name
self.coords = coords
self.classification = classification
def copy(self):
return deepcopy(self)
def __getitem__(self, i): # make Point iterable
return self.coords[i]
def __eq__(self, other):
try:
return (self.coords == other.coords
and self.classification == other.classification)
except:
return False
def __str__(self):
ret = "Point(" + str(self.coords)
if self.classification:
ret += ", " + str(self.classification)
if self.name:
ret += ", name=" + str(self.name)
ret += ")"
return ret
__repr__ = __str__
################################################################################
############################### OTHER FUNCTIONS ################################
################################################################################
def is_class_instance(obj, class_name):
return hasattr(obj, '__class__') and obj.__class__.__name__ == class_name
def isinstance_Classifier(obj):
return is_class_instance(obj, 'Classifier')
def isinstance_IdentificationTreeNode(obj):
return is_class_instance(obj, 'IdentificationTreeNode')
def isinstance_Point(obj):
return is_class_instance(obj, 'Point')
#id_tree
from api import *
import math
log2 = lambda x: math.log(x, 2)
INF = float('inf')
import pandas as pd
def id_tree_classify_point(point, id_tree):
if id_tree.is_leaf():
return id_tree.get_node_classification()
else:
new_tree = id_tree.apply_classifier(point)
get_point = id_tree_classify_point(point, new_tree)
return get_point
def split_on_classifier(data, classifier):
"""Given a set of data (as a list of points) and a Classifier object, uses
the classifier to partition the data. Returns a dict mapping each feature
values to a list of points that have that value."""
#Dictionary which will contain the data after classification.
class_dict = {}
#Iterating through all the points in data
for i in range(len(data)):
get_value = classifier.classify(data[i])
if get_value not in class_dict:
class_dict[get_value] = [data[i]]
else:
class_dict[get_value].append(data[i])
return class_dict
def branch_disorder(data, target_classifier):
"""Given a list of points representing a single branch and a Classifier
for determining the true classification of each point, computes and returns
the disorder of the branch."""
#Getting data after classification based on the target_classifier
class_dict = split_on_classifier(data, target_classifier)
if (len(class_dict) == 1):
#Homogenous condition
return 0
else:
disorder = 0
for i in class_dict:
get_len = len(class_dict[i])
p_term = get_len/ float(len(data))
disorder += (-1) * p_term * log2(p_term)
return disorder
def average_test_disorder(data, test_classifier, target_classifier):
"""Given a list of points, a feature-test Classifier, and a Classifier
for determining the true classification of each point, computes and returns
the disorder of the feature-test stump."""
average_disorder = 0.0
#Getting all the branches after applying test_classifer
get_branches = split_on_classifier(data, test_classifier)
#Iterating through the branches
for i in get_branches:
disorder = branch_disorder(get_branches[i], target_classifier)
average_disorder += disorder * (len(get_branches[i])/ float(len(data)))
return average_disorder
#### CONSTRUCTING AN ID TREE
def find_best_classifier(data, possible_classifiers, target_classifier):
"""Given a list of points, a list of possible Classifiers to use as tests,
and a Classifier for determining the true classification of each point,
finds and returns the classifier with the lowest disorder. Breaks ties by
preferring classifiers that appear earlier in the list. If the best
classifier has only one branch, raises NoGoodClassifiersError."""
#Base values to start with
best_classifier = average_test_disorder(data, possible_classifiers[0], target_classifier)
store_classifier = possible_classifiers[0]
#Iterating over the list of possible classifiers
for i in range(len(possible_classifiers)):
avg_disorder = average_test_disorder(data, possible_classifiers[i], target_classifier)
if avg_disorder < best_classifier:
best_classifier = avg_disorder
store_classifier = possible_classifiers[i]
get_branches = split_on_classifier(data, store_classifier)
if len(get_branches)==1:
#Only 1 branch present
raise NoGoodClassifiersError
else:
return store_classifier
def construct_greedy_id_tree(data, possible_classifiers, target_classifier, id_tree_node=None):
"""Given a list of points, a list of possible Classifiers to use as tests,
a Classifier for determining the true classification of each point, and
optionally a partially completed ID tree, returns a completed ID tree by
adding classifiers and classifications until either perfect classification
has been achieved, or there are no good classifiers left."""
#print data
#print "possible", possible_classifiers
#print "target", target_classifier
if id_tree_node == None:
#Creating a new tree
id_tree_node = IdentificationTreeNode(target_classifier)
if branch_disorder(data, target_classifier) == 0:
id_tree_node.set_node_classification(target_classifier.classify(data[0]))
else:
try:
#Getting the best classifier from the options available
best_classifier = find_best_classifier(data, possible_classifiers, target_classifier)
get_branches = split_on_classifier(data, best_classifier)
id_tree_node = id_tree_node.set_classifier_and_expand(best_classifier, get_branches)
#possible_classifiers.remove(best_classifier)
branches = id_tree_node.get_branches()
for i in branches:
construct_greedy_id_tree(get_branches[i], possible_classifiers, target_classifier, branches[i])
except NoGoodClassifiersError:
pass
return id_tree_node
possible_classifiers = [feature_test('age'),
feature_test('gender'),
feature_test('duration'),
feature_test('Mood')
]
df1 = pd.read_csv("data_form.csv")
#df1 = df1.drop("age", axis=1)
print(df1)
a = []
with open("data_form.csv") as myfile:
firstline = True
for line in myfile:
if firstline:
mykeys = "".join(line.split()).split(',')
firstline = False
else:
values = "".join(line.split()).split(',')
a.append({mykeys[n]:values[n] for n in range(0,len(mykeys))})
keys = a[0].keys()
print(keys)
with open('data_clean.csv', 'w') as output_file:
dict_writer = csv.DictWriter(output_file, keys)
dict_writer.writeheader()
dict_writer.writerows(a)
print(a)
tar = feature_test('genre')
print(construct_greedy_id_tree(a, possible_classifiers, tar))
#book_suggestion
import random
#from out import *
def genre(Mood, age, gender, duration):
print("Hi")
res_0= input("What's your name?")
res_1 = input("How are you, "+str(res_0)+"?")
if res_1 in ("good","fine","ok","nice"):
print ("Oh nice")
else:
print("Oh! It's alright")
Mood = input("What is your current mood?")
age = input("What is your age range : 10-12, 12-15,13-14,15-18,18+?")
gender = input("What is your gender?")
duration = input("How long do you want to read : 1week, 2weeks, 3weeks, 3+weeks, 2hours")
def get_book(genre):
suggestions = []
genre_to_book = {"Fantasy":["Just me and my babysitter - Mercer Mayer","Just Grandpa and me - Mercer Mayer","Just me and my babysitter - Mercer Mayer",
"The new Potty - Mercer Mayer","I was so mad - Mercer Mayer","Just me and my puppy" ,"Just a mess" ,"Me too"
,"The new Baby","Just shopping with mom"],
"Encyclopedias":["Brain Power - Paul Mcevoy", "My best books of snakes Gunzi Chrisitian","MY best books of MOON Grahame,Ian",
"The book of Planets Twist,Clint", "Do stars have points? Melvin", "Young discover series:cells Discovery Channel"]
,
"Action" : ["The Kane Chronicle:The Throne of Fire s Book 2 Riordan,Rick",
"Zane : ninja of ice Farshtey, Greg",
"Escape from Sentai Mountain Farshtey, Greg",
"Percy jackson Rick Riordan",
"The Kane Chronicle:The Throne of Fire s Book 2 Rick Riordan"],
"Comic" : ["Double Dork Diaries Russell Rachel Renée",
"Dork Dairies Russell Rachel Renee",
"Dork Dairies Russell Rachel Renée"],
"Mystery" : ["Sparkling Cyanide Christie Agatha",
"Poirot's Early Cases: Agatha Christie",
"The Name of this Book is Secret Bosch,Pseudonyuous"],
"Biographies" :["All by myself Mercer Mayer", "D Days prett bryan",
"Snake Bite Lane Andrew"] }
if (genre == "Fantasy"):
suggestions = [random.sample(genre_to_book["Fantasy"], 3)]
elif (genre == "Action"):
suggestions = [random.sample(genre_to_book["Action"], 3)]
elif (genre == "Comic"):
suggestions = [random.sample(genre_to_book["Comic"], 3)]
elif (genre == "Mystery"):
suggestions = [random.sample(genre_to_book["Mystery"], 3)]
elif (genre == "Encyclopedias"):
suggestions = random.sample(genre_to_book["Encyclopedias"], 3)
elif (genre == "Biographies"):
suggestions = random.sample(genre_to_book["Biographies"], 3)
return suggestions
print(get_book(genre(Mood, age, gender, duration)))
I want the program to not depend on out.py and and run on the information of id tree
The current implementation of the suggestions works by asking the user for a genre, then looking up a list of book titles in a dictionary using that genre as the key, then randomly selecting one of the titles and printing it. The current implementation also (presumably) constructs a IdentificationTreeNode containing recommendations, but then does nothing with it except printing it to the standard output.
The next step would be to not discard the tree, but save it in a variable and use in the recommendation process. Since the class structure is not given, it is not clear how this could be done, but it seems a reasonable assumption that it is possible to provide a keyword (the genre) and receive some collection of objects where each one contains data on a recommendation.
If constructing the IdentificationTreeNode is too costly to run on each recommendation request, it is possible to split the construction into its own script file and using python's pickle package to save the object in a file that can then be unpickled more quickly in the script performing the recommendations.

union of two default dictionaries

am trying to create a union of two default dictionaries. Here is the code for the method in my class:
def __add__(self,right):
mergedbag = copy.copy(self.bag_value)
for item in right:
if item not in mergedbag:mergedbag[item] = 0
mergedbag[item] += right[item]
return mergedbag
I create two default dictionaries:
b = Bag(['d','a','b','d','c','b','d'])
c = Bag(['d','a','b','d','c','b','d'])
the result of
print(b+c)
should be the total count of elements after the union..
Bag(a[a],b[4],c[3],d[6])
This is the error I keep getting:
Traceback (most recent call last):
File "D:\workspace33\courselib\driver.py", line 229, in driver
Command[print(b+c)]: exec(old,local,globl)
File "<string>", line 1, in <module>
File "D:\workspace33\Project2\src\bag.py", line 58, in __add__
mergedbag[item] += right[item]
TypeError: 'Bag' object is not subscriptable
Here is the new code:
class Bag:
def __init__(self, items = []):
self.bag_value = defaultdict(int)
for item in items:
self.bag_value[item] += 1
def __repr__(self):
bag_list = []
for item, count in self.bag_value.items():
bag_list.extend(list(item*count))
return 'Bag(' + str(bag_list) + ')'
def __str__(self):
return 'Bag(' + ','.join(str(item) + '[' + str(count) + ']' for item, count in self.bag_value.items()) + ')'
def __len__(self):
bag_len = 0
for value in self.bag_value:
bag_len += self.bag_value[value]
return bag_len
def unique(self):
return len(self.bag_value)
def __contains__(self, item):
return item in self.bag_value
def count(self, item):
return(self.bag_items.count(item))
def add(self, new):
self.bag_value[new] += 1
def __add__(self,right):
mergedbag = copy.copy(self.bag_value)
for item in right:
if item not in mergedbag:mergedbag[item] = 0
mergedbag[item] += right[item]
return mergedbag
def remove(self, item):
if item in self.bag_items:
del(item)
else:
raise ValueError(type_as_str(item) + ' not in bag.')
def __eq__(self, right):
if type(right) is not Bag:
raise TypeError('Cannot compare Bag with' + type_as_str(right) + '. Can only compare Bag with Bag')
else:
return (len(self) == len(right)) and (self.unique() == right.unique())
def __ne__(self, right):
return not self.__eq__(right)
def _bag_gen(self, bag_value):
for item in self.bag_value:
for count in range(self.bag_value[item]):
yield item
def __iter__(self):
return self._bag_gen(self.bag_value)
if __name__ == '__main__':
# bag = Bag(['d','a','b','d','c','b','d'])
# bag2 = Bag(['d','a','b','d','c','b','d'])
# bag3 = Bag(['d','a','b','d','c','b'])
# print(bag == bag2)
# print(bag == bag3)
# print(bag != bag2)
# print(bag != bag3)
import driver
driver.driver()
First of all, it is important to note that your Bag class is basically the same as a Counter. If you need any specific extension, just inherit from it and you are done. I would say that the following code just address all the functionality you are implementing yourself in your Bag class:
from collections import Counter
class Bag(Counter):
pass
b = Bag(['d','a','b','d','c','b','d'])
c = Bag(['d','a','b','d','c','b','d'])
print (b)
print (c)
print (b+c)
If you are doing an exercise to learn, the problem is that you are not implementing setitem and getitem methods in your Bag class to allow the [] notation. You could implement them or just access the attribute bag_value:
def __add__(self, right):
mergedbag = Bag()
mergedbag.bag_value = copy.copy(self.bag_value)
for item in right.bag_value.keys():
mergedbag.bag_value[item] += right.bag_value[item]
return mergedbag
Be sure to create and return a Bag object
The definition is
def __add__(self,other):
So you ought to merge the contents of self.items with other.items and then return the result.
Also, not sure if bag1+bag2 will work, but my pyfu is weak, you will explicitly have to iterate through them and add the counts together.
import copy
c = {'a':2,'b':1}
d = {'b':1,'c':1}
#
mergedBag = copy.copy(c) #might wanna use deepcopy?
for k in d:
if k not in mergedBag: mergedBag[k] = 0
mergedBag[k] += d[k]
print mergedBag

How do I get my insert function for a binary tree to run for efficiently

It seems as if my function runs very slowly. It should only take around 10 seconds to insert 100,000 elements, but it actually takes minutes.
Note: rit_object is a private class which puts types for each parameter
from rit_object import *
class EyecuBST(rit_object):
__slots__ = ('left', 'right', 'parent',
'value', 'height', 'size', 'imbalance')
_types = ('EyecuBST', 'EyecuBST', 'EyecuBST',
int, int, int, int)
def createEyecuBST(el, parent):
""" creates a BST containing just this node, and connected
to the given parent node, which is None if this is the root.
Returns the tree node.
"""
return EyecuBST(None, None, parent, el, 0, 1, 0)
def eyecuToString(tr):
""" takes an EyecuBST tree and generates a string containing
an inorder processing of the nodes of the tree. For each
node, the string contains the following information:
value, height, size, imbalance.
Returns the string
"""
if tr == None:
return ""
else:
thisNodeStr = "Value: " + str(tr.value) + ", Height: " + \
str(tr.height) + ", Size: " + str(tr.size) + ", Imbalance: " + \
str(tr.imbalance) + "\n"
return eyecuToString(tr.left) + thisNodeStr + eyecuToString(tr.right)
def insert(tr, el):
""" function to insert an element into a binary search tree
following the rules of binary search trees.
return: an updated tree
precondition: assumed all elements unique
"""
if tr == None:
#print('inserting node root')
return createEyecuBST(el, None)
tr.height = 1
else:
if tr.value > el: #if node is greater than element
if tr.left == None:
tr.left = createEyecuBST(el, tr) #create new node
#print('inserting node left')
tr.size += 1 # size of tree + 1
return tr # return new tree
else:
insert(tr.left, el)
return tr
if tr.left == None:
if tr.right == None:
tr.height = 1
else:
tr.height = tr.right.height + 1
else:
if tr.right == None:
tr.height = tr.left.height + 1
else:
tr.height = max(tr.left.height, tr.right.height) + 1
else:
if tr.right == None:
tr.right = createEyecuBST(el, tr)
#print('inserting node right')
tr.size += 1
return tr
else:
insert(tr.right, el)
return tr
if tr.right == None:
if tr.left == None:
tr.height = 1
else:
tr.height = tr.left.height + 1
else:
if tr.left == None:
tr.height = tr.right.height + 1
else:
tr.height = max(tr.left.height, tr.right.height) + 1
def treeHeight(tr):
"""
Returns the height of the tree rooted at this node. Returns -1
if input tr is an empty tree (None).
"""
if tr is None:
return -1
else:
return tr.height
def treeSize(tr):
"""
Returns the size of the tree rooted at target node. Returns 0
is input tr is an empty tree (None)
"""
if tr is None:
return 0
else:
return tr.size
def treeImbalance(tr):
"""
Returns the imbalance of the tree rooted at target node. Returns 0
if input tr is an empty tree (None)
"""
#if tr is None:
# return 0
#else:
def findNode(tr, val):
""" finds the target node in the tree. Returns the node reference.
Returns None if the node is not in the tree.
precondtion: val is non-negative integer.
"""
# replace with your findNode function code
return None
Any suggestions? I'm trying to get the computation time to speed up. on my insert function, as well as how to keep track of the imbalance.
Can you post the whole code? Is your insert function inside of a class? I feel like you did way too much work. I need to see more of your code in order to help you.

Resources