从Java转换父母列表构建树到Python

2024-10-03 04:26:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在正在摆弄二叉树,并在Java上实现了一个从其父数组创建二叉树的代码。 然而,当我尝试在python上重写相同的算法时(我对python是新手),我偶然发现代码返回一个emty树:

Python代码(工作不正常):

parent1 = input().split()
parent =[]
constructed =[]

class TreeNode:
    def __init__(self, data):
        self.data = data
        self.left = None
        self.right = None
    def __str__(self):
        return str(self.data)


root = TreeNode(0)

def constructNode(i, constructed, parent):

    if constructed[int(i)] is not None:
        return

    if parent[int(i)] == -1:
        constructed[i] = TreeNode(i)
        root = constructed[i] 
        return

    pc = constructed[parent[i]]
    if pc is None:
        constructNode(parent[i], constructed, parent)

    constructed[i] = TreeNode(i)

    if pc is not None:

        if pc.left is None:

            pc.left = constructed[i]
        else:
            pc.right = constructed[i]


def constructTreeBottomUp(parent):
    n = len(parent)
    constructed = [TreeNode(0) for i in range(n)]
    for i in range(n):
        constructNode(i, constructed, parent)
    return root

def getInOrder(root):
    if root is None:
        return
    if root is not None:
        getInOrder(root.left)
        print(root.data + " ")
        getInOrder(root.right)


root1 = constructTreeBottomUp(parent1)
print("T1", root1)//returns T1 = 0

问题似乎出在第root = constructed[i]行,其中root被认为是局部变量,在constructNode()中未使用。 我试过了 global root = TreeNode()global root = TreeNode(0), 但python将其视为一个等式,或者在模块级别将其视为未定义的。 你能解释一下,我要怎么做才能修好它吗?你知道吗

Java代码(工作代码,用于比较:)

  private class TreeNode
    {
        int data;
        TreeNode left;
        TreeNode right;

        TreeNode(int data)
        {
            this.data = data;
        }
    }

    TreeNode root;

    private void constructNode(int i, TreeNode[] constructed, int[] parent)
    {

        if (constructed[i] != null)
        {
            return;
        }
        if (parent[i] == -1)
        {
            constructed[i] = new TreeNode(i);
            root = constructed[i];
            return;
        }

        if (constructed[parent[i]] == null)
        {
            constructNode(parent[i], constructed, parent);
        }

        constructed[i] = new TreeNode(i);

        if (constructed[parent[i]] != null) // sanity check
        {
            if (constructed[parent[i]].left == null)
            {
                constructed[parent[i]].left = constructed[i];
            }
            else
            {
                constructed[parent[i]].right = constructed[i];
            }
        }
    }


    public TreeNode constructTreeBottomUp(int[] parent)
    {
        TreeNode[] constructed = new TreeNode[parent.length];

        for (int i = 0; i < constructed.length; i++)
        {
            constructNode(i, constructed, parent);
        }
        return root;
    }

    public void getInorder(TreeNode root)
    {
        if (root == null) return;
        getInorder(root.left);
        System.out.println(root.data + " ");
        getInorder(root.right);
    }

Tags: selfrightnonedatareturnifisroot