I am trying to write a function which given a binary search tree T of integers and a rectangular matrix M of integers, verify if there exists a row of M whose values belong to T.
This is my code:
M = [
[1, 2, 3],
[4, 5, 6]
]
class Tree:
def __init__(self, root=None, left=None, right=None):
self.root = root
self.left = left
self.rigth = right
def FindRowInTree(T, M):
if T is None:
return False
else:
for r in M:
for e in r:
if e == T.root and FindRowInTree(T.left, M) is True and FindRowInTree(T.right, M) is True:
return True
FindRowInTree(T.left, M) and FindRowInTree(T.right,M)
return False
t = Tree(4, Tree(5, None, None), Tree(6, None, None))
x = FindRowInTree(t, M)
print(x)
It always returns False.
What would I need to change to make it work properly?
Break the problem into pieces. First write a function to find a single value in the tree:
class Tree:
def __init__(self, root=None, left=None, right=None):
self.root = root
self.left = left
self.right = right
def __contains__(self, value):
return (
self.root == value
or self.left is not None and value in self.left
or self.right is not None and value in self.right
)
Note that with an ordered binary tree, you could make this function more efficient by having it only check left or right depending on how the value you're looking for compares to the root value; that's what a "binary search" is. Since your tree is unordered, though, you just need to search both children at each node, meaning you're traversing the entire tree.
In any case, once you have a function that looks up a single value, all you need to do is call it in a loop:
def find_row_in_tree(tree, matrix):
return any(
all(i in tree for i in row)
for row in matrix
)
If you're trying to do this in a more efficient way, an unordered binary tree is not doing you any favors; I'd just write a utility function to convert it to something more useful, like a set:
def tree_to_set(tree):
if tree is None:
return set()
return {tree.root} | tree_to_set(tree.left) | tree_to_set(tree.right)
def find_row_in_tree(tree, matrix):
tree_as_set = tree_to_set(tree)
return any(tree_as_set.issuperset(row) for row in matrix)
Related
I am having trouble find the duplicate subtrees.
In the following leetcode problem 652. Find Duplicate Subtrees
Given the root of a binary tree, return all duplicate subtrees. For each kind of duplicate subtrees, you only need to return the root node of any one of them. Two trees are duplicate if they have the same structure with the same node values.
My implementation is as follows:
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def __init__(self):
self.duplicates = []
def isSameSubtree(self, left, right):
if left is None or right is None:
return True
elif left is None:
return False
elif right is None:
return False
if left.val != right.val:
return False
return self.isSameSubtree(left.left, right.left) or \
self.isSameSubtree(left.right, right.right)
def helper(self, node):
if node is None:
return node
left = self.helper(node.left)
right = self.helper(node.right)
if self.isSameSubtree(left, right):
self.duplicates.append(left)
return left
def findDuplicateSubtrees(self, root: Optional[TreeNode]) -> List[Optional[TreeNode]]:
self.helper(root)
return self.duplicates
My thought process was that I would check both left and right subtrees by passing each parent node of each subtree into isSameSubtree method. Problem is I don't feel like I am doing any of this correctly. What I want is to analyze both left and right subtrees by doing what I am doing in my helper function and when I go back up the stack and find a parent node with both left and right subtrees that match return that parent node.
Here is a solution that is related to Merkle Trees. In fact, we can define a helper function (no need to modify the Node or Tree class, although of course that is also a possibility if preferred) as follow:
def sign(t):
"""depth-first iterator; for each node, yield (hash, node) tuple"""
hashes = (hash(t.data), )
for c in t.children or []:
for h, n in sign(c):
yield h, n
hashes = hashes + (h, )
yield hash(hashes), t
Usage example, on a simplistic Node structure:
class Node:
def __init__(self, data, children=None):
self.data = data
self.children = children
def __repr__(self):
if self.children:
return f'{self.data}:{self.children}'
return str(self.data)
Then, for example:
t = Node('a', (Node('b', (Node('c'), Node('d'))), (Node('b', (Node('c'), Node('d'))))))
>>> t
a:(b:(c, d), b:(c, d))
>>> list(sign(t))
[(-8022040514780200320, c),
(-5838450013443269355, d),
(-6612941109014678970, b:(c, d)),
(-8022040514780200320, c),
(-5838450013443269355, d),
(-6612941109014678970, b:(c, d)),
(-3704690573080571573, a:(b:(c, d), b:(c, d)))]
As you can see, the subtrees Node('d') all have the same fingerprint: -5838450013443269355, and all b:(c, d) nodes have -6612941109014678970 (Note: actual numerical values will depend on your PYTHONHASHSEED, the values given here are just examples). One very nice property of this is to be very lightweight, it is O[n] for n nodes (there are no needlessly repeated hash computations).
From there, it is trivial to detect repeated subtrees:
seen = set()
for h, c in sign(t):
if h in seen:
print(f'duplicate: {c}')
seen.add(h)
# on the example tree above, this prints:
# duplicate: c
# duplicate: d
# duplicate: b:(c, d)
Note, if you are allowed to modify your Node structure, then the solution is even simpler: add this to your Node structure:
def __hash__(self):
return hash((self.data, tuple(hash(c) for c in self.children or [])))
def __eq__(self, other):
return isinstance(other, Node) and self.data == other.data and self.children == other.children
You can then compute the hash of any Node and compare for equality between nodes that have the same hash. In practice, your duplicate detection becomes:
def dupes(t, seen=None):
if seen is None:
seen = set()
for c in t.children or []:
yield from dupes(c, seen)
if t in seen:
yield t
else:
seen.add(t)
Example:
>>> list(dupes(t))
[c, d, b:(c, d)]
I am writing some code for traversing a tree:
root = [1, None, 2, 3]
class Treenode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def post_order_traversal_iterative(self,root):
stack = [root]
output = []
if not root:
return []
while stack:
curr = stack.pop()
output.append(curr.val)
if curr.right:
stack.append(curr.right)
if curr.left:
stack.append(curr.left)
return output
x = Solution()
print(Solution.post_order_traversal_iterative(x,root))
But I am getting the error that a 'list object has no attribute val'- which I totally get, but then how do I pass in my list as the input??
There are a few issues in your code:
post_order_traversal_iterative expects a Treenode instance but you're passing it a list instead; you should do something like root = Treenode() and then x.post_order_traversal_iterative(root)
once again since you put on the stack the left and right values of the current node they too should be instances of Treenode, not integer
apart from being a list and not a Treenode your root contains four elements while three are requested
So assuming you were trying to create a mini tree with just a root node "1" with two children "2" and "3" you should have something like
rootnode = Treenode(val=1)
firstchild = Treenode(val=2)
secondchild = Treenode(val=3)
rootnode.left = firstchild
rootnode.right = secondchild
or more succinctly
rootnode = Treenode(val=1, left=Treenode(val=2), right=Treenode(val=3))
I need help completing the recursive part of my function. The function is supposed to use my ListBinaryTree Class to help reconstruct a tree given its inorder and preorder traversal in string format: eg.
preorder = '1234567'
inorder = '3241657'
def build_tree(inorder, preorder):
head = preorder[0]
print(head)
head_pos = inorder.index(head)
print(head_pos)
left_in = inorder[:head_pos]
print(left_in)
right_in = inorder[(head_pos+1):]
print(right_in)
left_pre = preorder[1:-len(right_in)]
print(left_pre)
right_pre = preorder[-len(right_in):]
print(right_pre)
Which finds the important values in the preorder and inorder traversals and splits the tree up to determine which numbers are for the left side and right side of the tree.
An example of its input and output is:
build_tree('3241657', '1234567')
.
1
3
324
657
234
567
My class that I use to create the tree is as follows:
class ListBinaryTree:
"""A binary tree class with nodes as lists."""
DATA = 0 # just some constants for readability
LEFT = 1
RIGHT = 2
def __init__(self, root_value, left=None, right=None):
"""Create a binary tree with a given root value
left, right the left, right subtrees
"""
self.node = [root_value, left, right]
def create_tree(self, a_list):
return ListBinaryTree(a_list[0], a_list[1], a_list[2])
def insert_value_left(self, value):
"""Inserts value to the left of this node.
Pushes any existing left subtree down as the left child of the new node.
"""
self.node[self.LEFT] = ListBinaryTree(value, self.node[self.LEFT], None)
def insert_value_right(self, value):
"""Inserts value to the right of this node.
Pushes any existing left subtree down as the left child of the new node.
"""
self.node[self.RIGHT] = ListBinaryTree(value, None, self.node[self.RIGHT])
def insert_tree_left(self, tree):
"""Inserts new left subtree of current node"""
self.node[self.LEFT] = tree
def insert_tree_right(self, tree):
"""Inserts new left subtree of current node"""
self.node[self.RIGHT] = tree
def set_value(self, new_value):
"""Sets the value of the node."""
self.node[self.DATA] = new_value
def get_value(self):
"""Gets the value of the node."""
return self.node[self.DATA]
def get_left_subtree(self):
"""Gets the left subtree of the node."""
return self.node[self.LEFT]
def get_right_subtree(self):
"""Gets the right subtree of the node."""
return self.node[self.RIGHT]
def __str__(self):
return '['+str(self.node[self.DATA])+', '+str(self.node[self.LEFT])+', '+\
str(self.node[self.RIGHT])+']'
For the recursive part of the function I tried doing something like:
my_tree= ListBinaryTree(head)
while my_tree.get_value() != None:
left_tree = build_tree(left_in, left_pre)
right_tree = build_tree(right_in, right_pre)
my_tree.insert_value_left(left_tree)
my_tree.insert_value_right(right_tree)
print (my_tree)
But it returns an "index out of range" error.
Also for something like:
def build_tree(inorder, preorder):
head = preorder[0]
head_pos = inorder.index(head)
left_in = inorder[:head_pos]
right_in = inorder[(head_pos+1):]
left_pre = preorder[1:-len(right_in)]
right_pre = preorder[-len(right_in):]
if left_in:
left_tree = build_tree(left_in, left_pre)
else:
left_tree = None
if right_in:
right_tree = build_tree(right_in, right_pre)
else:
right_tree = None
my_tree = ListBinaryTree(head, left_tree, right_tree)
print(my_tree)
input
build_tree('3241657', '1234567')
returns
[3, None, None]
[4, None, None]
[2, None, None]
[6, None, None]
[7, None, None]
[5, None, None]
[1, None, None]
Can anyone please help me with the recursive part?
Thanks
You're making the recursive part much harder than necessary.
if left_in:
left_tree = build_tree(left_in, left_pre)
else:
left_tree = None
if right_in:
right_tree = build_tree(right_in, right_pre)
else:
right_tree = None
return ListBinaryTree(head, left_tree, right_tree)
You could perhaps simplify it even further by moving the checks for empty sequences up to the top of the function (e.g. if not inorder: return None) so it only needs to appear once.
I am trying to find the kth smallest element of binary search tree and I have problems using recursion. I understand how to print the tree inorder/postorder etc. but I fail to return the rank of the element. Can someone point where I am making a mistake? In general, I am having hard time understanding recursion in trees.
Edit: this is an exercise, so I am not looking for using built-in functions. I have another solution where I keep track of number of left and right children as I insert nodes and that code is working fine. I am wondering if it is possible to do this using inorder traversal because it seems to be a simpler solution.
class BinaryTreeNode:
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
def traverseInOrder(root,order):
if root == None:
return
traverseInOrder(root.left,order+1)
print root.data,
print order
traverseInOrder(root.right,order)
"""
a
/ \
b c
/ \ / \
d e f g
/ \
h i
"""
h = BinaryTreeNode("h")
i = BinaryTreeNode("i")
d = BinaryTreeNode("d", h, i)
e = BinaryTreeNode("e")
f = BinaryTreeNode("f")
g = BinaryTreeNode("g")
b = BinaryTreeNode("b", d, e)
c = BinaryTreeNode("c", f, g)
a = BinaryTreeNode("a", b, c)
print traverseInOrder(a,0)
If this is an academic exercise, make traverseInOrder (or similar method tailored to the purpose) return the number of children it visited. From there things get simpler.
If this isn't academic, have a look at http://stromberg.dnsalias.org/~dstromberg/datastructures/ - the dictionary-like objects are all trees, and support iterators - so finding the nth is a matter of zip(tree, range(n)).
You could find the smallets element in the binary search tree first. Then from that element call a method to give you the next element k times.
For find_smallest_node method, note that you can traverse all the nodes "in-order" until reach to smallest. But that approach takes O(n) time.
However, you do not need a recursion to find the smallest node, because in BST smallest node is simply the left most node, so you can traverse the nodes until finding a node that has no left child and it takes O(log n) time:
class BST(object):
def find_smallest_node(self):
if self.root == None:
return
walking_node = self.root
smallest_node = self.root
while walking_node != None:
if walking_node.data <= smallest_node.data:
smallest_node = walking_node
if walking_node.left != None:
walking_node = walking_node.left
elif walking_node.left == None:
walking_node = None
return smallest_node
def find_k_smallest(self, k):
k_smallest_node = self.find_smallest_node()
if k_smallest_node == None:
return
else:
k_smallest_data = k_smallest_node.data
count = 1
while count < k:
k_smallest_data = self.get_next(k_smallest_data)
count += 1
return k_smallest_data
def get_next (self, key):
...
It just requires to keep the parent of the nodes when inserting them to the tree.
class Node(object):
def __init__(self, data, left=None, right=None, parent=None):
self.data = data
self.right = right
self.left = left
self.parent = parent
An implementation of the bst class with the above methods and also def get_next (self, key) function is here. The upper folder contains the test cases for it and it worked.
How would I return a tuple containing the number of interior nodes and the number of leaves?
Below is what I got so far, but it's doesn't seem to be working properly.
Also, does anyone know of a good website where I can learn binary trees and recursion with problem sets for more practice?
class BTNode:
'''Node in binary tree'''
def __init__(self, value, left, right):
'''
Create new BTNode with value and possible children left and right'''
self.value, self.left, self.right = value, left, right
def count_nodes(n:'BTNode') -> (int, int):
'''
Return a tuple containing the number of interior nodes and the number of
leaves in the tree rooted at n, or (0,0) if n is None.
'''
if not n:
return (0,0)
else:
left_internal, left_leaves = count_nodes(n.left)
right_internal, right_leaves = count_nodes(n.right)
internal, leaf = (1 if n.left or n.right else 0,
1 if not n.left and not n.right else 0)
return (left_internal + right_internal + internal,
left_leaves + right_leaves + leaf)
class BTNode:
'''Node in binary tree'''
def __init__(self, value, left=None, right=None):
'''
Create new BTNode with value and possible children left and right
'''
self.value, self.left, self.right = value, left, right
def count_nodes(self):
'''
Return a tuple containing the number of interior nodes and the number of
leaves in the tree rooted at n, or (0,0) if n is None.
'''
if self.left is None and self.right is None:
# leaf
return (0, 1)
else:
# internal node
left_nodes, left_leaves = (0, 0) if self.left is None else self.left.count_nodes()
right_nodes, right_leaves = (0, 0) if self.right is None else self.right.count_nodes()
return (left_nodes + 1 + right_nodes, left_leaves + right_leaves)