out = tree.Node(root1.value) out.left_child = mergetree(root1.left_child, None) out.right_child = mergetree(root1.right_child, None) if root2 and root1 == None: out = tree.Node(root2.value) out.left_child = mergetree(None, root2.left_child) out.right_child = mergetree(None, root2.right_child) if root1 and root2: out = tree.Node(root1.value + root2.value) out.left_child = mergetree(root1.left_child, root2.left_child) out.right_child = mergetree(root1.right_child, root2.right_child) return out bst1 = tree.BST() bst2 = tree.BST() bst1.insert(4) bst1.insert(2) bst1.insert(1) bst1.insert(5) bst2.insert(3) bst2.insert(1) bst2.insert(2) bst2.insert(6) bst2.insert(7) mergetree(bst1.root, bst2.root)
temp = root while temp != None: s.append(temp) temp = temp.left_child return s def iwot(root): s = [] final = [] current = root while current != None or (len(s) > 0): s = leftnext(current, s) current = s.pop() final.append(current.value) current = current.right_child return final bst = tree.BST() bst.insert(5) bst.insert(3) bst.insert(2) bst.insert(2.5) bst.insert(4) bst.insert(8) bst.insert(6) iwot(bst.root)
def LRM(root): if root == None: return out = [root.value] temp = root while temp.left_child: out.append(temp.left_child.value) temp = temp.left_child temp = root while temp.right_child: out.append(temp.right_child.value) temp = temp.right_child return out bst = tree.BST() bst.insert(5) bst.insert(3) bst.insert(2) bst.insert(2.5) bst.insert(4) bst.insert(8) bst.insert(6) LRM(bst.root) bst2 = tree.BST() bst2.insert(15) bst2.insert(10) bst2.insert(8) bst2.insert(12) bst2.insert(20)