从父类列表构建树从java转移到python

时间:2017-05-11 00:22:58

标签: java python python-3.x binary-tree

我目前正在摆弄二叉树,并在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);
    }

0 个答案:

没有答案