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)
阅读全文
0 0
- python3 二叉树及单链表
- python3 二叉树遍历
- 二叉节点及二叉树
- 树及二叉树
- 树及二叉树
- 树及二叉树
- 树及二叉树
- 二叉树及哈夫曼树
- 哈夫曼树及二叉树
- 树、二叉树及二叉查找树
- 二叉树及二叉搜索树
- 二叉树及特殊的二叉树
- 图解二叉树及二叉树遍历
- 数据结构 二叉树及遍历二叉树
- 二叉树及二叉树的应用
- 二叉树基本性质及二叉树链表类
- 二叉树创建及遍历
- 二叉树创建及遍历
- Java与MongoDB的配合使用备忘3-聚合操作group具体实例
- 禁止弹出软键盘
- 如何按行政区划下载谷歌地图并裁剪
- 45、扑克牌顺子
- 创建信号量RACSignal
- python3 二叉树及单链表
- github简单使用教程
- SET NOCOUNT ON 作用
- select服务器
- jni string 转换
- 前端工程师新手必读
- VIM常用命令
- Netty简单示例----客户端与服务器通信
- [golang note] 错误处理