How to yield all elements on each depth level of BST? - python

I am trying to create generator which will give me values from next depth level of BST. Let's say that our tree will look like:
I would like to get result for following code like:
for level in tree:
print(level)
# 3
# (1,5)
# (2,4,7)
I was testing different variations of yield usage, but none of them gave me expected result. Here is code which I ended with:
class Node:
def __init__(self, element):
self.left = None
self.right = None
self.data = element
def __iter__(self):
yield self.data
if self.left and self.right:
yield from self.left.__iter__() and self.right.__iter__()
elif self.left:
yield from self.left.__iter__()
elif self.right:
yield from self.right.__iter__()
else:
pass
def add_element(self, element):
if self.data > element:
if self.left is None:
self.left = Node(element)
else:
self.left.add_element(element)
else:
if self.right is None:
self.right = Node(element)
else:
self.right.add_element(element)
def get_structure(self):
return (self.left.get_structure() if self.left else None, self.data, self.right.get_structure() if self.right else None)
This is given example from top:
from node import *
tree = Node(3)
tree.add_element(5)
tree.add_element(1)
tree.add_element(2)
tree.add_element(4)
tree.add_element(7)
print('Structure:',tree.get_structure()) #result: (None, 1, (None, 2, None)), 3, ((None, 4, None), 5, (None, 7, None)))
print('Iteration:', end=" ") #reslut: 3 5 7 / expected result: 3 (1,5) (2,4,7)
for level in tree:
print(level, end=" ")
Explain me please how can I fix my __iter__ method to get values from each depth level of tree?

Related

Binary Search Tree Delete method Deletes Whole Subtree

I have been learning data structures and have been trying to implement the delete method in my BST class. What I have noticed is that instead of deleting one node it will delete the whole subtree that the node is a part of.
class BST:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def addchild(self, child):
if child == self.data:
return
if child < self.data:
if self.left:
self.left.addchild(child)
else:
self.left = BST(child)
else:
if self.right:
self.right.addchild(child)
else:
self.right = BST(child)
def search(self, data):
if self.data == data:
return True
if data < self.data:
if self.left:
return self.left.search(data)
else:
return False
else:
if self.right:
return self.right.search(data)
else:
return False
def iot(self):
vals = []
if self.left:
vals += self.left.iot()
vals.append(self.data)
if self.right:
vals += self.right.iot()
return vals
def findmax(self):
if self.right:
return self.right.findmax()
else:
return self.data
def findmin(self):
if self.left:
return self.left.findmin()
else:
return self.data
def delete(self, data):
if data < self.data:
if self.left:
self.left = self.left.delete(data)
elif data > self.data:
if self.right:
self.right = self.right.delete(data)
else:
if self.left is None and self.right is None:
return None
elif self.left is None:
return self.right
elif self.right is None:
return self.left
minval = self.right.findmin()
self.data = minval
self.right = self.right.delete(minval)
def buildtree(arr):
r = BST(arr[0])
for i in range(1, len(arr)):
r.addchild(arr[i])
return r
So when input with these values and deleting the number 1 from the tree it will print this
bst = buildtree([34, 7, 8, 1, 3, 4, 5, 10, 65, 98, 100, 203])
print(bst.iot())
bst.delete(1)
print(bst.iot())
1st print statement output: [1, 3, 4, 5, 7, 8, 10, 34, 65, 98, 100, 203]
2nd print statement output: [34, 65, 98, 100, 203]
When I debug in Pycharm, before leaving the delete method and after all steps are executed the tree will show that the left subtree is intact and everything is how it should be. However once I leave the method the left subtree becomes None.
Any help would be appreciated.
Here's a strong hint:
You are missing a return statement on final block of you code:
minval = self.right.findmin()
self.data = minval
self.right = self.right.delete(minval)
A debug answer:
In your example 1 is going to be the leftmost leaf in the tree.
So look what’s happening:
When you start the method delete you say that
if data < self.data:
if self.left:
self.left = self.left.delete(data)
So basically the left son of the root will hold whatever comes at the end of the process. If you will follow the left branch until you get 1, you will see that you get the the case of
else:
if self.left is None and self.right is None:
return None
This means that the root left son is None, and this explains your bug

How do I make TreeNode from list by Python

