How do I traverse binary tree made up of nodes? (code inside) - python

I am given a class that creates a binary tree filled with nodes.each node is given a parent and a pointer to its left or right child.
Binary tree node class:
class BTNode():
''' a class that represents a binary tree node'''
def __init__(self, data, parent=None, left_child=None, right_child=None):
'''(BTNode, obj, BTNode, BTNode, BTNode) -> NoneType
Constructs a binary tree nodes with the given data'''
self._parent = parent
self._left = left_child
self._data = data
self._right = right_child
def set_parent(self, parent):
'''(BTNode, BTNode) -> NoneType
set the parent to the given node'''
self._parent = parent
def set_left(self, left_child):
'''(BTNode, BTNode) -> NoneType
set the left child to the given node'''
self._left = left_child
def set_right(self, right_child):
'''(BTNode, BTNode) -> NoneType
set the right child to the given node'''
self._right = right_child
def set_data(self, data):
'''(BTNode, obj) -> NoneType
set the data at this node to the given data'''
self._data = data
def get_parent(self):
'''(BTNode) -> BTNode
return the pointer to the parent of this node'''
return self._parent
def get_left(self):
'''(BTNode) -> BTNode
return the pointer to the left child'''
return self._left
def get_right(self):
'''(BTNode) -> BTNode
return the pointer to the right child'''
return self._right
def get_data(self):
'''(BTNode) -> obj
return the data stored in this node'''
return self._data
def has_left(self):
'''(BTNode) -> bool
returns true if this node has a left child'''
return (self.get_left() is not None)
def has_right(self):
'''(BTNode) -> bool
returns true if this node has a right child'''
return (self.get_right() is not None)
def is_left(self):
'''(BTNode) -> bool
returns true if this node is a left child of its parent'''
# you need to take care of exception here, if the given node has not parent
return (self.get_parent().get_left() is self)
def is_right(self):
'''(BTNode) -> bool
returns true if the given node is a right child of its parent'''
# you need to take care of exception here, if the given node has not parent
return (self.get_parent().get_right() is self)
def is_root(self):
'''(BTNode) -> bool
returns true if the given node has not parent i.e. a root '''
return (self.get_parent() is None)
code example of how to create a tree:
''' create this BT using BTNode
A
/ \
B C
/\ \
D E F
/
G
'''
node_G = BTNode("G")
node_F = BTNode("F", None,node_G)
node_G.set_parent(node_F)
node_C = BTNode("C", None, None, node_F)
node_F.set_parent(node_C)
node_D = BTNode("D")
node_E = BTNode("E")
node_B = BTNode("B",None, node_D, node_E)
node_D.set_parent(node_B)
node_E.set_parent(node_B)
node_A = BTNode("A",None, node_B, node_C)
node_B.set_parent(node_A)
I dont know how to traverse this tree. I was suggested using recursion but Im not sure how. For example, I need to return True if the tree differs in height by at most 1 level,so the tree above would return true. How do I do this? Thanks!

Try to think recursively. Let's start off with a few definitions.
A tree is balanced if its left and right trees have the same height and each of it's subtrees is balanced. Also we will define an empty tree as being balanced.
The height of a tree, h(t) = 1 + max(h(t.left), h(t.right)). In English, the height of a tree is 1 + the height of its taller child tree. Also we will assume that an empty tree has a height of 0.
So for every node in the tree we can check the height of both of its children and compare them. If they aren't equal we know the tree is not balanced and we return false.
Let's start by defining the code to check if a tree is balanced.
def is_balanced(node):
if node is None:
return True
left_height = get_height(node.get_left())
right_height = get_height(node.get_right())
return left_height == right_height and is_balanced(node.get_left()) and is_balanced(node.get_right())
Now let's define the function get_height that we used above. Since the height of a tree is a function of a height of it's subtrees we can use recursion. Since recursion requires a base case so we do not recurse infinitely we can use the fact that an empty tree has a height of 0.
def get_height(node):
if node is None:
return 0 # Assuming empty tree has a height of 0
return 1 + max(get_height(node.get_left()), get_height(node.get_right()))
Now to put it all together we can recursively iterate through the tree and check that every node is balanced by calling is_balanced on the root.
is_balanced(node_A)
BONUS Exercise:
The code I gave you will work but it won't scale well. If the tree gets very large it will run much slower. Why is it slow and what can you do to make it faster?

