Advertisement
STANAANDREY

lsd add node to bin src tree

Nov 18th, 2022
941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. t = {'value': 4, 'left': {'value': 2, 'left': {'value': 1, 'left': None, 'right': None}, 'right': {'value': 3, 'left': None, 'right': None}}, 'right': {'value': 7, 'left': {'value': 5, 'left': None, 'right': None}, 'right': None}}
  2.  
  3.  
  4. def add_node_to_bin_src_tree(tree, new_node):
  5.     if tree is None:
  6.         return new_node
  7.     if tree['value'] == new_node['value']:
  8.         return tree
  9.  
  10.     if new_node['value'] < tree['value']:
  11.         tree['left'] = add_node_to_bin_src_tree(tree['left'], new_node)
  12.     else:
  13.         tree['right'] = add_node_to_bin_src_tree(tree['right'], new_node)
  14.  
  15.     return tree
  16.  
  17.  
  18. print(add_node_to_bin_src_tree(t, {'value': 5, 'left': None, 'right': None}))
  19.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement