数据结构二叉树代码(先序,中序,后序以及通过先中后序读取二叉树) python

数据结构二叉树代码(先序,中序,后序以及通过先中后序读取二叉树) python,第1张

好久不见,甚是想念!

各位如果有兴趣与网络和数学建模可以关注我的朋友:朔方鸟


现在我们进入正题,未来我将介绍数据结构与算法与大家一起学习一起进步,基础的链表我们就暂且不表,现有二叉树讲起。

第一,我们要做二叉树的题目,我们要先定义二叉树:

       我们需要知道二叉树由当前的数据和他的左右子节点组成,那么我们写起来非常容易。

下面由我做的示范,可以参考一下。

class BTree:
    def __init__(self, value, left, right):
        self.data = value
        self.left = left
        self.right = right

第二点,我们要先学会三种遍历。

对于三种的理解,我觉得大家可以看一下正弦定理大佬的想法,大家看明白可以去给大佬点点关注,大佬写的非常详细。

下面为我写的代码。

def pre(root):  # 先序遍历
    if root != None:
        print(root.data, end=' ')
        pre(root.left)
        pre(root.right)


def onorder(root):  # 中序遍历
    if root != None:
        onorder(root.left)
        print(root.data, end=' ')
        onorder(root.right)


def endorder(root):  # 后序遍历
    if root != None:
        endorder(root.left)
        endorder(root.right)
        print(root.data, end=' ')

 第三点,怎么通过先中或者后中进行读取:

要想明白这个,我们需要先去思考中序的意义是什么,中序很明显为把他的左右子树通过母节点分开了,明白了这个,我们就明白了,如果没有中序遍历,我们没法确定这是从左子树来的,还是右子树来的。那既然这样,我们就拿先序举例吧,那我们应该怎么做呢?

1、我们可以通过先序遍历找到节点

2、将这个点从中序遍历中找到

3、在这个点左边的就是他的左子树,右边是它的右子树,然后,可以知道左子树第一个为左子节点,右子树第一个为右子节点

......

那我们把思想转化为代码看看吧:

def preon(arr1, arr2):  # 根据先序,中序求二叉树(arr1为先序,arr2为中序)
    if len(arr1) == 0:
        return None
    else:
        root = arr1[0]
        root_l = -1
        if root in arr2:
            root_l = arr2.index(root)
        # for i in range(len(arr2) - 1):
        #     if arr2[i] == root:
        #         root_l = i
        #         break
        larr2 = arr2[0:root_l]
        rarr2 = arr2[root_l + 1:]
        ll = len(larr2)
        larr1 = arr1[1:ll + 1]
        rarr1 = arr1[ll + 1:]
        lroot = preon(larr1, larr2)
        rroot = preon(rarr1, rarr2)
        return BTree(root, lroot, rroot)

 同理,后中排序为:

def onend(arr1, arr2):  # 根据中序,后序求二叉树(arr1为后序,arr2为中序)
    x = len(arr1)
    if x == 0:
        return None
    else:
        root = arr1[x - 1]
        root_l = -1
        for i in range(len(arr2)):
            if arr2[i] == root:
                root_l = i
                break
        larr2 = arr2[0:root_l]
        rarr2 = arr2[root_l + 1:]
        ll = len(larr2)
        larr1 = arr1[0:ll]
        rarr1 = arr1[ll:x - 1]
        lroot = onend(larr1, larr2)
        rroot = onend(rarr1, rarr2)
        return BTree(root, lroot, rroot)

 然后附上总代码:

# Description: 命里有时终须有,命里无时莫强求
# Autor: Neptune
# Date: 2022/4/21 12:53
class BTree:
    def __init__(self, value, left, right):
        self.data = value
        self.left = left
        self.right = right


def pre(root):  # 先序遍历
    if root != None:
        print(root.data, end=' ')
        pre(root.left)
        pre(root.right)


def onorder(root):  # 中序遍历
    if root != None:
        onorder(root.left)
        print(root.data, end=' ')
        onorder(root.right)


def endorder(root):  # 后序遍历
    if root != None:
        endorder(root.left)
        endorder(root.right)
        print(root.data, end=' ')


def preon(arr1, arr2):  # 根据先序,中序求二叉树(arr1为先序,arr2为中序)
    if len(arr1) == 0:
        return None
    else:
        root = arr1[0]
        root_l = -1
        if root in arr2:
            root_l = arr2.index(root)
        # for i in range(len(arr2) - 1):
        #     if arr2[i] == root:
        #         root_l = i
        #         break
        larr2 = arr2[0:root_l]
        rarr2 = arr2[root_l + 1:]
        ll = len(larr2)
        larr1 = arr1[1:ll + 1]
        rarr1 = arr1[ll + 1:]
        lroot = preon(larr1, larr2)
        rroot = preon(rarr1, rarr2)
        return BTree(root, lroot, rroot)


def onend(arr1, arr2):  # 根据中序,后序求二叉树(arr1为后序,arr2为中序)
    x = len(arr1)
    if x == 0:
        return None
    else:
        root = arr1[x - 1]
        root_l = -1
        for i in range(len(arr2)):
            if arr2[i] == root:
                root_l = i
                break
        larr2 = arr2[0:root_l]
        rarr2 = arr2[root_l + 1:]
        ll = len(larr2)
        larr1 = arr1[0:ll]
        rarr1 = arr1[ll:x - 1]
        lroot = onend(larr1, larr2)
        rroot = onend(rarr1, rarr2)
        return BTree(root, lroot, rroot)


def leftt(root):


arr1 = ['A', 'B', 'D', 'G', 'H', 'C', 'E', 'I', 'F']
arr2 = ['G', 'D', 'H', 'B', 'A', 'E', 'I', 'C', 'F']
arr3 = ['G', 'H', 'D', 'B', 'I', 'E', 'F', 'C', 'A']
# r = preon(arr1, arr2)
r = onend(arr3, arr2)

pre(r)
print()
onorder(r)
print()
endorder(r)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/726679.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-26
下一篇 2022-04-26

发表评论

登录后才能评论

评论列表(0条)

保存