0

I'm currenttly fiddling with binary trees, and implemented a code creating binary tree from its parent array on Java. However, when I tried to rewrite the same algorithm on python (I am new to python), I stumbled onto the fact that code returns an emty tree:

Python code (isn't working properly):

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

The problem, it seems, is in the line root = constructed[i], where root is considered to be a local variable and unused in the constructNode(). I tried global root = TreeNode(),global root = TreeNode(0), but python counts it as an equation or as undefined at module level. Could you please explain, what I have to do to fix it?

Java code (working one, for comparison:)

  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);
    }
TheDoctor
  • 105
  • 1
  • 9
  • 2
    What does _"isn't working properly"_ mean exactly? If you want to display the contents of each `TreeNode`, given them a [`__str__()`](http://stackoverflow.com/questions/3691101/what-is-the-purpose-of-str-and-repr-in-python) method. – Christian Dean May 11 '17 at 00:27
  • @ChristianDean, I want to return the values of the binary tree that was built from the input (parent array). E.g., parent array: -1 0 1, print tree result : 2 1 0 I'm not well-aquainted with python, so could you please elaborate? I need to override `_str_()` method? – TheDoctor May 11 '17 at 00:39
  • Yes, your correct. You need to create a `__str__()` method inside of your `TreeNode` class. You then need to return a "string representation of your class". Basically, what you want to be printed out when you do `print(TreeNode())`. In your case you probably want to return `str(self.data)`. For an example implementation see section 17.6 of [this article](http://www.greenteapress.com/thinkpython/html/book018.html). – Christian Dean May 11 '17 at 00:45
  • @ChristianDean, thank you, that heplped. This gave me an understanding of the fact that my tree is somewhy empty. Hmmmm – TheDoctor May 11 '17 at 00:53
  • Ha. Well good-luck man. Glad I could help. – Christian Dean May 11 '17 at 00:55

0 Answers0