算法导论 python代码 第十二章

来源:互联网 发布:人工智能编程语言 编辑:程序博客网 时间:2024/06/04 18:09
# author Ttssxuan# chapter 12# the binary search treeclass Node:    '''    the node of the binary tree    Properties:        p - the parent of the current node        left - the left child of the current node        right - the right child of the current node        value - the value of this node    '''    def __init__(self):        self.p = None        self.left = None        self.right = None        self.value =  None    def __init__(self, other):        self.p = other.p        self.left = other.left        self.right = other.right        self.value = other.value    def __str__(self):        return str(self.value)def inorder_tree_walk(x):    '''    scan all keys by inorder tree walk method    Parameters:        x - the root of the binary search tree    Returns:        none    '''    if x != None:        inorder_tree_walk(x.left)        print(x + " ")        inorder_tree_walk(x.right)def interative_inorder_tree_wal(x):    '''    12.1.3    scan all keys by inorder tree walk method with nonrecursive    Parameters:        x - the root of the binary search tree    Returns:        none    '''    done = False    cur = Node(x)    stack = []    while(done):        if cur != None:            stack.append(cur)            cur = cur.left        else:            if len(stack) > 0:                cur = stack.pop()                print(cur + " ")                cur = cur.right            else:                done = Truedef preorder_tree_walk(x):    '''    12.1.4    scan all keys by preorder tree walk method    Parameters:        x - the root of the binary search tree    Returns:        none    '''    if x != None:        print(x + " ")        preorder_tree_walk(x.left)        preorder_tree_walk(x.right)def postorder_tree_walk(x):    '''    12.1.4    scan all keys by postorder tree walk method    Parameters:        x - the root of the binary search tree    Returns:        none    '''    if x != None:        postorder_tree_walk(x)        postorder_tree_walk(x)        print(x + " ")def tree_search(x, k):    '''    12.2    search the given key of the binary search tree    Parameters:        x - the root of the binary search tree        k - the key need to search    Returns:        the node that contains the k    '''    if x == None or k == x.value:        return x    if k < x.value:        return tree_search(x.left, k)    else:        return tree_search(x.right, k)def interative_tree_search(x, k):    '''    12.2    the unrolling version of searching the binary search tree    Parameters:        x - the root of the binary search tree        k - the key need to search    Returns:        the node that contains the k    '''    while x != None and k != x.value:        if k < x.value:            x = x.left        else:            x = x.right    return xdef tree_minimum(x):    '''    12.2    find the minimum key of the binary search tree    Parameters:        x - the root of the binary search tree    Returns:        the node that contains the minimum key     '''    if x == None:        return None    while x.left != None:        x = x.left    return xdef tree_maximum(x):    '''    12.2    find the maximum key of the binary search tree    Parameters:        x - the root of the binary search tree    Returns:        the node that contains the maximum key    '''    if x == None:        return None    while x.right != None:        x = x.right    return xdef tree_successor(x):    '''    12.2    find the successor of the given node    Parameters:       x - the node need to find the successor    Returns:        the node which contains the successor of x    '''    if x.right != None:        return tree_minimum(x.right)    y = x.p    while y != None and x == y.right:        x = y        y = y.p    return Nonedef tree_predecessor(x):    '''    12.2.3    find the predecessor of the given node    Parameters:        x - the node need to find the predecessor    Returns:        the node whick contains the predecessor of x    '''    if x.left != None:        return tree_maximum(x.left)    return x.pdef tree_insert(t, x):    '''    12.3    insert the given node into the binary search tree    Parameters:        t - the given binary search tree        x - the node needs to insert into the binary search tree    Returns:        none    '''    y = None    x = t.root    while x != None:        y = x        if z.value < x.value:            x = x.left        else:            x = x.right    z.p = y    if y == None:        t.root = z    elif z.key < y.key:        y.left = z    else:        y.right = zdef transplant(t, u, v):    '''    move subtrees around within the binary search tree    Parameters:        t - the given binary tree        u - the subtree was to be replaced        v - the subtree used to replacing the subtree u    Return:        None    '''    if u.p == None:        t.root = v    elif u == u.p.left:        u.p.left = v    else:        u.p.right = v    if v != None:        v.p = u.pdef tree_delete(t, z):    '''    delete the given node z which belongs to the tree t    '''    if z.left == None:        transplant(t, z, z.right)    elif z.right == None:        transplant(t, z, z.left)    else:        y = tree_minimum(z.right)        if y.p != z:            transplant(t, y, y.right)            y.right = z.right            y.right.p = y        transplant(t, z, y)        y.left = z.left        y.left.p = y

0 0
原创粉丝点击