"""
节点与引用法构建和访问二叉树
"""
class BinaryTree:
"""
构造一个二叉树的简单类
本方法的要点是:
属性left和right会指向BinaryTree类的其他实例
"""
def __init__(self
, rootObj
):
self
.key
= rootObj
self
.leftChild
= None
self
.rightChild
= None
def insertLeft(self
, newNode
):
"""
插入左子树
"""
if self
.leftChild
is None:
self
.leftChild
= BinaryTree
(newNode
)
else:
t
= BinaryTree
(newNode
)
t
.leftChild
= self
.leftChild
self
.leftChild
= t
def insertRight(self
, newNode
):
"""
插入右子树
"""
if self
.rightChild
is None:
self
.rightChild
= BinaryTree
(newNode
)
else:
t
= BinaryTree
(newNode
)
t
.rightChild
= self
.rightChild
self
.rightChild
= t
def getRootVal(self
):
return self
.key
def setRootVal(self
, newValue
):
self
.key
= newValue
def getLeftChild(self
):
return self
.leftChild
def getRightChild(self
):
return self
.rightChild
转载请注明原文地址:https://tech.qufami.com/read-28483.html