树和树算法(1)

1、树的相关术语

1.1、树的性质

  • 1、树是分层的;2、一个节点的子节点和另一个节点的子节点是完全独立的;3、它的每个叶节点都是不同的;4、树的所有下层部分(子树)可以被移动到树的另一个位置二不影响更下层的情况。

1.2、术语表和定义

  • 【节点】:是树的基本构成部分,它包含一个数据元素和若干指向其子树的分支。
    • 节点的度:一个节点拥有子树的个数称为该节点的度。
    • 树的度:整个书中所有节点的度的最大值为该树的度。
    • 一棵树分为:根节点、内部节点、叶节点。根节点只有【出边】,没有入边
    • 几个概念:双亲、孩子、兄弟、堂兄弟、祖先要了解
  • 【节点的层次】:从根节点开始为第一层,后面的层数依次加一。
  • 【树的深度】:树中节点最大的层次称为该树的深度(高度)。

简单的二叉树实现

  • 使用嵌套列表法
'''简单示例'''
#myTree = ['a',['b',['d',[],[]],['e',[],[]]],['c',['f',[],[]],[]]]
def BinaryTree(r):
    return [r,[],[]]
def insertLeft(root, newBranch):
    t = root.pop(1)
    if len(t) > 1:
        root.insert(1,[newBranch,t,[]])
    else:
        root.insert(1,[newBranch,[],[]])
    return root

def insertRight(root, newBranch):
    t = root.pop(2)
    if len(t) > 1:
        root.insert(2,[newBranch,[],t])
    else:
        root.insert(2,[newBranch,[],[]])
    return root

def getRootVal(root):
    return root[0]
def setRootVal(root, newVal):
    root[0] = newVal
def getLeftChild(root):
    return root[1]
def getRightChild(root):
    return root[2]
def getParent(root):
    return root[0]
r = BinaryTree(3)
insertLeft(r,4)
insertLeft(r,5)
insertRight(r,6)
insertRight(r,7)
L = getLeftChild(r)
L
[5, [4, [], []], []]
r
[3, [5, [4, [], []], []], [7, [], [6, [], []]]]
setRootVal(L,9)
r
[3, [9, [4, [], []], []], [7, [], [6, [], []]]]
insertLeft(L,11)
print(r)
[3, [9, [11, [4, [], []], []], []], [7, [], [6, [], []]]]
getRightChild(r)
[7, [], [6, [], []]]
getRightChild(getRightChild(r))
[6, [], []]
p = getParent(L)
p
9
R = getRightChild(r)
R
[7, [], [6, [], []]]
getParent(R)
7
R2 = getRightChild(R)
R2
[6, [], []]

使用节点和引用实现树

class BinaryTree:
    def __init__(self,rootObj):
        self.key = rootObj
        self.leftChild = None
        self.rightChild = None
    def insertLeft(self, newNode):
        if self.leftChild == None:
            self.leftChild = BinaryTree(newNode)
        else:
            t = BinaryTree(newNode)
            t.leftChild = self.leftChild
            self.leftChild = t
    def insertRight(self, newNode):
        if self.rightChild == None:
            self.rightChild = BinaryTree(newNode)
        else:
            t = BinaryTree(newNode)
            t.rightChild = self.rightChild
            self.rightChild = t   
    def getRightChild(self):
        return self.rightChild
    def getLeftChild(self):
        return self.leftChild
    def setRootVal(self,obj):
        self.key = obj
    def getRootVal(self):
        return self.key

r = BinaryTree('a')
r.getRootVal()
'a'
r.insertLeft('b')
r.insertRight('c')
r.getLeftChild()
<__main__.BinaryTree at 0x101e8d0>
r.getRightChild().getRootVal()
'c'
r.getRightChild().setRootVal("hello")
r.getRightChild().getRootVal()
'hello'
# 练习
r = BinaryTree('a')
r.insertLeft('b')
r.insertRight('c')
r.getLeftChild().insertLeft("d")
r.getRightChild().insertRight('e')
r.getRightChild().insertRight('f')
r.getRootVal()
'a'
r.getLeftChild().getRootVal()
'b'
r.getRightChild().getRightChild().getRootVal()
'f'

树的遍历

# 前序遍历
def preorder(tree):
    if tree:
        print(tree.getRootVal(),end=" , ")
        preorder(tree.getLeftChild())
        preorder(tree.getRightChild())
preorder(r)
a , b , d , c , f , e , 
# 后序遍历
def postorder(tree):
    if tree!=None:
        postorder(tree.getLeftChild())
        postorder(tree.getRightChild())
        print(tree.getRootVal(),end=" , ")
postorder(r)
d , b , e , f , c , a , 
# 中序遍历
def inorder(tree):
    if tree != None:
        inorder(tree.getLeftChild())
        print(tree.getRootVal(),end=" , ")
        inorder(tree.getRightChild())
inorder(r)
d , b , a , c , f , e , 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值