Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java Binary Search Tree

//Java Binary Search Tree with In-Order, Pre-Order and Post-Order Traversals
public class BinarySearchTree {
    public static void main(String[] args) {
        searchSolution s = new searchSolution();

        s.insertionkey('J');
        s.insertionkey('B');
        s.insertionkey('E');
        s.insertionkey('C');
        s.insertionkey('G');
        s.insertionkey('A');
        s.insertionkey('H');
        s.insertionkey('I');
        s.insertionkey('D');
        s.insertionkey('F');

        System.out.println("IN ORDER Search");
        s.InOrderTraversal(s.root);
        System.out.println();
        System.out.println();

        System.out.println("PRE ORDER Search");
        s.PreOrderTraversal(s.root);
        System.out.println();
        System.out.println();

        System.out.println("POST ORDER Search");
        s.PostOrderTraversal(s.root);
    }

}

class searchSolution {
    node root;

    void insertionkey(char key) {
        root = insertionInTree(root, key);
    }

    node insertionInTree(node root, char key) {
        if (root == null) {
            root = new node(key);
            return root;
        }

        if (key < root.key) {
            root.left = insertionInTree(root.left, key);
        } else if (key > root.key) {
            root.right = insertionInTree(root.right, key);
        }
        return root;
    }

    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 + " ");
        }
    }
}

class node { 
    char key;
    node left, right;

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

PREVIOUS NEXT
Code Example
Java :: java byte array to blob 
Java :: enhanced for loops 
Java :: how to set edittext color in android 
Java :: Caused by: android.view.InflateException: Binary XML file line 
Java :: bufferedreader 
Java :: java length of array 
Java :: set preference value android 
Java :: java lambda 
Java :: java abstract class 
Java :: java check if instance of subclass 
Java :: Get the number of weeks between two dates 
Java :: synchronized block in java 
Java :: finding min and max from given number in java 
Java :: how to add a number to the ascii value of a char in java 
Java :: imperative programming paradigm example 
Java :: how to change my file into binary data using java 
Java :: coin flip in java 
Java :: code wars jaden casting java 
Java :: android studio fecth audio from app directory 
Sql :: mysql set max connections 
Sql :: postgresql update sequence next value 
Sql :: show size of all tables postgres 
Sql :: oracle find unusable indexes 
Sql :: Duplicating a MySQL table with all the data Command 
Sql :: how to restart mysql in linux 
Sql :: alter user password postgres 
Sql :: update part of a string in mysql 
Sql :: Mysql query add column with default string value 
Sql :: oracle modify column type 
Sql :: The metadata storage is not up to date, please run the sync-metadata-storage command to fix this issue. 
ADD CONTENT
Topic
Content
Source link
Name
3+6 =