python3 二叉树及单链表

来源:互联网 发布:在重庆网络教育有哪些 编辑:程序博客网 时间:2024/06/04 19:52

看了http://blog.csdn.net/qq_14959801/article/details/52988861及http://blog.csdn.net/hongwing/article/details/52433933的博客,写的挺好的,就自己记录了

单链表:

class Node():    def __init__(self, value, next=None):        self.value = value        self.next = nextdef creatlist(n):    if n <= 0:        return False    elif n == 1:        return Node(1)    else:        root = Node(1)        tmp = root        for i in range(2, n+1):            tmp.next = Node(i)            tmp = tmp.next    return rootdef printlist(head):    p = head    while p != None:        print(p.value)        p = p.nextdef listlen(head):    c = 0    p = head    while p != None:        c = c+1        p = p.next    return cdef insertlist(head, n):    if n < 1 or n > listlen(head):        return 0    p = head    for i in range(1, n-1):        p = p.next    a = input('Enter a value:')    t = Node(value=a)    t.next = p.next    p.next = t    return headdef dellist(head, n):    if n < 1 or n > listlen(head):        return head    elif n == 1:        head = head.next    else:        p = head        for i in range(1, n-1):            p = p.next        q = p.next        p.next = q.next    return headdef main():    print('Create a linklist:')    head = creatlist(7)    printlist(head)    print('----------------------')    n1 = int(input('Enter the index to insert'))    insertlist(head, n1)    printlist(head)    print('---------------')    n2 = int(input('Enter the index to delete'))    dellist(head, n2)    printlist(head)if __name__ == "__main__":    main()
二叉树:

class BTNode(object):    """docstring for BTNode"""    def __init__(self, data):        self.data = data        self.leftChild = None        self.rightChild = None# insert elementdef InsertElementBinaryTree(root, node):    if root:        if node.data < root.data:            if root.leftChild:                InsertElementBinaryTree(root.leftChild, node)            else:                root.leftChild = node        else:            if root.rightChild:                InsertElementBinaryTree(root.rightChild, node)            else:                root.rightChild = node    else:        return 0# init a treedef InitBinaryTree(dataSource, length):    root = BTNode(dataSource[0])    for x in range(1,length):        node = BTNode(dataSource[x])        InsertElementBinaryTree(root, node)    return root    print ('Done...')# pre-orderdef PreorderTraversalBinaryTree(root):    if root:        print ('%d | ' % root.data,)        PreorderTraversalBinaryTree(root.leftChild)        PreorderTraversalBinaryTree(root.rightChild)# in-orderdef InorderTraversalBinaryTree(root):    if root:        InorderTraversalBinaryTree(root.leftChild)        print ('%d | ' % root.data,)        InorderTraversalBinaryTree(root.rightChild)# post-orderdef PostorderTraversalBinaryTree(root):    if root:        PostorderTraversalBinaryTree(root.leftChild)        PostorderTraversalBinaryTree(root.rightChild)        print ('%d | ' % root.data,)# layer-orderdef TraversalByLayer(root, length):    stack = []    stack.append(root)    for x in range(length):        node = stack[x]        print ('%d | ' % node.data,)        if node.leftChild:            stack.append(node.leftChild)        if node.rightChild:            stack.append(node.rightChild)if __name__ == '__main__':    dataSource = [3, 4, 2, 6, 7, 1, 8, 5]    length = len(dataSource)    BTree = InitBinaryTree(dataSource, length)    print ('****NLR:')    PreorderTraversalBinaryTree(BTree)    print ('\n****LNR')    InorderTraversalBinaryTree(BTree)    print ('\n****LRN')    PostorderTraversalBinaryTree(BTree)    print ('\n****LayerTraversal')    TraversalByLayer(BTree, length)

原创粉丝点击