Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

traversing a tree in python

"""Inorder Traversing"""
def inorder_traversing(self, root):
  res = []
  if root:
    res = self.inorder_traversing(root.left)
    res.append(root.data)
    res = res + inorder_traversing(root.right)
   return res
Comment

traversing a tree in python

"""Post-order"""
    def postorder_traversal(self, root):
        res = []

        if root:
            res = self.predorder_traversal(root.left)
            res = res + self.predorder_traversal(root.right)
            res.append(root.data)
        return res
Comment

traversal tree in python

class Node:
     def __init__(self,data):
          self.data = data
          self.parent = None
          self.left = None
          self.right = None

     def __repr__(self):
          return repr(self.data)

     def add_left(self,node):
         self.left = node
         if node is not None:
             node.parent = self

     def add_right(self,node):
         self.right = node
         if node is not None:
             node.parent = self
'''
Example:
          _2_
        /       
       7         9
      /          
     1   6         8
         /        / 
       5   10   3   4
'''
def create_tree():
    two = Node(2)
    seven = Node(7)
    nine = Node(9)
    two.add_left(seven)
    two.add_right(nine)
    one = Node(1)
    six = Node(6)
    seven.add_left(one)
    seven.add_right(six)
    five = Node(5)
    ten = Node(10)
    six.add_left(five)
    six.add_right(ten)
    eight = Node(8)
    three = Node(3)
    four = Node(4)
    nine.add_right(eight)
    eight.add_left(three)
    eight.add_right(four)

    # now return the root node
    return two

def pre_order(node):
    print(node)
    if node.left:
        pre_order(node.left)
    if node.right:
        pre_order(node.right)

def in_order(node):
    if node.left:
        in_order(node.left)
    print(node)
    if node.right:
        in_order(node.right)

def post_order(node):
    if node.left:
        post_order(node.left)
    if node.right:
        post_order(node.right)
    print(node)

if __name__ == "__main__":
    root = create_tree()
    print("
Pre-order traversal:")
    pre_order(root)
    print("
In-order traversal:")
    in_order(root)
    print("
Post-order traversal:")
    post_order(root)
Comment

PREVIOUS NEXT
Code Example
Python :: Data Structure tree in python 
Python :: python - login 
Python :: discord bot python 
Python :: continue statement in python 
Python :: is there a null data type in python 
Python :: negative slicing in python list 
Python :: python call function that need args with decorator 
Python :: python environment 
Python :: order_by django queryset order by ordering 
Python :: Label enconding code with sklearn 
Python :: purpose of migration folder in django 
Python :: sorted multiple keys python 
Python :: flask app run 
Python :: upgrade python version windows 
Python :: python add list 
Python :: remove timezone from a datetime object? 
Python :: python print not working 
Python :: heroku how to access config vars django 
Python :: python remove character from string 
Python :: python class getters and setters 
Python :: run ansible playbook python 
Python :: how to earn money as a python developer 
Python :: python rounding numbers to n digits 
Python :: import os in python 
Python :: merge sort in python 
Python :: sessions in flask 
Python :: pybase64 
Python :: python package install 
Python :: solving linear equation using numpy 
Python :: self object 
ADD CONTENT
Topic
Content
Source link
Name
9+1 =