so I have been working on this class project implementing a binary search tree. The professor wants us to make the private recursive while make the public one simple. (like when to insert_element(50), it calls a private function recursive_insert(50, self.__root) to solve).
My insertion function runs for no error yet the test case always return empty, and here are my codes for the private functions:
class Binary_Search_Tree:
class __BST_Node:
def __init__(self, value):
self.value = value
self.left=None
self.right=None
def __init__(self):
self.__root = None
self.__height=0
self.__size=0
def _in_order_str(self, root):
if root is None:
outcome= "[ ]"
elif self.__size==1:
outcome = "[ " + str(root.value) + " ]"
else:
outcome = "[ "
self._in_order_str(root.left)
outcome += str(root.value) +", "
self._in_order_str(root.right)
outcome+= " ]"
return outcome
def _recur_ins(self, val,root):
if root is None:
root=Binary_Search_Tree.__BST_Node(val)
elif root.value>val:
root.left = _recur_ins(val,root.left) #do I need self here?
elif root.value <val:
root.right = _recur_ins(val,root.right)
return root
And this one is for the public:
def insert_element(self, value):
self._recur_ins(value,self.__root)
self.__size+=1
My Test Case:
def test_insertion_from_empty(self):
root=None
self.__bst.insert_element(50)
self.__bst.insert_element(30)
self.__bst.insert_element(70)
self.assertEqual('[ 30, 50, 70 ]', self.__bst.in_order())
UPDATE: I think the problem comes from my _in_order_str(self, root): method. The general case I found online is:
def inorder(root):
if root is not None:
inorder(root.left)
print root.key
inorder(root.right)
I know this could be a very silly question, but I really failed to figure it our by myself. Any help will be appreciated so thank you so much!!!
After changing your code as little as possible, I think I have managed to get it working.
from pprint import pprint # For debugging
class Binary_Search_Tree:
class __BST_Node:
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def __init__(self):
self.__root = None
self.__height = 0
self.__size = 0
def __in_order_str(self, root):
if root is None:
outcome = "[ ]"
elif self.__size == 1:
outcome = "[ " + str(root.value) + " ]"
else:
outcome = "[ "
self.__in_order_str(root.left)
outcome += str(root.value) + ", "
self.__in_order_str(root.right)
outcome += " ]"
return outcome
def __recur_ins(self, val, root):
if root is None:
root = Binary_Search_Tree.__BST_Node(val)
elif root.value > val:
root.left = self.__recur_ins(val, root.left)
elif root.value < val:
root.right = self.__recur_ins(val, root.right)
return root
def insert_element(self, value):
self.__root = self.__recur_ins(value, self.__root)
self.__size += 1
def test_insertion_from_empty(self):
self.insert_element(50)
self.insert_element(60)
self.insert_element(70)
# self.assertEqual('[ 30, 50, 70 ]', self.__bst.in_order())
test = Binary_Search_Tree()
test.test_insertion_from_empty()
pprint(vars(test))
Notes on the changes:
changed some functions(_recur_ins, _in_order_str) from using '_' to '__' to make them private functions. I did it based on Python Official Documentation, private functions use at least two leading underscores and at most one trailing underscore.
First line in insert_element, added 'self.__root= ' so that the returned root value will be stored as the new root
Added 'self.' in front of '__recur_ins', since as far as I know, you must use self whenever you need to call a function which is located at the same class.
I did not modify anything much in __in_order_str, since I think the author only asked for the insertion (?)
commented assertEqual, since no function is provided in the question (?)
Modified the spaces so that it can be more readable
If I put the debug mode right before I dumped the variable, this is what I get:
Which I think should be correct. 50 is inserted first, thus it is used as the root, then 60 is inserted on the right child of 50, and 70 is put on the right child of 60.
Note: I'm also just a novice, please tell me any mistakes that I have done and I will rectify it :)
Related
Very new at Python and I'm trying to understand recursion over a binary tree. I've implemented a very simple tree, which funnily enough maps English characters to binary (1's and 0's). I've only used a very simple structure because I am struggling to get my head round a more complex question that I've been set. I figure if I can get my head round my example then I should be able to go away and look at the question I've been set myself.
The following creates the class BinaryTree and an instance of this
class BinaryTree:
"""A rooted binary tree"""
def __init__(self):
self.root = None
self.left = None
self.right = None
def is_empty(testtree: BinaryTree) -> bool:
"""Return True if tree is empty."""
return testtree.root == testtree.left == testtree.right == None
def join(item: object, left: BinaryTree, right: BinaryTree) -> BinaryTree:
"""Return a tree with the given root and subtrees."""
testtree = BinaryTree()
testtree.root = item
testtree.left = left
testtree.right = right
return testtree
EMPTY = BinaryTree()
C = join('C',EMPTY,EMPTY)
D = join('D',EMPTY,EMPTY)
E = join('E',EMPTY,EMPTY)
F = join('F',EMPTY,EMPTY)
A = join('A',C,D)
B = join('B',E,F)
BINARY = join('START',B,A)
I visualise it as follows
Visualisation of the Binary tree
Now I'm trying to create a function that will take two inputs, a BinaryTree and a single character and the output will be the binary code for the corresponding letter (as an example, D = " 10 "). I'm outputting as a string rather than an integer. My function and test case as follows
# global variable
result = ''
#Convert binary to letter
def convert_letter(testtree: BinaryTree, letter: str) -> str:
global result
if testtree == None:
return False
elif testtree.root == letter:
return True
else:
if convert_letter(testtree.left, letter) == True:
result += "1"
return result
elif convert_letter(testtree.right, letter) == True:
result += "0"
return result
#Test
test = 'D' #Return '10'
convert_letter(BINARY, test)
And unfortunately that's where I'm hitting a brick wall. I had tried initialising an empty string within the function, but everytime it iterates over the function it overwrites the string. Any help greatly appreciated.
The problem is that your function will sometimes return a boolean, sometimes a string, and sometimes None. So with this code:
if convert_letter(testtree.left, letter) == True:
result += "1"
return result
elif convert_letter(testtree.right, letter) == True:
result += "0"
return result
... you are not capturing all successful searches, as a successful search would return the actual string of "0" and "1" which obviously is not True. In that case the execution has no else to go to and returns None -- even when the letter was found in a deeper node.
Your function should not return a boolean -- that doesn't match the type hint either. It should be a string (the result). You could reserve None to indicate the letter was not found.
Some other problems:
result += "0" will append the digit, but since you already made the recursive call, you need to prepend the digit -- as you are higher up in the tree now.
The initialisation of your tree makes a different tree than you put in the image: A should be the left child, not the right child. So it should be join('START', A, B)
With those fixes, you'd have this code:
def convert_letter(testtree: BinaryTree, letter: str) -> str:
global result
if testtree is None:
result = None # Not found here
elif testtree.root == letter:
result = '' # Found! Start a path
elif convert_letter(testtree.left, letter) is not None:
result = "1" + result # Prepend
elif convert_letter(testtree.right, letter) is not None:
result = "0" + result # Prepend
else:
result = None # Not found here
return result
If you also correct to use join('START', A, B), then the output will be 10.
Better Practice
There are some things you can do better:
Don't use a global variable for storing the function result. As you return it, you can capture the result you get from a recursive call as a local variable, prepend to it, and return it again.
The definition of EMPTY makes your tree unnecessarily big. Just use None to denote an empty tree.
Don't call a node's value root. A rooted tree has only one root, and it is a node, not a value of a node. So call that attribute value or data, but not root.
The join function is nice, but why not use the constructor for that feature? The constructor can take those arguments as optional and immediately initialise the left and right attributes with those arguments.
The code-comment above the convert_letter function describes the opposite from what the function does.
Taking all that into account, your code could look like this:
class BinaryTree:
def __init__(self, value, left: 'BinaryTree'=None, right: 'BinaryTree'=None):
self.value = value
self.left = left
self.right = right
def convert_letter(tree: BinaryTree, letter: str) -> str:
if not tree:
return # Not found here, return None
if tree.value == letter:
return "" # Bingo: return an empty path
# No more global. path is a local variable
path = convert_letter(tree.left, letter)
if path is not None:
return "1" + path
path = convert_letter(tree.right, letter)
if path is not None:
return "0" + path
# Look how nice it is to create a tree using the constructor arguments
binary = BinaryTree("Start",
BinaryTree("A",
BinaryTree("C"), BinaryTree("D")
),
BinaryTree("B",
BinaryTree("E"), BinaryTree("F")
)
)
# Test
test = 'D'
print(convert_letter(binary, test)) # 10
I took the liberty of simplfying your code a bit let me know if you have any questions about how this works.
class node:
"""A rooted binary tree"""
def __init__(self, value = None, left = None, right = None):
self.value = value
self.left = left
self.right = right
C = node('C')
D = node('D')
E = node('E')
F = node('F')
A = node('A',C,D)
B = node('B',E,F)
BINARY = node('START',B,A)
def convert_letter(n,letter):
if n.value == letter:
return "1"+(convert_letter(n.left,letter) if not n.left is None else "")+(convert_letter(n.right,letter)if not n.right is None else "")
else:
return "0"+(convert_letter(n.left,letter) if not n.left is None else "")+(convert_letter(n.right,letter)if not n.right is None else "")
def walk(n):
return n.value+(walk(n.left) if not n.left is None else "")+(walk(n.right) if not n.right is None else "")
test = 'D'
print(convert_letter(BINARY, test))
print(walk(BINARY))
This is not how I would personally structure an answer, but I think it most closely follows what you are attempting. The shortcoming of your answer only being that you are only returning one value, but kind of tracking two values. Note, I have taken the liberty of correcting:
BINARY = join('START',A,B)
Let's modify your method to return both a Boolean indicating if the letter was found as well as the indicator of the path.
def convert_letter2(testtree: BinaryTree, letter: str):
if not testtree:
return (False, "")
if testtree.root == letter:
return (True, "")
test, val = convert_letter2(testtree.left, letter)
if test:
return (True, "1" + val)
test, val = convert_letter2(testtree.right, letter)
if test:
return (True, "0" + val)
return (False, "")
Then if we:
print(convert_letter2(BINARY, "D")[1])
We should get back "10"
Why this code for insertion into binary search tree not working?
class BinaryTreeNode:
def __init__(self,key):
self.key=key
self.left=None
self.right=None
def insert(root,data):
if root is None:
root=BinaryTreeNode(data)
else:
if data>root.key:
insert(root.right,data)
else:
insert(root.left,data)y
The logic of the code seems perfectly fine but I have one concern .
In this code , is it returning anything ?
class BinaryTreeNode:
def __init__(self,key):
self.key=key
self.left=None
self.right=None
def insert(root,data):
if root is None:
root=BinaryTreeNode(data)
else:
if data>root.key:
insert(root.right,data)
else:
insert(root.left,data)
return
I added a return in your insert method , maybe it will be able to run now .Due to missing return , your method will keep calling itself , but if it won't return the stack will not close , resulting in your program getting stuck forever .
I have a insert method created as well , maybe you could review and test if you missed out somewhere .
def insert(self, val):
treeNode = Node(val)
placed = 0
tmp = self.root
if not self.root:
self.root = treeNode
else:
while(not placed):
if val<tmp.info:
if not tmp.left:
tmp.left = treeNode
placed = 1
else:
tmp = tmp.left
else:
if not tmp.right:
tmp.right = treeNode
placed = 1
else:
tmp = tmp.right
return
Below code is a simple implementation of BFS in Python. I able to print the values level by level from a tree. However when I want to search a element and print it . I am not able to do it. Whts is the error?
def search_bfs(self,root,key):
q=QueueClass()
q.enqueue(root)
while q.size() > 0:
curr_node = q.dequeue()
#print curr_node
#print key
if curr_node == key:
print curr_node
break
if curr_node.left is not None:
q.enqueue(curr_node.left)
if curr_node.right is not None:
q.enqueue(curr_node.right)
from QueueClass import QueueClass
class Node:
def __init__(self,data):
self.data=data
self.left=None
self.right=None
def __str__(self):
return str(self.data)
class searchtree:
def __init__(self):
self.root = None
def create(self,val):
if self.root == None:
self.root=Node(val)
else:
current=self.root
while 1:
if val < current.data:
if current.left:
current=current.left
else:
current.left=Node(val)
break
if val > current.data:
if current.right:
current=current.right
else:
current.right=Node(val)
break
else:
break
tree=searchtree()
lst=[3,1,2,6,4,5,8,12]
for i in lst:
tree.create(i)
tree.search_bfs(tree.root, 3)
You really make it hard to reproduce your problem! So here's what I did:
class QueueClass(object):
def __init__(self):
self.l = []
def size(self): return len(self.l)
def enqueue(self, it): self.l.append(it)
def dequeue(self): return self.l.pop()
class Node:
def __init__(self, name, left=None, right=None):
self.name = name
self.left = left
self.right = right
def __str__(self):
return '{}:{}/{}'.format(self.name, self.left, self.right)
root = Node('root')
adding all the code you omitted (more than you supplied!-).
And now, adding your code exactly as reported, the call:
search_bfs(None, root, root)
emits
root:None/None
exactly as desired and contrary to your report.
It follows that your bug is in some of code you didn't show us, not in the coded you did show.
You either have a buggy queue-class, or are building a different tree than you thought, or searching for a node that is not actually in the tree.
Hard to debug code you're now showing, you know.
Added: so now I've integrated the extra code per your edit and at the end I have:
st = searchtree()
st.create('imtheroot')
st.search_bfs(st.root, st.root)
and of course it prints imtheroot as expected.
Is your bug perhaps STILL hiding in parts you're not yet showing, e.g instead of looking for a node you may be looking for something else?
E.g, if the final call was erroneously st.search_bfs(st.root, 'imtheroot') then obviously the search would fail -- you're checking equality of the key parameter with a node, so key clearly must be a node. not a string or other things (unless the Node class defines a very, very peculiar __eq__ method, which the one you've shown fortunately doesn't:-).
I think the issue is that when you do if curr_node == key, curr_node is a Node object, which has an integer .data attribute, but key is the integer value.
So I think you just need to use if curr_node.data == key.
Here is the Class named BNode,
class BNode:
def __init__(self, value=None, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __repr__(self):
return '' % (self.value, self.left, self.right)
and print out below
>>> root = BNode('root')
>>> root.left = BNode('left')
>>> root.right = BNode('right')
>>> root.left.left = BNode('left-left')
>>> root.left.right = BNode('left=right')
>>> print root
root (
left (
left-left (
None
None)
left-right (
None
None))
right (
None
None))
Q) Modify the Class, so that the result like below could be made.
root (
left (
left-left (
None
None)
left-right (
None
None))
right (
None
None))
and my answer is the following one.
class BNode:
def __init__(self, value=None, left=None, right=None):
self.value = value
self.left = left
self.right = right
def __repr__(self, level=0):
R = ''
for ele in range(level):
R += '\t'
R += str(self.value)
R += ' ('
R += '\n'
if isinstance(self.left, BNode):
R += BNode.__repr__(self.left, level+1)
else:
for ele in range(level+1):
R += '\t'
R += str(self.left)
R += '\n'
if isinstance(self.right, BNode):
R += BNode.__repr__(self.right, level+1)
else:
for ele in range(level+1):
R += '\t'
R += str(self.right)
R += ')'
return R
But I want to know if it is the best solution for this question.
I think there is much nicer one...
Is there some efficient way?
Thanks in advance ~ :)
First, I'd kill the kwarg on __repr__ - as a dundermethod, it's code path should be exclusively for handling repr() calls, etc. Just move the current code to a .format_node() (or w/e) method that has the kwarg, with __repr__ kicking it off with level=0.
Also, check out the textwrap stdlib module - with the right mix of subsequent_indent, drop_whitespace, and super-large width kwargs, it might be able to do what you need, but the source is among the more accessible of the stdlib modules, check it out for some ideas on the general approach.
http://docs.python.org/2/library/textwrap.html#textwrap.TextWrapper
http://hg.python.org/cpython/file/80e9cb6163b4/Lib/textwrap.py
You're using a recursive method, which is good.
However, you're not using methods correctly - you don't need to call them as BNode.__repr__(self.left, level+1). Instead, you can do: self.left.__repr__(level+1)
Likewise, to create a sequence filled with the same item, instead of an explicit loop, you can use *: '\t'*level
In general, python has a number of string manipulation facilities which you'll find useful: http://docs.python.org/2/library/string.html
Finally, you might like to create a separate tree-oriented map method to do the traversal, and separately write the function which does the work. See: http://rosettacode.org/wiki/Tree_traversal#Python
You can and should clean up your code with these changes.
I programmed a Trie as a class in python. The search and insert function are clear, but now i tried to programm the python function __str__, that i can print it on the screen. But my function doesn't work!
class Trie(object):
def __init__(self):
self.children = {}
self.val = None
def __str__(self):
s = ''
if self.children == {}: return ' | '
for i in self.children:
s = s + i + self.children[i].__str__()
return s
def insert(self, key, val):
if not key:
self.val = val
return
elif key[0] not in self.children:
self.children[key[0]] = Trie()
self.children[key[0]].insert(key[1:], val)
Now if I create a Object of Trie:
tr = Trie()
tr.insert('hallo', 54)
tr.insert('hello', 69)
tr.insert('hellas', 99)
And when i now print the Trie, occures the problem that the entries hello and hellas aren't completely.
print tr
hallo | ellas | o
How can i solve that problem?.
Why not have str actually dump out the data in the format that it is stored:
def __str__(self):
if self.children == {}:
s = str(self.val)
else:
s = '{'
comma = False
for i in self.children:
if comma:
s = s + ','
else:
comma = True
s = s + "'" + i + "':" + self.children[i].__str__()
s = s + '}'
return s
Which results in:
{'h':{'a':{'l':{'l':{'o':54}}},'e':{'l':{'l':{'a':{'s':99},'o':69}}}}}
There are several issues you're running into. The first is that if you have several children at the same level, you'll only be prefixing one of them with the initial part of the string, and just showing the suffix of the others. Another issue is that you're only showing leaf nodes, even though you can have terminal values that are not at a leaf (consider what happens when you use both "foo" and "foobar" as keys into a Trie). Finally, you're not outputting the values at all.
To solve the first issue, I suggest using a recursive generator that does the traversal of the Trie. Separating the traversal from __str__ makes things easier since the generator can simply yield each value we come across, rather than needing to build up a string as we go. The __str__ method can assemble the final result easily using str.join.
For the second issue, you should yield the current node's key and value whenever self.val is not None, rather than only at leaf nodes. As long as you don't have any way to remove values, all leaf nodes will have a value, but we don't actually need any special casing to detect that.
And for the final issue, I suggest using string formatting to make a key:value pair. (I suppose you can skip this if you really don't need the values.)
Here's some code:
def traverse(self, prefix=""):
if self.val is not None:
yield "{}:{}".format(prefix, self.val)
for letter, child in self.children.items():
yield from child.traverse(prefix + letter)
def __str__(self):
return " | ".join(self.traverse())
If you're using a version of Python before 3.3, you'll need to replace the yield from statement with an explicit loop to yield the items from the recursive calls:
for item in child.traverse(prefix + letter)
yield item
Example output:
>>> t = Trie()
>>> t.insert("foo", 5)
>>> t.insert("bar", 10)
>>> t.insert("foobar", 100)
>>> str(t)
'bar:10 | foo:5 | foobar:100'
You could go with a simpler representation that just provides a summary of what the structure contains:
class Trie:
def __init__(self):
self.__final = False
self.__nodes = {}
def __repr__(self):
return 'Trie<len={}, final={}>'.format(len(self), self.__final)
def __getstate__(self):
return self.__final, self.__nodes
def __setstate__(self, state):
self.__final, self.__nodes = state
def __len__(self):
return len(self.__nodes)
def __bool__(self):
return self.__final
def __contains__(self, array):
try:
return self[array]
except KeyError:
return False
def __iter__(self):
yield self
for node in self.__nodes.values():
yield from node
def __getitem__(self, array):
return self.__get(array, False)
def create(self, array):
self.__get(array, True).__final = True
def read(self):
yield from self.__read([])
def update(self, array):
self[array].__final = True
def delete(self, array):
self[array].__final = False
def prune(self):
for key, value in tuple(self.__nodes.items()):
if not value.prune():
del self.__nodes[key]
if not len(self):
self.delete([])
return self
def __get(self, array, create):
if array:
head, *tail = array
if create and head not in self.__nodes:
self.__nodes[head] = Trie()
return self.__nodes[head].__get(tail, create)
return self
def __read(self, name):
if self.__final:
yield name
for key, value in self.__nodes.items():
yield from value.__read(name + [key])
Instead of your current strategy for printing, I suggest the following strategy instead:
Keep a list of all characters in order that you have traversed so far. When descending to one of your children, push its character on the end of its list. When returning, pop the end character off of the list. When you are at a leaf node, print the contents of the list as a string.
So say you have a trie built out of hello and hellas. This means that as you descend to hello, you build a list h, e, l, l, o, and at the leaf node you print hello, return once to get (hell), push a, s and at the next leaf you print hellas. This way you re-print letters earlier in the tree rather than having no memory of what they were and missing them.
(Another possiblity is to just descend the tree, and whenever you reach a leaf node go to your parent, your parent's parent, your parent's parent's parent... etc, keeping track of what letters you encounter, reversing the list you make and printing that out. But it may be less efficient.)