Lately, I started "leetcode" for studying programming. Sometimes, I encounter the question which is related to TreeNode.
https://leetcode.com/problems/longest-univalue-path/
I usually run code in local to make sure if my code work. But those questions require me to prepare for TreeNode in advance, otherwise, I can not run in local. I don't know how to build TreeNode from a list.
I want to make TreeNode from a list by Python, like here.
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None
input: [5,4,5,1,1,5]
output:
TreeNode{val: 5, left: TreeNode{val: 4, left: TreeNode{val: 1, left: None, right: None}, right: TreeNode{val: 1, left: None, right: None}}, right: TreeNode{val: 5, left: TreeNode{val: 5, left: None, right: None}, right: None}}
I know we can make sure whether the code work or not on leetcode. However, I think it's slow for me to check the code on leetcode. I would like to run my code in local. I hope you will help me.
Take a look at LeetCode's official explanation https://support.leetcode.com/hc/en-us/articles/360011883654-What-does-1-null-2-3-mean-in-binary-tree-representation- of how their serialized formatting of a binary tree into the kind of list you see in their test cases works. If you want to run your solution against those test cases locally, you'll also need to write some code (or I'm sure you can find some online) that will input a serialized list, build the tree, and return the tree's root TreeNode so you can pass it to your find_longest_univalue_path function.
Guess this is what you need:
class TreeNode(object):
def __init__(self, x):
self.val = x
self.left = None
self.right = None
def creatBTree(data, index):
pNode = None
if index < len(data):
if data[index] == None:
return
pNode = TreeNode(data[index])
pNode.left = creatBTree(data, 2 * index + 1) # [1, 3, 7, 15, ...]
pNode.right = creatBTree(data, 2 * index + 2) # [2, 5, 12, 25, ...]
return pNode
Say you are cracking pathSum, populate the tree by calling
lst = [5,4,8,11,None,13,4,7,2,None,None,None,1]
root = creatBTree(lst, 0)
Here is just beautified StefanPochmann's solution which was described at
LeetCode's Help Center
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __repr__(self):
return 'TreeNode({})'.format(self.val)
def deserialize(string):
if string == '[]':
return None
nodes = [None if val == 'null' else TreeNode(int(val))
for val in string.strip('[]').split(',')]
kids = nodes[::-1]
root = kids.pop()
for node in nodes:
if node:
if kids:
node.left = kids.pop()
if kids:
node.right = kids.pop()
return root
if __name__ == '__main__':
tree = deserialize('[3,9,20,null,null,15,7]')
assert tree == TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))
Here's a drop-in replacement for the incomplete class LeetCode provides in questions such as https://leetcode.com/problems/invert-binary-tree/
import json
class TreeNode:
def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right
def __repr__(self):
# Shrink the long json string by removing unnecessary lines
t = '\n'.join([l for l in self.json_repr.split('\n')
if '[' not in l and ']' not in l])
return t.replace(',','')
#property
def json_repr(self):
if self.val is None:
return 'None'
# Recursively construct a json-compliant representation
if self.left is None:
text = f"[{self.val}]"
else:
text = f"[{self.val}, [{self.left.json_repr}, {self.right.json_repr}]]"
return json.dumps(json.loads(text), indent=1)
def from_list(l):
nodes = [TreeNode(v) for v in l]
kids = nodes[::-1]
root = kids.pop()
for node in nodes:
if node:
if kids:
node.left = kids.pop()
if kids:
node.right = kids.pop()
return root
t = TreeNode.from_list([4,2,7,1,3,6,9])
print(t)
Returns:
4
2
1
3
7
6
9

Sum of binary tree leaves' values

I wrote this code and when I use print I see that I get the leaves. However, the final return from the function is None and not the sum of the leaves, which is supposed to be 7 in this example. I'd be happy to know whats wrong here. Thank you !
class Node:
def __init__(self, val=None):
self.left = None
self.right = None
self.val = val
def sum_leafs(tree):
if tree is None:
return 0
if tree.right and tree.left:
sum_leafs(tree.right)
sum_leafs(tree.left)
elif tree.right or tree.left:
if tree.right:
sum_leafs(tree.right)
elif tree.left:
sum_leafs(tree.left)
elif tree.right is None and tree.left is None:
return sum_leafs(tree.left) + 1
node = Node(10)
node.right = Node(2)
node.left = Node(11)
node.left.right = Node(5)
print(sum_leafs(node))
You forgot to add + when you sum the branches (left/right) and also you forgot to access val which is the most crucial thing for the whole thing to work.
Further, the logic can be simplified:
def sum_leafs(tree):
if tree is None:
return 0
if not tree.right and not tree.left:
return tree.val
return sum_leafs(tree.right) + sum_leafs(tree.left)
You are not adding the sums together or returning them. This can also be done with a method in the class:
class Node:
def __init__(self, val=None):
self.left = None
self.right = None
self.val = val
def sum(self):
s = 0
if self.left is not None:
s += self.left.sum()
if self.right is not None:
s += self.right.sum()
return self.val + s
node = Node(10)
node.right = Node(2)
node.left = Node(11)
node.left.right = Node(5)
print(node.sum())
returns:
28
You are not properly returning the calculated leaf sums. Try this:
class Node:
def __init__(self, val=None):
self.left = None
self.right = None
self.val = val
def sum_leafs(tree):
if tree is None:
return 0
elif tree.right and tree.left:
return sum_leafs(tree.right) + sum_leafs(tree.left)
elif tree.right or tree.left:
if tree.right:
return sum_leafs(tree.right)
elif tree.left:
return sum_leafs(tree.left)
elif tree.right is None and tree.left is None:
return tree.val
node = Node(10)
node.right = Node(2)
node.left = Node(11)
node.left.right = Node(5)
print(sum_leafs(node))
7
node
First I'm going to update your Node interface so that it's possible to set left and right branches when creating nodes -
class Node:
def __init__(self, val=None, left=None, right=None):
self.left = left
self.right = right
self.val = val
This allows us to create tress more ergonomically, such as -
t = Node(10, Node(11, None, Node(5)), Node(2))
traverse
Now we write a generic traverse procedure. This allows us to separate 1) the traversal of our tree from 2) the intended operation we want to perform on each tree element -
def traverse(tree):
if tree is None:
return
else:
yield tree.val
yield from traverse(tree.left)
yield from traverse(tree.right)
Now the need for sum_leafs disappears. We have decoupled traversal logic from summing logic. We can calculate the sum of leafs with a simple combination of sum and traverse -
print(sum(traverse(t)))
# 28
don't repeat yourself
Or, instead of summing the values, we could write a search function to find the first value that passes a predicate -
def search(test, tree):
for val in traverse(tree):
if test(val):
return val
print(search(lambda x: x < 10, t))
# 5
print(search(lambda x: x > 99, t))
# None
Or, we could simply collect each value into a list -
print(list(traverse(t)))
# [ 10, 11, 5, 2 ]
As you can see, removing the traversal logic from each function that depends on our tree can be a huge help.
without generators
If you don't like generators, you can write the eager version of traverse which always returns a list. The difference now is there is no way to partially traverse the tree. Note the similarities this program shares with the generator version -
def traverse(t):
if t is None:
return [] # <-- empty
else:
return \
[ t.val
, *traverse(t.left) # <-- yield from
, *traverse(t.right) # <-- yield from
]
print(traverse(t))
# [ 10, 11, 5, 2 ]

