while root.rchild is not None:
        root = root.rchild

    return root


def GetNode(root: BiTNode):
    maxNode = GetMax(root)
    minNode = GetMin(root)

    # 找到中间值
    mid = (maxNode.data + minNode.data) / 2

    result = None
    while root is not None:
        if root.data <= mid:
            root = root.rchild
        else:
            result = root
            root = root.lchild

    return result


if __name__ == '__main__':
    root = Arr2Tree(RandArr(10), 0, 9)

    Node = GetNode(root)
    print(Node.data)
Exemple #2
0
    while maxEle != 0:
        maxEle //= 10
        maxDigit += 1

    mod = 10
    div = 1
    bucket = [[] for _ in range(10)]

    for i in range(maxDigit):
        for j in range(len(arr)):
            num = arr[j] % mod // div
            bucket[num].append(arr[j])

        # 回填
        arr.clear()
        for j in range(10):
            arr += bucket[j]
            bucket[j].clear()

        i += 1
        mod *= 10
        div *= 10

    return arr


if __name__ == '__main__':
    arr = RandArr(10, low=0, high=1000)

    print(radixSort(arr))
Exemple #3
0
from Array import RandArr


def IsEqual(root1, root2: BiTNode):
    if (root1 and root2) is None:
        return True

    if root1 is None and root2 is not None:
        return False

    if root1 is not None and root2 is None:
        return False

    if root1.data == root2.data:
        return IsEqual(root1.lchild, root2.lchild) and \
               IsEqual(root1.rchild, root2.rchild)
    else:
        return False


if __name__ == '__main__':
    length = 10
    arr = RandArr(length)

    root1 = Arr2Tree(arr, 0, length - 1)
    root2 = Arr2Tree(arr, 0, length - 1)
    root3 = Arr2Tree(RandArr(length), 0, length - 1)

    print(IsEqual(root1, root2))
    print(IsEqual(root1, root3))
Exemple #4
0
    i = j = 0
    result = []
    while i < len(a) and j < len(b):
        if a[i] > b[j]:
            result.append(b[j])
            j += 1
        else:
            result.append(a[i])
            i += 1

    result += a[i:]
    result += b[j:]
    return result


def mergeSort(arr: list):
    length = len(arr)
    if length <= 1:
        return arr

    mid = length // 2
    left = mergeSort(arr[:mid])
    right = mergeSort(arr[mid:])
    return merge(left, right)


if __name__ == '__main__':
    arr = RandArr(10)

    print(mergeSort(arr))
    # 转换root 的左子树
    InOrderBSTree(root.lchild)

    root.lchild = link.pEnd
    if link.pEnd is None:
        link.pHead = root
    else:
        link.pEnd.rchild = root

    link.pEnd = root
    InOrderBSTree(root.rchild)


if __name__ == '__main__':
    length = 10
    arr = RandArr(length)

    root = Arr2Tree(arr, 0, length - 1)

    InOrderBSTree(root)

    cur = link.pHead
    while cur is not None:
        print(cur.data, end=" ")
        cur = cur.rchild

    print()

    cur = link.pEnd
    while cur is not None:
        print(cur.data, end=" ")
Exemple #6
0
        arr[high] = arr[low]

    arr[low] = key
    return low


def GetMid(arr: list):
    low = 0
    length = len(arr)
    high = length - 1
    mid = (low + high) // 2

    while True:
        pivot = Partition(arr, low, high)
        # 找到
        if pivot == mid:
            break
        # 右半部分
        elif pivot > mid:
            high = pivot - 1
        # 左半部分
        else:
            low = pivot + 1

    # 奇偶, 中位数
    return arr[mid] if length % 2 != 0 else (arr[mid] + arr[mid + 1]) / 2


if __name__ == '__main__':
    arr = RandArr(9)
    print(GetMid(arr))