算法导论 python代码 第十二章

# author Ttssxuan
# chapter 12
# the binary search tree

class 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 = True

def 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 x

def 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 x

def 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 x

def 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 None

def 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.p

def 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 = z


def 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.p

def 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值