简体   繁体   中英

How to create a Binary Tree in Python?

# 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: 在此处输入图片说明

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

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)
# 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 :(

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM