Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

binary tree java

//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

java non binary tree

class Node
{
    public int data;
    public Node parent;
    public List<Node> children;
}

Node node = new Node(){ data=5, parent=null, children=new List<Node>() }
Comment

PREVIOUS NEXT
Code Example
Java :: Passing data from an activity to a fragment 
Java :: how to set the length to int array in java 
Java :: stream reduce 
Java :: declare matrix in java 
Java :: java sort reverse lambda 
Java :: hashmap java 
Java :: java design patterns 
Java :: how to create a java txt file from programm 
Java :: android hide and show bottom navigation 
Java :: convert character arraylist to array 
Java :: javax 
Java :: calling static method in java 
Java :: Add Elements in java map 
Java :: properties object java 
Java :: abstract class java 
Java :: android videoview not smooth for mp4 
Java :: int to integer array in java 
Java :: madhava kripa 
Java :: java regex of eauations 
Java :: convert kotlin code to java online 
Java :: set the content of a Jlist from an other Jlist (Swing) 
Java :: java bitwise xor 
Java :: Android number format thousands separator 
Java :: java returning an comparable array of inorder traversal of binary tree 
Java :: trémaux’ methode 
Java :: i have use tried catch but still prints exception java 
Java :: java function that returns the index of the largest value in an array 
Java :: success listener with Glide Android java 
Java :: lcd initialize arduino 
Java :: diamonds 
ADD CONTENT
Topic
Content
Source link
Name
4+6 =