Beispiel #1
0
def buidTree(ino, pro):
  t = BinaryTree()
  index = Index()
  index.value = 0
  start = 0
  end = len(ino) - 1
  root = _buildTree(ino, pro, start, end, index)
  t.root = root
  return t
Beispiel #2
0
def buildTreeInPo(ino, poo):
  index = Index()
  length = len(ino)
  start = 0
  end = length - 1
  index.value = end
  root = _buildTreeInPo(ino, poo, start, end, index)
  t = BinaryTree()
  t.root = root
  return t
Beispiel #3
0
'''Invert a binary tree.'''
import __init__
from binarytree import BinaryTree
from treenode import TreeNode
from util.btutil import BinaryTreeUtil
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Invert:
    def invertTree(self, root: TreeNode) -> TreeNode:
        if root:
            right = root.right
            left = root.left
            root.right = self.invertTree(left)
            root.left = self.invertTree(right)
        return root

if __name__ == '__main__':
    print("Create a binary tree")
    bt = BinaryTree()
    btutil = BinaryTreeUtil()
    btutil.constructBinaryTree(bt)
    bt.printTree(traversal=BinaryTree.LevelOrder, verbose=True)
    bt.printTree(traversal=BinaryTree.InOrder)
    print("Height of the tree: ", bt.getHeight())
    bt.printTree(traversal=BinaryTree.LevelOrder, verbose=True)
    bt.root = Invert().invertTree(bt.root)
    bt.printTree(traversal=BinaryTree.InOrder)
Beispiel #4
0
def identical(root):
    clone = BinaryTree()
    clone.root = _identical(root)
    return clone
Beispiel #5
0
def mirror(root):
    mirror = BinaryTree()
    mirror.root = _mirror(root)
    return mirror