Printing out python tree from nested list and flat list - python

I want to define two functions, one that prints a tree out from a nested list and the other that prints a tree from a flat list.
For exaple, if we have the lists
[5, [10, None, None], [11, [2, None, None],[6, None, None]]],
and [None, 5, 10, 11, None, None, 2, 6], we would produce a tree that looks like
10
5
15
11
22
This is the Binary Tree implementation
class BinaryTree:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def get_left(self):
return self.left
def get_right(self):
return self.right
def set_left(self, tree):
self.left = tree
def set_right(self, tree):
self.right = tree
def set_data(self, data):
self.data = data
def get_data(self):
return self.data
def create_string(self, spaces):
info = ' ' * spaces + str(self.data)
if self.left != None:
info += self.left.create_string(spaces+4)
if not self.right == None:
info += self.right.create_string(spaces+4)
return info
def __str__(self):
representation = self.create_string(0)
return representation
I basically want to convert the list into a tree because there is a create string function in the Binary Tree class

A simple recursive function would allow you to create the nested lists into a btree:
def create_tree(data):
if not data:
return data
d, l, r = data
btree = BinaryTree(d)
btree.set_left(create_tree(l))
btree.set_right(create_tree(r))
return btree
>>> print(create_tree([10, [5, None, None], [15, [11, None, None],[22, None, None]]]))
10
(l) 5
(r) 15
(l) 11
(r) 22
Though personally I would update the __init__() method of the BinaryTree class to optionally take the left and right:
class BinaryTree(object):
def __init__(self, data, left=None, right=None):
self.data = data
self.left = left
self.right = right
Then the recursive function becomes:
def create_tree(data):
if not data:
return data
d, l, r = data
return BinaryTree(d, create_tree(l), create_tree(r))
It's unclear what the mapping from the flat list is to the tree structure.

Related

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

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

Zigzag level order traversal

I am trying to do the zigzag level order traversal of a binary tree's nodes values (ie, from left to right, then right to left for the next level and alternate between) on https://www.interviewbit.com/problems/zigzag-level-order-traversal-bt/ But the compiler gives time limit exceeded error. How can I resolve it?
# Definition for a binary tree node
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
# #param A : root node of tree
# #return a list of list of integers
def zigzagLevelOrder(self, A):
st_crt_lvl =[A]
st_nxt_lvl =[]
ans_list = []
while st_crt_lvl:
u = st_crt_lvl.pop(0)
ans_list.append(u.val)
if u.left:
st_nxt_lvl.append(u.left)
if u.right:
st_nxt_lvl.append(u.right)
while st_nxt_lvl:
u = st_nxt_lvl.pop()
ans_list.append(u.val)
if u.right:
st_crt_lvl.append(u.right)
if u.left:
st_crt_lvl.append(u.left)
return ans_list
You can eliminate multiple inner while loops from your code, by making the queue st_nxt_lvl temporary and copying the content of this temporary queue to the current one st_crt_lvl at the end processing of each level of the binary tree.
This could be achieved by keeping just a single queue (without any temporary storage) and level order traversal by standard bfs algorithm, but since we want zig-zag order traversal at each level, it's more elegant to have a temporary queue, so that the temporary queue only keeps the next level elements and when processing of the current level elements is done, the current queue points to the next level.
With some modification of your code, along with an example tree:
def zigzagLevelOrder(A):
st_crt_lvl = [A] # initialize
ans_list = []
level = 0
while st_crt_lvl: # check if all levels are processed
st_nxt_lvl =[] # temporary queue to hold the next level elements
tmp = [] # temporary storage to append to the ans_list
while st_crt_lvl:
u = st_crt_lvl.pop(0)
tmp.append(u.val)
if u.left:
st_nxt_lvl.append(u.left)
if u.right:
st_nxt_lvl.append(u.right)
if (len(tmp) > 0): # if tmp is not empty
if level % 2 == 1: # ensure zig-zag level order traversal
tmp = tmp[::-1]
ans_list.append(tmp)
st_crt_lvl = st_nxt_lvl # copy the temporary queue to the current queue
level += 1
return ans_list
class BinaryTree:
def __init__(self, left, right, data):
self.left = left
self.right = right
self.val = data
A = BinaryTree(None, None, 3)
A.left = BinaryTree(None, None, 9)
A.right = BinaryTree(None, None, 20)
A.left.left = BinaryTree(None, None, 1)
A.left.right = BinaryTree(None, None, 2)
A.right.left = BinaryTree(None, None, 15)
A.right.right = BinaryTree(None, None, 7)
zigzagLevelOrder(A)
# [[3], [20, 9], [1, 2, 15, 7]]
You can use a breadth-first search:
from collections import deque, defaultdict
class Tree:
def __init__(self, **kwargs):
self.__dict__ = {i:kwargs.get(i) for i in ['left', 'right', 'value']}
def __contains__(self, _val):
if self.value != _val and self.left is None and self.right is None:
return False
return True if self.value == _val else any([_val in [[], self.left][self.left is not None], _val in [[], self.right][self.right is not None]])
def __lookup(self, _val, _depth = 0):
if self.value == _val:
return _depth
return getattr(self, 'left' if _val in self.left else 'right').__lookup(_val, _depth+1)
def __getitem__(self, _val):
return self.__lookup(_val)
def bfs(_head):
_d = deque([_head])
_seen = []
_last = None
_final_result = defaultdict(list)
while _d:
_current = _d.popleft()
if _current.value not in _seen:
_seen.append(_current.value)
_r = list(filter(None, [getattr(_current, i, None) for i in ['left', 'right']]))
_d.extend(_r)
_final_result[_head[_current.value]].append(_current.value)
marker = iter(range(len(_final_result)))
return [i[::-1] if next(marker)%2 else i for _, i in sorted(_final_result.items(), key=lambda x:x[0])]
When constructing the same tree in the example from the link in the question, the output is:
t = Tree(value=3, left=Tree(value=9), right=Tree(value=20, left=Tree(value=15), right=Tree(value=7)))
print(bfs(t))
Output:
[[3], [20, 9], [15, 7]]
This solution uses the breadth-first search to traverse the tree horizontally. However, since a (complete) binary tree has two children, only two values will be added to the queue. Therefore, in order to determine the current tree level the search has reached, a method to find the depth of a particular value has to be implemented in the tree (__getitem__ and __lookup).

