Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

Java 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 :: scrollbar in textarea java 
Java :: how to find last element in array java 
Java :: can abstract class have constructor in java 
Java :: can you override static methods in java 
Java :: java bufferedreader 
Java :: The JAVA_HOME environment variable is not defined correctly, this environment variable is needed to run this program. 
Java :: gson parse json 
Java :: java arrow function 
Java :: java method exercises 
Java :: call activity method from adapter 
Java :: java remove map key 
Java :: enum to string java 
Java :: how to create a boolean list in java 
Java :: how to be good at javasctript 
Java :: start an activity in adapter 
Java :: x = x + y; in java 
Java :: multiple root tags android manifest 
Java :: Simple Write a simple Java program that prints a staircase or a figure as show 
Java :: interact with databse java 
Sql :: mysql create user with mysql_native_password 
Sql :: cannot truncate a table referenced in a foreign key constraint 
Sql :: update sql server from another table 
Sql :: wordpress database add admin 
Sql :: postgresql blocked on mac 
Sql :: sql server beginning of month 
Sql :: update password postgres user 
Sql :: .... ERROR! The server quit without updating PID file mysql centos 
Sql :: how to start mysql 
Sql :: create new table plsql 
Sql :: postgres change owner of schema 
ADD CONTENT
Topic
Content
Source link
Name
3+5 =