You can traverse the left and right sides of the tree to find the maximum path length to a leaf:
class Tree:
def __init__(self, **kwargs):
self.__dict__ = {i:kwargs.get(i) for i in ['value', 'left', 'right']}
def get_length(self, current=[]):
yield current+[1]
yield from getattr(self.left, 'get_length', lambda _:[])(current+[1])
yield from getattr(self.right, 'get_length', lambda _:[])(current+[1])
def right_length(self):
return len(max(getattr(self.right, 'get_length', lambda :[[]])(), key=len))
def left_length(self):
return len(max(getattr(self.left, 'get_length', lambda :[[]])(), key=len))
t = Tree(value = 'A', left=Tree(value='B', left=Tree(value='D'), right=Tree(value='E')), right = Tree(value='C', left = Tree(value='F', left=Tree(value='G'))))
print(t.right_length() - t.left_length())
Output:
1

Related

How to find duplicate subtrees

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)]

Recursive function failing while inorder traverse in tree ds

I am trying to implement tree data structure but I am stuck and having trouble understanding how to create a recursive function for inorder traversal for my binary tree.
This is what i have done so far:
class Node:
def __init__(self, node):
self.node = node
self.left = None
self.right= None
def inorder_traversal(self):
if self.node != None:
return inorder_traversal(self.node.left)
return self.node
return inorder_traversal(self.node.right)
I don't seem to understand what's wrong.
Test inputs:
root = Node(1)
root.left = Node(3)
root.right = Node(4)
Error:
File "trees-implementation.py", line 23, in inorder_traversal
return inorder_traversal(self.node.left)
NameError: name 'inorder_traversal' is not defined
It looks like you've defined your traversal functions with respect to the Node. Does this make sense to you?
You should define a traversal with respect to the Tree, not the Node. The Node does not know it belongs to the tree, it's a dumb object.
Define the node.
class Node:
def __init__(self, value):
self.value = value
self.left = None
self.right= None
Define the tree. Also define your traversal methods here.
class Tree:
def __init__(self, root=None):
self.root = root
def inorder_traversal(self):
def _inorder(root):
if root != None:
yield from _inorder(root.left)
yield root.value
yield from _inorder(root.right)
return list(_inorder(self.root))
Initialise the tree with a root, and then traverse it.
tree = Tree(root)
tree.inorder_traversal()
[3, 1, 4]
First, could you check if when you have your object, you don't put a parameter in
root = Node()
Then are you sure you can have several returns in your inorder_traversal() function ?
Finally, the function is in the class Node() so if you call it try to add self.yourfunction

How to list the longest path in binary tree?

