前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数据结构学习-python实现-树--0413

数据结构学习-python实现-树--0413

原创
作者头像
到不了的都叫做远方
修改2020-04-13 14:30:14
3530
修改2020-04-13 14:30:14
举报

树的特点:

  • 1.分类体系是层次化的,越靠近根部,性质越普遍,越靠近叶子,性质越独特。
  • 2.同一父节点下的不同子节点相互隔离且独立。
  • 3.每个叶节点具有唯一性。

树的定义:由若干节点以及两两相连节点的边组成,具备以下性质:

  • 1.其中一个节点被设置为根
  • 2.每个节点n(除了根节点)都恰有一条来自父节点p的边。
  • 3.每个节点从根节点开始的路径是唯一的。
  • 若每个节点最多有两个子节点,称为二叉树。
代码语言:python
代码运行次数:0
复制
# 用嵌套列表实现二叉树
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 getrootvalue(root):
    return root[0]

def setrootvalue(root, newvalue):
    root[0] = newvalue

def getleftchild(root):
    return root[1]

def getrightchild(root):
    return root[2]

r = binarytree(3)
insertleft(r, 4)
insertright(r, 5)
insertright(r, 6)
insertright(r, 7)

l = getleftchild(r)
print(l)

setrootvalue(l, 9)
print(r)

insertleft(l, 11)
print(r)
代码语言:python
代码运行次数:0
复制
# 递归的思想生成二叉树
class BinaryTree:
    def __init__(self, rootobj):
        self.key = rootobj  # 节点值
        self.leftchild = None  # 左子树
        self.rightchild = None  # 右子树

    def insertleft(self, newnode):  # 记住每个节点的插入,都是子树,叶子只是子树为None。
        t = BinaryTree(newnode)
        if self.leftchild is None:
            self.leftchild = t
        else:
            t.leftchild = self.leftchild
            self.leftchild = t

    def insertright(self, newnode):
        t = BinaryTree(newnode)
        if self.rightchild is None:
            self.rightchild = t
        else:
            t.rightchild = self.rightchild
            self.rightchild = t

    def getrightchild(self):
        return self.rightchild

    def getleftchild(self):
        return self.leftchild

    def setrootvalue(self, obj):
        self.key = obj

    def getrootvalue(self):
        return self.key

r = BinaryTree('a')
r.insertleft('b')
r.insertright('c')
r.getrightchild().setrootvalue('hello')
r.getleftchild().insertright('d')

# 生成的是树,需要使用遍历的方法查看。此方法是中序遍历。
def printexp(tree):
    sval = ''
    if tree:
        sval = printexp(tree.getleftchild())
        sval = sval + str(tree.getrootvalue())
        sval = sval + printexp(tree.getrightchild())
        if len(sval) != 1:
            sval = '(' + sval + ')'
    return sval
    
代码语言:python
代码运行次数:0
复制
def preorder(tree):  # 前序遍历,记住递归的思想
    if tree:
        print(tree.getrootvalue())
        preorder(tree.getleftchild())
        preorder(tree.getrightchild())


def postorder(tree):  # 后序遍历
    if tree is not None:
        postorder(tree.getleftchild())
        postorder(tree.getrightchild())
        print(tree.getrootvalue())


def inorder(tree):  # 中序遍历
    if tree is not None:
        inorder(tree.getleftchild())
        print(tree.getrootvalue())
        inorder(tree.getrightchild())

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档