# print(repeated_word("It was a queer, sultry summer, the summer they electrocuted the Rosenbergs, and I didn’t know what I was doing in New York..."))

    # CC32
    node1 = Node(150)
    node2 = Node(100)
    node3 = Node(250)
    node4 = Node(75)
    node5 = Node(160)
    node6 = Node(200)
    node7 = Node(350)
    node8 = Node(125)
    node9 = Node(175)
    node10 = Node(300)
    node11 = Node(500)
    binary_tree1 = BinaryTree()
    binary_tree1.root = node1
    node1.left = node2
    node1.right = node3
    node2.left = node4
    node2.right = node5
    node3.left = node6
    node3.right = node7
    node5.left = node8
    node5.right = node9
    node7.left = node10
    node7.right = node11

    node_one = Node(42)
    node_two = Node(100)
    node_three = Node(600)
    node_four = Node(15)
Esempio n. 2
0
def make_bst(vals):
    """ `vals` is a list of sorted numbers """
    bst = BinaryTree()
    bst.root = make_balanced_bst(n=None, vals=vals)
    return bst
Esempio n. 3
0
            return inorder_successor(node.parent, True)
    else:
        return get_leftmost(node.rchild)


def get_leftmost(node):
    """ Returns leftmost node in subtree rooted at node. """
    if node.lchild is None:
        return node
    else:
        return get_leftmost(node.lchild)


if __name__ == "__main__":
    bt = BinaryTree()
    bt.root = BinaryNode(item=1)
    bt.root.lchild = BinaryNode(item=2, parent=bt.root)
    bt.root.rchild = BinaryNode(item=3, parent=bt.root)

    bt.root.lchild.lchild = BinaryNode(item=4, parent=bt.root.lchild)
    bt.root.lchild.rchild = BinaryNode(item=5, parent=bt.root.lchild)

    bt.root.rchild.lchild = BinaryNode(item=6, parent=bt.root.rchild)
    bt.root.rchild.rchild = BinaryNode(item=7, parent=bt.root.rchild)

    # For now assume bt.root itself is not None.
    print("\nHere's the tree, and inorder/preorder/postorder traversals ...")
    bt.pretty_print()
    inorder(bt.root)
    print(" (Inorder)")
    preorder(bt.root)

def is_bst_node(n, min, max):
    if n is None:
        return True
    if not (min <= n.item <= max):
        return False
    return is_bst_node(n.lchild, min, n.item) and \
           is_bst_node(n.rchild, n.item, max)


if __name__ == "__main__":
    choices = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
    for k in choices:
        vals = [x for x in range(k)]
        if len(vals) == 0: continue
        print("\nvals = {}\n".format(vals))
        bt = make_bst(vals)
        bt.pretty_print()
        print("IS IT A BINARY SEARCH TREE? {}".format(is_bst(bt)))

    print("\nNow some false cases.")
    bt = BinaryTree()
    bt.root = BinaryNode(3)
    bt.root.lchild = BinaryNode(2)
    bt.root.lchild.lchild = BinaryNode(-20)
    bt.root.lchild.rchild = BinaryNode(20)
    bt.root.rchild = BinaryNode(10)
    bt.pretty_print()
    print("IS IT A BINARY SEARCH TREE? {}".format(is_bst(bt)))