Here we are trying to list the longest path in a binary tree. For instance,
list_longest_path(None)
[]
list_longest_path(BinaryTree(5))
[5]
b1 = BinaryTree(7)
b2 = BinaryTree(3, BinaryTree(2), None)
b3 = BinaryTree(5, b2, b1)
list_longest_path(b3)
[5, 3, 2]
My code is at bottom. Apparently the code returns every node in the tree. Here I am having difficulty on how to generate all lists while using max()at the same time?
class BinaryTree:
"""
A Binary Tree, i.e. arity 2.
=== Attributes ===
#param object data: data for this binary tree node
#param BinaryTree|None left: left child of this binary tree node
#param BinaryTree|None right: right child of this binary tree node
"""
def __init__(self, data, left=None, right=None):
"""
Create BinaryTree self with data and children left and right.
#param BinaryTree self: this binary tree
#param object data: data of this node
#param BinaryTree|None left: left child
#param BinaryTree|None right: right child
#rtype: None
"""
self.data, self.left, self.right = data, left, right
def __eq__(self, other):
"""
Return whether BinaryTree self is equivalent to other.
#param BinaryTree self: this binary tree
#param Any other: object to check equivalence to self
#rtype: bool
>>> BinaryTree(7).__eq__("seven")
False
>>> b1 = BinaryTree(7, BinaryTree(5))
>>> b1.__eq__(BinaryTree(7, BinaryTree(5), None))
True
"""
return (type(self) == type(other) and
self.data == other.data and
(self.left, self.right) == (other.left, other.right))
def __repr__(self):
"""
Represent BinaryTree (self) as a string that can be evaluated to
produce an equivalent BinaryTree.
#param BinaryTree self: this binary tree
#rtype: str
>>> BinaryTree(1, BinaryTree(2), BinaryTree(3))
BinaryTree(1, BinaryTree(2, None, None), BinaryTree(3, None, None))
"""
return "BinaryTree({}, {}, {})".format(repr(self.data),
repr(self.left),
repr(self.right))
def __str__(self, indent=""):
"""
Return a user-friendly string representing BinaryTree (self)
inorder. Indent by indent.
>>> b = BinaryTree(1, BinaryTree(2, BinaryTree(3)), BinaryTree(4))
>>> print(b)
4
1
2
3
<BLANKLINE>
"""
right_tree = (self.right.__str__(
indent + " ") if self.right else "")
left_tree = self.left.__str__(indent + " ") if self.left else ""
return (right_tree + "{}{}\n".format(indent, str(self.data)) +
left_tree)
def __contains__(self, value):
"""
Return whether tree rooted at node contains value.
#param BinaryTree self: binary tree to search for value
#param object value: value to search for
#rtype: bool
>>> BinaryTree(5, BinaryTree(7), BinaryTree(9)).__contains__(7)
True
"""
return (self.data == value or
(self.left and value in self.left) or
(self.right and value in self.right))
def list_longest_path(node):
"""
List the data in a longest path of node.
#param BinaryTree|None node: tree to list longest path of
#rtype: list[object]
>>> list_longest_path(None)
[]
>>> list_longest_path(BinaryTree(5))
[5]
>>> b1 = BinaryTree(7)
>>> b2 = BinaryTree(3, BinaryTree(2), None)
>>> b3 = BinaryTree(5, b2, b1)
>>> list_longest_path(b3)
[5, 3, 2]
"""
if node is None:
return []
elif not node.left and not node.right:
return [node]
else:
return [node]+list_longest_path(node.left)+list_longest_path(node.right)
The longest path in a tree, is called "diameter". So you are looking for something like a "python tree diameter calculator".
You can see the implementation of the algorithm here:
http://www.geeksforgeeks.org/diameter-of-a-binary-tree/
and here:
http://tech-queries.blogspot.com.br/2010/09/diameter-of-tree-in-on.html
Since this website contains only code in C and JAVA, you could take a look here to get some pythonic coding ideas:
Optimize finding diameter of binary tree in Python
Here's a Python function that will return the path:
def list_longest_path(root):
if not root:
return []
l = list_longest_path(root.left)
r = list_longest_path(root.right)
if len(l) > len(r):
return [root] + l
else:
return [root] + r
In your code there's no need to check if left or right child exists since
your function will return list in any case. What you need to do though is
to check the length of the lists returned from children an pick the one
that is longer.

Python: Build Binary Tree with Pre and Inorder

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.

Reconstructing a tree using inorder and preorder traversals in Python

