Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

inorder preorder postorder

preorder: parent => left => right
inorder: left => parent => right
postorder: left => right => parent
Comment

inorder preorder postorder

Hint to Remember: 
  IN means root is in middle: inorder: left => root => right 
  PRE means root is at front: preorder: root => left => right
  POST means root is at the end: postorder: left => right => root
Always left comes first than right
Comment

inorder traversal of tree

//*******this is a C program for implementation and searching in A BT*******
#include<stdlib.h>
#include <stdio.h>

struct BinaryTree{
    int data;
    struct BinaryTree*right;
    struct BinaryTree*left;
};

struct BinaryTree*createnode(int val){
    struct BinaryTree*root=(struct BinaryTree*)malloc(sizeof(struct BinaryTree));
    root->data=val;
    root->left=NULL;
    root->right=NULL;
    
}

void inorder(struct BinaryTree*root){
    if(root==NULL){
    return;
    }
    else {inorder(root->left);
    printf("%d->",root->data);
    inorder(root->right);
}

}
 
void preorder(struct BinaryTree*root){
    if(root==NULL){
        return;
    }
    else {
        printf("%d->",root->data);
        preorder(root->left);
        preorder(root->right);
    }
}

void postorder(struct BinaryTree*root){
    if(root==NULL){
        return;
    }
    else{
        postorder(root->left);
        postorder(root->right);
        printf("%d->",root->data);
    }
}
int main()
{
    printf("Lets grow the tree
");
    struct BinaryTree*root=createnode(1);
    root->left=createnode(2);
    root->right=createnode(3);
    root->left->left=createnode(4);
    root->left->right=createnode(5);
    
    printf("tree has grown up
");
    
    printf("Inorder traversal ");
    inorder(root);printf("NULL");

    printf("
preorder traversal ");
    preorder(root);printf("NULL");
    
    printf("
Postorder  traversal");
    postorder(root);printf("NULL");
    
    return 0 ;
}
Comment

Tree: Postorder Traversal

def postOrder(root):
    if root:
        postOrder(root.left)
        postOrder(root.right)
        print(root, end = " ")
Comment

Tree Traversals inorder,preorder and postorder

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

inorder traversal

class Solution {
    public List<Integer> inorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        return dfs(root, list);
    }
    private List<Integer> dfs(TreeNode root, List<Integer> list)
    {
        if(root == null)
            return list;
        list = dfs(root.left, list);
        list.add(root.val);
        return dfs(root.right,list);
    }
}
Comment

Tree: Inorder Traversal

def inOrder(root):
    if root:
        inOrder(root.left)
        print(root, end = " ")
        inOrder(root.right)        
        
Comment

Preorder, inorder & postorder traversal code

class Node:
    def __init__(self, k):
        self.left = None
        self.right = None
        self.key = k


def inorder(root):
    if root != None:
        inorder(root.left)
        print(root.key)
        inorder(root.right)


# Driver Code

root = Node(10)
root.left = Node(20)
root.right = Node(30)
root.right.left = Node(40)
root.right.right = Node(50)

inorder(root)

# time complexity (using recurrence tree method)  - O(n)
where, 
n == total nodes 
# space complexity - O(height of tree)
Note: height can be both n (if each node has exactly 1 child) and 
log(n) (if every node has exactly 2 children).

# IMP NOTE : FOR PREORDER AND POSTORDER JUST ORDER OF STATEMENTS CHANGE

for ex:
below is preorder & postorder traversal with same time & space complexity as inorder

def preorder(root):
    if root == None:
        return
    print(root.key)
    preorder(root.left)
    preorder(root.right)

def postorder(root):
    if root == None:
        return
    preorder(root.left)
    preorder(root.right)
    print(root.key)
    
Comment

PREVIOUS NEXT
Code Example
Python :: generate coordinates python 
Python :: iterate array python with index 
Python :: recurrent neural network pytorch 
Python :: Async-Sync 
Python :: take columns to rows in pandas 
Python :: convert exception to string python 
Python :: django button 
Python :: drop row pandas column value not a number 
Python :: speech enhancement techniques 
Python :: changes in settings.py for media storage without db 
Python :: How to clone or copy a list in python 
Python :: import sentence transformers 
Python :: euclidean distance 
Python :: binary search in python 
Python :: when to use finally python 
Python :: python strip function 
Python :: Removing Elements from Python Dictionary Using pop() method 
Python :: longest palindromic substring using dp 
Python :: truncatechars django 
Python :: oop in python 
Python :: pandas df number of columns 
Python :: run pyinstaller from python 
Python :: plotly express change legend labels 
Python :: youtube mp3 downloader python 
Python :: python 2 
Python :: python size of set 
Python :: how to remove a string in python 
Python :: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:997) 
Python :: adding array to array python 
Python :: indent python code 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =