コード例 #1
0
from __future__ import print_function
from tree import Node
from insertNode import insert


def iterativePreorder(root):

    stack = [root]

    while stack:
        node = stack.pop()

        print(node.data, end=", ")

        if node.right:
            stack.append(node.right)
        if node.left:
            stack.append(node.left)


if __name__ == "__main__":
    nums = [3, 2, 4, 5, 10, 12, 7, 8, 5, 1, 9, 6, 11, 13, 14]

    root = stringToTreeNode(nums)
    for i in xrange(len(nums)):
        root = insert(root, nums[i])

    root.prettyPrint()
    iterativePreorder(root)
コード例 #2
0
    while queue:
        size = len(queue)
        for i in xrange(size):
            node = queue.pop(0)
            if node.data == val:
                key_node = node

            if node.left:
                queue.append(node.left)

            if node.right:
                queue.append(node.right)

    last_val = node.data

    deleteLast(root, last_val)
    key_node.data = last_val
    return root


if __name__ == "__main__":
    root = None
    for i in xrange(31):
        root = insert(root, i)

    root.prettyPrint()

    root = deleteNode(root, 5)
    root.prettyPrint()