Level Order Traversal - Tree

I need to define a function called level_order_travel which takes a tree as an output, a, and prints a list of all the nodes in the list in level order.
The following code here shows this:
def create_tree(node_list, index=1):
if index >= len(node_list) or node_list[index] is None:
return None
d = node_list[index]
l = index * 2
r = l + 1
tree = BinaryTree(d)
tree.set_left(create_tree(node_list, l))
tree.set_right(create_tree(node_list, r))
return tree
def level_order_travel(a):
###
def test():
list_of_leaves = [None, 10, 5, 15, None, None, 11, 22]
my_tree = create_tree(list_of_leaves )
print("Breadth first =", level_order_travel(my_tree))
test()
This is my BinaryTree class:
class BinaryTree:
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def get_left(self):
return self.left
def get_right(self):
return self.right
def set_left(self, tree):
self.left = tree
def set_right(self, tree):
self.right = tree
def set_data(self, data):
self.data = data
def get_data(self):
return self.data
def create_string(self, spaces):
info = ' ' * spaces + str(self.data)
if self.left != None:
info += '\n(l)' + self.left.create_string(spaces+4)
if not self.right == None:
info += '\n(r)' + self.right.create_string(spaces+4)
return info
def __str__(self):
representation = self.create_string(0)
return representation
This is my Queue class:
class Queue:
def __init__(self):
self.items = []
def is_empty(self):
return self.items == []
def enqueue(self, item):
self.items.insert(0,item)
def dequeue(self):
return self.items.pop()
def size(self):
return len(self.items)
def peek(self):
return self.items[self.size() - 1]
This is my attempt so far:
def level_order_travel(a):
root = a.get_data()
q = Queue()
q.enqueue(root)
list_of_leaves = []
if root is None:
return []
else:
if a.get_left() is not None:
q.enqueue(a.get_left().get_data())
if a.get_right() is not None:
q.enqueue(a.get_right().get_data())
while q.is_empty() == False:
list_of_leaves.append(q.dequeue())
return list_of_leaves
This should produce the following output:
[10, 5, 15, 11, 22]
but instead it produces the following output:
[10, 5, 15]
Any help is appreciated. Thank you.
Modify your bfs traversal function to keep track of the visited nodes, it should work for any graph (not only the acyclic ones as trees):
def breadth_first_traversal(a):
if a is None:
return []
visited = set([])
q = Queue()
q.enqueue(a)
list_of_leaves = []
while not q.is_empty():
a = q.dequeue()
visited.add(a)
child = a.get_left()
if child is not None and not child in visited:
q.enqueue(child)
child = a.get_right()
if child is not None and not child in visited:
q.enqueue(child)
list_of_leaves.append(a.get_data())
return list_of_leaves
test()
# ('Breadth first =', [10, 5, 15, 11, 22])
Also, if you want to use the implementation only for trees then you can further simplify (you don't need to keep track of the visited nodes, since each node is guaranteed to be visited only once, for each node has only one parent):
def breadth_first_traversal(a): # only for trees
if a is None:
return []
q = Queue()
q.enqueue(a)
list_of_leaves = []
while not q.is_empty():
a = q.dequeue()
child = a.get_left()
if child is not None:
q.enqueue(child)
child = a.get_right()
if child is not None:
q.enqueue(child)
list_of_leaves.append(a.get_data())
return list_of_leaves

Can you define multiple different iterators for a Python class?

I'm writing a very simple Tree class:
class Tree:
def __init__(self, value_ = None, children_ = None):
self.value = value_
self.children = children_
I'd like to be able to perform both DFS and BFS traversal with a simple loop, i.e.:
t = Tree()
# ...fill tree...
for node in t:
print(node.value)
In C++, for example, you can have multiple types of iterators - so I could define both a DFS and a BFS iterator and use one or the other depending on what type of traversal I wanted to do. Is this possible to do in Python?
You can have multiple methods returning iterators and have the 'default' one as __iter__. Below is a simple binary tree where 'default' iterator does DFS and which additionally supports BFS with separate method:
from collections import deque
class Tree(object):
def __init__(self, value):
self.value = value
self.left = None
self.right = None
def __iter__(self):
if self.left:
for x in self.left:
yield x
yield self.value
if self.right:
for x in self.right:
yield x
def bfs(self):
q = deque([self])
while q:
x = q.popleft()
if x:
yield x.value
q.extend([x.left, x.right])
Short example of usage:
root = Tree(2)
root.left = Tree(1)
root.right = Tree(4)
root.right.left = Tree(3)
root.right.right = Tree(5)
print list(root) # [1, 2, 3, 4, 5]
print list(root.bfs()) # [2, 1, 4, 3, 5]
You could write separate methods on your class for the two types of iteration. These could for instance be generators that yield values in whatever order you want. You would then write something like:
for node in t.depth_first():
# ...
for node in t.breadth_first():
# ...

Categories