Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

java tree traversal

//BINARY TREE TRAVERSAL
//---------------------


public class traversal{
    
    
    public static void main(String[] args) {
        treetraversal t = new treetraversal();
        
        t.root = new node('A'); // initializing the root node
        t.root.left = new node('B'); // initializing the left node
        t.root.right = new node('C'); // initializing the right node
        
        t.root.left.left = new node('D'); // initializing the sub-left node of the left node
        t.root.left.right = new node('E'); // initializing the sub-right node of the left node
        
        t.root.right.left = new node('F'); // initializing the sub-left node of the right node
        t.root.right.right = new node('G'); // initializing the sub-right node of the right node
        
        //this tree can be made as large as the we want it to be by adding further sub nodes
        
        System.out.println("IN ORDER TRAVERSAL");
        t.InOrderTraversal(t.root);
        System.out.println();
        System.out.println();

        System.out.println("PRE ORDER TRAVERSAL");
        t.PreOrderTraversal(t.root);
        System.out.println();
        System.out.println();
        
        System.out.println("POST ORDER TRAVERSAL");
        t.PostOrderTraversal(t.root);
    }
}

class node{ //as there are nodes in trees
    char key;//as every node has a value or key
    node left,right;//as every node will have a left and a right child

    node(char KEY){
        this.key = KEY;
    }
}

class treetraversal{
    /*there are thre types of traversals
    1. InOrder Traversal
    2. PreOrder Traversal
    3.PostOrder Traversal
    */

    node root;//as every tree has a root node to which there exist left and right nodes
    
        void InOrderTraversal(node n){
            //InOrder Traversal = Left Root Right
            if(n!=null){
                InOrderTraversal(n.left);
                System.out.print(n.key + " ");
                InOrderTraversal(n.right);
            }
        }

    void PreOrderTraversal(node n){
        //PreOrder Traversal = Root Left Right
        if(n!=null){
            System.out.print(n.key + " ");
            PreOrderTraversal(n.left);
            PreOrderTraversal(n.right);
        }
    }
    
    void PostOrderTraversal(node n){
        //PostOrder Traversal = Left Right Root
        if(n!=null){
            PostOrderTraversal(n.left);
            PostOrderTraversal(n.right);
            System.out.print(n.key + " ");
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: generate hash in java 
Java :: switch in java 
Java :: implement queue using array in java 
Java :: iterate through arraylist java 
Java :: static block in java 
Java :: adding watermark to excel 
Java :: array of string java 
Java :: How to perform in-order traversal of a binary tree? 
Java :: how do you handle exceptions in java 
Java :: java enum in switch statement 
Java :: list of list in java 
Java :: method overriding in java 
Java :: java map sorted by key 
Java :: check if object is empty java 8 
Java :: classpath in java 
Java :: Permutations of array in Java 
Java :: java evaluate two expressions in if statemenmt 
Java :: how todo a timeout test java 
Java :: natural log in java 
Java :: first none duplicated char 
Java :: square oot of 154 
Java :: Creategoogle maps marker with custom image/bitmap 
Java :: countdown timer with seekbar 
Java :: destroy fragent after navigating 
Java :: java try-with-resources nested streams 
Java :: How can I add a listener on the ok button of JOptionPane 
Java :: .throwFor in java 
Java :: pagina en java 
Java :: java.lang.String cannot be cast to java.util.Map 
Java :: FirebaseCrashlyticsPlugin.java uses or overrides a deprecated API. 
ADD CONTENT
Topic
Content
Source link
Name
6+8 =