How to fix yield usage in this BST implementation?

I would like to get values from tree as generator to iterate over them. In this case yield just gives me a single value. Function print_tree works as expected - it shows values from smallest to biggest. How to get similar result, but with catching them one by one? This is my BST implementation:
class Node:
def __init__(self, element):
self.left = None
self.right = None
self.data = element
def add_element(self, element):
if self.data > element:
if self.left is None:
self.left = Node(element)
else:
self.left.add_element(element)
else:
if self.right is None:
self.right = Node(element)
else:
self.right.add_element(element)
def print_tree(self):
if self.left: self.left.print_tree()
print(self.data, end=' ')
if self.right: self.right.print_tree()
def get_values(self):
if self.left: self.left.get_values()
yield self.data
if self.right: self.right.get_values()
def get_structure(self):
return (self.left.get_structure() if self.left else None, self.data, self.right.get_structure() if self.right else None)
Code for testing:
from node import *
x = Node(3)
x.add_element(5)
x.add_element(1)
x.add_element(7)
print(x.get_structure()) #result: ((None, 1, None), 3, (None, 5, (None, 7, None)))
x.print_tree() #result: 1 3 5 7
z = x.get_values()
print(list(z)) #result: [3]
Could you please explain me where am I making a mistake and how can I correct it?
First you yield all elements from the left subtree, then the node value and finally all elements from the right subtree
def get_values(self):
if self.left:
yield from self.left.get_values()
yield self.data
if self.right:
yield from self.right.get_values()

How recursion keeps the function values in python?

I am on the way of learning data structures and facing a problem which is related with python and recursion.
I have a function (below - called update_BST) and in one version of the code doesn't keep the correct value from recursion.
I provide the two version of the code snippet.
Thanks
"""
Given a Binary Search Tree (BST),
modify it so that all greater values in
the given BST are added to every node.
For example, consider the following BST.
50
/ \\
30 70
/ \ / \\
20 40 60 80
The above tree should be modified to following
260
/ \\
330 150
/ \ / \\
350 300 210 80
"""
class Node:
def __init__(self, data):
self.data = data
self.right = None
self.left = None
def insert(self, data):
if self.data == data:
return False
elif self.data > data:
if self.left:
self.left.insert(data)
else:
self.left = Node(data)
else:
if self.right:
self.right.insert(data)
else:
self.right = Node(data)
def in_order(self):
if self:
if self.left:
self.left.in_order()
print(self.data)
if self.right:
self.right.in_order()
class BST:
def __init__(self):
self.root = None
def insert(self, data):
if self.root:
self.root.insert(data)
else:
self.root = Node(data)
return True
def in_order(self):
if self.root is not None:
self.root.in_order()
bst = BST()
arr = [50, 30, 20, 40, 70, 60, 80]
for i in arr:
bst.insert(i)
def update_BST(node, temp):
if node == None:
return
update_BST(node.right, temp)
temp[0] = temp[0] + node.data
node.data = temp[0]
update_BST(node.left, temp)
update_BST(bst.root, [0])
bst.in_order()
This codes works as it suppose to work. It gives back the right values.
BUT, I don't understand why it is not working if I use -- 0 -- instead of the -- [0] -- and of course modifying the reamining code like:
def update_BST(node, temp):
if node == None:
return
update_BST(node.right, temp)
temp = temp + node.data
node.data = temp
update_BST(node.left, temp)
update_BST(bst.root, 0)
So the question is why I need to use the [0] - why the simple integer 0 is not working?

Categories