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

J_Y
  • 143
  • 4
  • 16
  • 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... – Tomerikoo Oct 31 '20 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? – Kevin Sheng Oct 31 '20 at 14:47
  • @KevinSheng I would like to print out the all the nodes in the Binary tree :( – J_Y Oct 31 '20 at 14:48
  • Possible duplicate: https://stackoverflow.com/questions/34012886/print-binary-tree-level-by-level-in-python – Tomerikoo Oct 31 '20 at 14:50
  • You may want to add another function to your node class, that prints its data and subnodes. – Wups Oct 31 '20 at 14:57

3 Answers3

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
mujjiga
  • 16,186
  • 2
  • 33
  • 51
  • hi so what if i turned "t" as an object for binary tree – J_Y Oct 31 '20 at 15:01
  • 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. – mujjiga Oct 31 '20 at 15:03
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)
Aziz Sonawalla
  • 2,482
  • 1
  • 5
  • 6
  • 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 – J_Y Oct 31 '20 at 14:49
  • You cannot do `t.root` because `t` is a function, not an object. Please look at the code I added – Aziz Sonawalla Oct 31 '20 at 14:56
  • ohh no Thanks @Aziz Sonawalla Now my question was how to create a binary tree as an object t – J_Y Oct 31 '20 at 15:00
  • as I dont know what t.root at first – J_Y Oct 31 '20 at 15:01
  • 1
    oh, in that case just do `t = t()` (but I suggest renaming the function then). I'll add the sample code above – Aziz Sonawalla Oct 31 '20 at 15:04
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 :(

J_Y
  • 143
  • 4
  • 16