先建立二叉树节点,有一个data数据域,left,right 两个指针域
复制代码 代码如下:
class TreeNode(object):
def __init__(self, left=0, right=0, data=0):
self.left = left
self.right = right
self.data = data
复制代码 代码如下:
class BTree(object):
def __init__(self, root=0):
self.root = root
手动建立二叉树
复制代码 代码如下:
node1 = TreeNode(data=1)
node2 = TreeNode(node1, 0, 2)
node3 = TreeNode(data=3)
node4 = TreeNode(data=4)
node5 = TreeNode(node3, node4, 5)
node6 = TreeNode(node2, node5, 6)
node7 = TreeNode(node6, 0, 7)
node8 = TreeNode(data=8)
root = TreeNode(node7, node8, 'root')
bt = BTree(root)
Copyright© 2013-2020
All Rights Reserved 京ICP备2023019179号-8