0
# Binary tree node
class node:
     
    def __init__(self, data):
        self.left=None
        self.right=None
        self.data=data
  
# Function to create a new
# Binary node
def newNode(data):
    return node(data)

def t():
    root=newNode("A")
    root.left = newNode("B")
    root.right = newNode("C")
    root.left.left = newNode("D")
    root.left.right = newNode("G")
    root.right.right = newNode("E")
    root.right.right.left = newNode("F")
    

print(t)

Hi, I have tried to create a Binary Tree above but I did not managed to print out the binary tree when I print "t". Instead of a Binary Tree, it shows me this: enter image description here

5
  • Well t is a function... Did you mean to call it - t()? Still you will not see anything printed, because t doesn't print anything... Commented Oct 31, 2020 at 14:47
  • You haven't actually called t. The thing is, even if you did, it still wouldn't print out anything meaningful. What do you want to print out? Commented Oct 31, 2020 at 14:47
  • @KevinSheng I would like to print out the all the nodes in the Binary tree :( Commented Oct 31, 2020 at 14:48
  • Possible duplicate: stackoverflow.com/questions/34012886/… Commented Oct 31, 2020 at 14:50
  • You may want to add another function to your node class, that prints its data and subnodes. Commented Oct 31, 2020 at 14:57

3 Answers 3

3

Function t just creates a binary tree. If you want to print a tree you need to traverse it and print it. Depending on the way you want to print a tree, there are different traversal techniques, the popular of which are Inorder, Preorder and Postorder. Check this wiki link for tree traversal methods.

Your code has to be extended to do the required tree traversal. Sample is below:

# Binary tree node
class node:
     
    def __init__(self, data):
        self.left=None
        self.right=None
        self.data=data
  
# Function to create a new
# Binary node
def newNode(data):
    return node(data)

def t():
    root=newNode("A")
    root.left = newNode("B")
    root.right = newNode("C")
    root.left.left = newNode("D")
    root.left.right = newNode("G")
    root.right.right = newNode("E")
    root.right.right.left = newNode("F")
    return root


def in_order(root):
    if root:
        in_order(root.left)
        print (root.data)
        in_order(root.right) 

def pre_order(root):
    if root:
        print (root.data)
        pre_order(root.left)
        pre_order(root.right)
        
def post_order(root):
    if root:        
        post_order(root.left)
        post_order(root.right)
        print (root.data)
        
root = t()


print ("In Order")
in_order(root)
print ("Pre Order")
pre_order(root)
print ("Post Order")
post_order(root)

Output:

In Order
D
B
G
A
C
F
E
Pre Order
A
B
D
G
C
E
F
Post Order
D
G
B
F
E
C
A
Sign up to request clarification or add additional context in comments.

2 Comments

hi so what if i turned "t" as an object for binary tree
But you still have to write the code to print it. Since it is a custom data structure you are building you still have to tell how to print it.
2

So two things:

When you do print(t) instead of print(t()), there's a difference. print(t) prints the function object itself, whereas print(t()) prints the result returned by the function.

However, even if you do the latter, you'll print None because t() doesn't return anything. You'll need to return root from t() and also you'll have to write a special function that iterates through the tree to print each node's value (if that's what you want)

Here's an example:

# Binary tree node
class node:
     
    def __init__(self, data):
        self.left=None
        self.right=None
        self.data=data
  
# Function to create a new
# Binary node
def newNode(data):
    return node(data)

def t():
    root=newNode("A")
    root.left = newNode("B")
    root.right = newNode("C")
    root.left.left = newNode("D")
    root.left.right = newNode("G")
    root.right.right = newNode("E")
    root.right.right.left = newNode("F")
    return root
    

def treeToString(root, level=0):
  ret = "\t"*level+repr(root.data)+"\n"
  if root.left != None:
      ret += treeToString(root.left, level+1)
  if root.right != None:
      ret += treeToString(root.right, level+1)
  return ret

print(treeToString(t()))


# if you want to assign the tree to an object then do this:

tree = t()
print(tree.left.data)
print(tree.right.data)

5 Comments

because i want to create a next function call the print_leaf_nodes_bylevel(t.root, level) @Aziz Sonawalla hence, i would like to create a binary tree function name "t" first before i can do t.root
You cannot do t.root because t is a function, not an object. Please look at the code I added
ohh no Thanks @Aziz Sonawalla Now my question was how to create a binary tree as an object t
as I dont know what t.root at first
oh, in that case just do t = t() (but I suggest renaming the function then). I'll add the sample code above
0
# Binary tree node
class node:
     
    def __init__(self, data):
        self.left=None
        self.right=None
        self.data=data
  
# Function to create a new
# Binary node
def newNode(data):
    return node(data)

def tree():
    root=newNode("A")
    root.left = newNode("B")
    root.right = newNode("C")
    root.left.left = newNode("D")
    root.left.right = newNode("G")
    root.right.right = newNode("E")
    root.right.right.left = newNode("F")
    return root

t = tree()

print(t.left.data)
print(t.right.data)
print(t.root)

How do I access the root of the tree from here?

I have tried printing t.root but it seemed to have an error :(

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.