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

29 篇文章 19 订阅

好久不见,甚是想念!

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


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

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

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

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

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)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,我明白了。这是一道关于二叉树的综合题目,我会尽力回答你的问题。 首,根据输入的完全二叉树列,我们可以通过递归的方式建立这棵树。具体来说,我们可以按照列的顺逐个读取结点,如果读到的是一个实结点,则创建一个新结点并将其赋值为当前读取的结点;如果读到的是一个虚结点,则直接返回 NULL。在创建完当前结点后,我们可以递归地创建其左子树和右子树,最终返回当前结点。 代码如下: ```python class TreeNode: def __init__(self, val=0, left=None, right=None): self.val = val self.left = left self.right = right def build_tree(preorder): n = len(preorder) if n == 0: return None root = TreeNode(val=preorder[0]) if n == 1: return root left_preorder = [] right_preorder = [] i = 1 while i < n: if i % 2 == 1: left_preorder.append(preorder[i]) else: right_preorder.append(preorder[i]) i += 1 root.left = build_tree(left_preorder) root.right = build_tree(right_preorder) return root ``` 接下来,我们可以实现和后遍历的递归操作。其遍历的操作很简单,只需要按照“根左右”的顺遍历整棵树即可;遍历和后遍历的操作可以根据递归的顺进行调整。 代码如下: ```python def preorder_traversal(root): if root is None: return [] res = [root.val] res += preorder_traversal(root.left) res += preorder_traversal(root.right) return res def inorder_traversal(root): if root is None: return [] res = inorder_traversal(root.left) res += [root.val] res += inorder_traversal(root.right) return res def postorder_traversal(root): if root is None: return [] res = postorder_traversal(root.left) res += postorder_traversal(root.right) res += [root.val] return res ``` 此外,我们还可以实现遍历的非递归操作。具体来说,我们可以使用一个栈来模拟递归的过程。对于当前结点,如果其左子树非空,则将其入栈,并继续遍历左子树;否则,输出当前结点的值,并遍历其右子树。当栈为空时,遍历结束。 代码如下: ```python def inorder_traversal_iter(root): res = [] stack = [] while root is not None or len(stack) > 0: while root is not None: stack.append(root) root = root.left if len(stack) > 0: node = stack.pop() res.append(node.val) root = node.right return res ``` 最后,我们可以求出树的高度、叶子结点个数和结点总数。其,树的高度可以通过递归地计算左子树和右子树的高度,然后取其较大值并加一得到;叶子结点个数可以通过递归地计算左子树和右子树的叶子结点个数,然后相加得到;结点总数可以通过递归地计算左子树和右子树的结点总数,然后加一得到。 代码如下: ```python def tree_height(root): if root is None: return 0 left_height = tree_height(root.left) right_height = tree_height(root.right) return max(left_height, right_height) + 1 def leaf_node_count(root): if root is None: return 0 if root.left is None and root.right is None: return 1 return leaf_node_count(root.left) + leaf_node_count(root.right) def node_count(root): if root is None: return 0 return node_count(root.left) + node_count(root.right) + 1 ``` 综上所述,我们可以通过以上代码实现对二叉树的各种操作。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值