I need to make a function that builds a tree from a preorder and inorder traversal, but I'm not sure where I should put the MakeNode and recursive calls to construct the tree properly.
Assuming inorder and preorder are both a list of ints, is the following algorithm correct to reconstruct the tree using the traversals?
def build(preorder, inorder):
root = preorder[0]
left subtree = inorder[:root-1]
right subtree = inorder[root+1:]
If so - How can one take that and construct a heap (ArrayHeap) using that algorithm recursively?
I have a class designed to construct nodes and then I can simply use heap.add(node) to create the heap.
Say my class to build a node is named "MakeNode" and is constructed as follows (for syntax purposes):
Class MakeNode():
def __init__(self, character, left=None, right=None):
To create the root node I would need to edit the function like this:
def build(preorder, inorder, heap):
root = preorder[0]
node = MakeNode(root) # Creating root node here
heap.add(node) # Adding to heap
left subtree = inorder[:root-1]
right subtree = inorder[root+1:]
But how should I use recursion to build the rest of the tree?
I can incorporate the left and right preorder for ordering purposes by doing this:
def build(preorder, inorder, heap):
root = preorder[0]
node = MakeNode(root)
heap.add(node)
left subtree = inorder[:root-1]
# order of left subtree = preorder[1:1+left subtree]
right subtree = inorder[root+1:]
# order of right subtree = preorder[root+1:]
I don't really know how to incorporate the recursive calls to build the tree or what exactly to put for the left or right parameters when doing so.
If anybody has any suggestions I would appreciate them, and I'm sorry if I've been unclear.
What you need to do is add the root of the pre-ordered list to the tree, and removing it from preorder list. Split the in order list as you are doing, then passing both the left and right branches recursively. Keep adding the first element of pre-order to the left of the previous node unless left_subtree is empty, then you need to add it to the right.
This is python code (already tested):
class Tree():
def __init__(self, inorder, preorder):
self.root = preorder[0]
lt = inorder[:inorder.index(self.root)]
rt = inorder[inorder.index(self.root) + 1:]
self.build(self.root, lt, rt, preorder[1:])
def build(self, last_node, left_subtree, right_subtree, preorder):
left_preorder = [node for node in preorder if node in left_subtree]
right_preorder = [node for node in preorder if node in right_subtree]
if len(left_subtree) > 0:
last_node.left = left_preorder[0]
lt = left_subtree[:left_subtree.index(last_node.left)]
rt = left_subtree[left_subtree.index(last_node.left) + 1:]
self.build(last_node.left, lt, rt, left_preorder)
if len(right_subtree) > 0:
last_node.right = right_preorder[0]
lt = right_subtree[:right_subtree.index(last_node.right)]
rt = right_subtree[right_subtree.index(last_node.right) + 1:]
self.build(last_node.right, lt, rt, right_preorder)
From http://www.cse.hut.fi/en/research/SVG/TRAKLA2/tutorials/heap_tutorial/taulukkona.html, you have:
parent(i) = i/2
left(i) = 2i
right(i) = 2i+1
so you can define a class:
class ArrayHeapNode:
def __init__(self, elements, index):
self.elements = elements
self.index = index
def left(self):
next = self.index * 2
if next >= len(self.elements):
return None
return ArrayHeapNode(self.elements, next)
def right(self):
next = (self.index * 2) + 1
if next >= len(self.elements):
return None
return ArrayHeapNode(self.elements, next)
def value(self):
return self.elements[self.index]
def set_value(self, _value):
self.elements[self.index] = _value
This gives you a class that can then function as a tree on an array representation of a binary heap according to the article. If there is no element in that branch, None is returned.
You can now create tree traversal algorithms (https://en.wikipedia.org/wiki/Tree_traversal):
def inorder_traversal(node, action):
if not node: return
inorder_traversal(node.left(), action)
action(node.value())
inorder_traversal(node.right(), action)
def preorder_traversal(node, action):
if not node: return
action(node.value())
preorder_traversal(node.left(), action)
preorder_traversal(node.right(), action)
These will also work with a traditional binary tree node:
class BinaryTreeNode:
def __init__(self, value, left, right):
self._value = value
self._left = left
self._right = right
def left(self):
return self._left
def right(self):
return self._right
def value(self):
return self._value
def set_value(self, _value):
self._value = _value
Now, you can make the traversal algorithms more flexible and python-like by doing:
def inorder(node):
if node:
for item in inorder(node.left()):
yield item
yield node.value()
for item in inorder(node.right()):
yield item
This allows you to write things like:
for item in inorder(tree):
print item
You can then count the elements from the node by doing:
n = sum(1 for e in inorder(root))
This then allows you to create an empty array capable of holding n elements for the elements in the heap:
elements = [0 for x in range(n)]
or combined:
elements = [0 for x in inorder(root)]
heap = ArrayHeapNode(elements, 0)
Now, you can iterate over both trees at the same time using:
for a, b in zip(inorder(root), inorder(heap)):
b = a
This should then assign all elements in the binary tree (root) to the correct elements in the array heap (heap) using inorder traversal. The same can be done by implementing a preorder function.
NOTE: I have not tested this code.
def pre_order(node):
print node #pre order ... print ourself first
pre_order(node.left)
pre_order(node.right)
def post_order(node):
post_order(node.left)
post_order(node.right)
print node # post order print self last...
def in_order(node):
in_order(node.left)
print node #in order ... between its children
in_order(node.right)
if you have any one of these you should be able to reproduce the tree
assume we have a tree like this
0
1 2
3 4 5 6
our traversals would be
0,1,3,4,2,5,6 #pre order
3,1,4,0,5,2,6 #in order
so from this we know that
zero is our root
3 is our left most deepest node
6 is our right most deepest node
0
...
3 6
our left over nodes are
1,4,2,5 # preorder
1,4,5,2 # in-order
from this we know that
1 is a child of zero
1 is the next level leftmost node
2 is the next level rightmost node
so we now have
0
1 2
3 6
leaving us with
4,5 # pre order
4,5 # in order
from this we know that 4 is a child of 1, and therefore 5 must be a child of 2 ...
now write a function that does all that
this article may help
http://leetcode.com/2011/04/construct-binary-tree-from-inorder-and-preorder-postorder-traversal.html

Categories

Resources