# -*- coding:utf=8 -*-
# 二叉树的实现
# 二叉树的前序遍历
class Node(object):
    def __init__(self, index):
        self.index = index
        self.leftChild = None
        self.rightChild = None
class BinaryTree(object):
    def __init__(self, root):
        self.root = root
    def preTravel(self, node):
        if not node:
            return
        print node.index
        self.preTravel(node.leftChild)
        self.preTravel(node.rightChild)
nodeDict = {}
for i in range(1, 10):
    nodeDict[i] = Node(i)
nodeDict[1].leftChild = nodeDict[2]
nodeDict[1].rightChild = nodeDict[3]
nodeDict[2].leftChild = nodeDict[5]
nodeDict[2].rightChild = nodeDict[6]
nodeDict[3].leftChild = nodeDict[7]
nodeDict[7].leftChild = nodeDict[8]
nodeDict[7].rightChild = nodeDict[9]
tree = BinaryTree(nodeDict[1])
tree.preTravel(tree.root)
分类: Python

0 条评论

发表回复

Avatar placeholder

您的电子邮箱地址不会被公开。 必填项已用*标注