天天看點

6666666

随機生成20個100内的數,直接插入排序。

class InsertSort(object):
    def __init__(self,items):
        self.items = items
    def insertSort(self):
        for i in range(1, len(self.items)):
            temp = self.items[i]
            j = i - 1
            while j >= 0 and temp < self.items[j]:
                self.items[j+1] = self.items[j]
                j -= 1
            self.items[j+1] = temp

if __name__=='__main__':
    import random
    arr = []
    for j in range(0,20):
        i = random.randint(0, 100)
        arr.append(i)
    select = InsertSort(arr)
    select.insertSort()
    print(arr)
           

轉換進制

class Solution:
    def translation(self, number):
        stack = []
        s = ''
        while number:
            stack.append(number % 8)
            number //= 8
        while stack:
            s += str(stack.pop())
        return s


if __name__ == '__main__':
    t = Solution()
print("請輸入要轉換的數字:")
    num = int(input())
    print(t.translation(num))
           

循環連結清單尾插入法

class Node(object):
    def __init__(self,val):
        self.val = val
        self.next = None
class CircleLinkedList(object):
    def __init__(self):
        self.head = None
        self.tail = None

    def empty(self):
        return self.head is None

    def length(self):
        size = 0
        if self.empty():
            return size
        cur = self.head
        size += 1
        while cur.next is not self.head:
            size += 1
            cur = cur.next
        return size

    def append(self,val):
        newNode = Node(val)
        if self.empty():
            self.head = newNode
            self.tail = newNode
        else:
            self.tail.next = newNode
            self.tail = newNode
            self.tail.next = self.head
    def traversal(self):
        if self.empty():
            raise IndexError("連結清單為空")
        cur = self.head
        while cur.next is not self.head:
            print(cur.val,end = " ")
            cur = cur.next
        print(cur.val)

if __name__ == '__main__':
    cl = CircleLinkedList()
    print(":", end=" ")
    for i in range(10):
        key = int(input())
        cl.append(key)
    print("檢視:",end=" ")
    cl.traversal()
           

證明:在非空二叉樹中,第i層上最多有2的i-1個節點

用數學歸納法證明,第一層2的1-1次=1.第二層2的2-1次=2.第三層2的3-1次=4.由上面的規律可以發現,這是一個首項為1、公比為2的等比數列,可以得到第n層的節點數最多為2的n-1次。

證明:n個節點的完全二叉樹的高度為【log2n】+1

由完全二叉樹定義可得,高度為k的完全二叉樹的前k-1層是高度為k-1的滿二叉樹,一共有2的k-1次-1個節點。由于完全二叉樹高度為K,故第k層上還有若幹個節點,是以,由該完全二叉樹n>2的k-1次-1可得2的k-1次-1<n<2的k次-1;由于n為整數,可以推出2的k-1次<=log2n<k,而k作為整數,是以k=【log2n】+1