Esempio n. 1
0
def main():
  tree = Node("a")
  tree.left = Node("b")
  tree.right = Node("c")
  assert is_symmetric(tree)
  tree.left.left = Node("x")
  assert not is_symmetric(tree)
Esempio n. 2
0
def main():
  tree1 = c_balanced(4, "x")
  tree2 = Node("x")
  tree2.left = Node("x")
  tree2.right = Node("x")
  tree2.right.right = Node("x")

  assert tree1 == tree2
Esempio n. 3
0
def c_balanced(c, value):
  if c == 0:
    return None
  root = Node(value)
  left_size = (c - 1) // 2
  right_size = (c - 1) - left_size
  root.left = c_balanced(left_size, value)
  root.right = c_balanced(right_size, value)

  return root
Esempio n. 4
0
def solution():
    root = Node(30)
    root.left = Node(20)
    root.left.left = Node(10)
    assert is_balanced(root) == -1
    root.right = Node(40)
    root.left.right = Node(25)
    root.left.left.left = Node(0)
    assert is_balanced(root) == -1
    root.right.left = Node(35)
    root.right.right = Node(45)
    assert is_balanced(root) == 4
def solution():
    root = Node(30)
    root.left = Node(20)
    root.left.left = Node(10)
    assert is_balanced(root) == -1
    root.right = Node(40)
    root.left.right = Node(25)
    root.left.left.left = Node(0)
    assert is_balanced(root) == -1
    root.right.left = Node(35)
    root.right.right = Node(45)
    assert is_balanced(root) == 4
Esempio n. 6
0
def main():
  tree1 = add_value(2)
  tree2 = Node(2)
  assert tree1 == tree2

  add_value(3, tree1)
  tree2.right = Node(3)
  assert tree1 == tree2

  add_value(0, tree1)
  tree2.left = Node(0)
  assert tree1 == tree2

  tree1 = from_list([3, 2, 5, 7, 1])
  tree2 = Node(3)
  tree2.left = Node(2)
  tree2.right = Node(5)
  tree2.right.right = Node(7)
  tree2.left.left = Node(1)
  assert tree1 == tree2

  assert is_symmetric(from_list([5, 3, 18, 1, 4, 12, 21]))
  assert not is_symmetric(from_list([3, 2, 5, 7, 4]))
def solution2():
    root = Node(30)
    root.left = Node(20)
    root.left.left = Node(50)
    assert is_bst2(root, [-sys.maxint - 1]) == False
    root.left.left = Node(10)
    assert is_bst2(root, [-sys.maxint - 1]) == True
    root.right = Node(40)
    root.right.right = Node(0)
    root.left.right = Node(25)
    root.left.left.left = Node(0)
    assert is_bst2(root, [-sys.maxint - 1]) == False
    root.right.right = Node(45)
    root.right.left = Node(35)
    assert is_bst2(root, [-sys.maxint - 1]) == True
Esempio n. 8
0
def solution2():
    root = Node(30)
    root.left = Node(20)
    root.left.left = Node(50)
    assert is_bst2(root, [-sys.maxint-1]) == False
    root.left.left = Node(10)
    assert is_bst2(root, [-sys.maxint-1]) == True
    root.right = Node(40)
    root.right.right = Node(0)
    root.left.right = Node(25)
    root.left.left.left = Node(0)
    assert is_bst2(root, [-sys.maxint-1]) == False
    root.right.right = Node(45)
    root.right.left = Node(35)
    assert is_bst2(root, [-sys.maxint-1]) == True
"""
from collections import defaultdict, deque
from bintree import Node


def vertical_order(root):
    queue = deque()
    queue.appendleft((root, 0))
    distances = defaultdict(list)
    while queue:
        node, hd = queue.pop()
        distances[hd].append(node.key)
        if node.left:
            queue.appendleft((node.left, hd-1))
        if node.right:
            queue.appendleft((node.right, hd+1))
    for hd in sorted(distances):
        print ", ".join([str(k) for k in distances[hd]])


if __name__ == "__main__":
    root = Node(12)
    root.left = Node(10)
    root.right = Node(20)
    root.right.left = Node(25)
    root.right.right = Node(40)
    root.right.left.right = Node(3)

    print "Vertical ordering"
    vertical_order(root)