Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.5 kB
1
Indexable
Never
class BinaryTree{
    static class Node{
        int key;
        Node left, right;

        public Node(int item){
            key = item;
            left = right = null;
        }
    }

    Node root;

    BinaryTree(){
        root = null;
    }

    BinaryTree(int value){
        root = new Node(value);
    }

    public Node getNode(){
        return root;
    }

    void inorder(){
        inorderRecurs(root);
    }

    //8 4 9 2 5 1 6 7 3
    void inorderRecurs(Node root){
        if(root != null){
            inorderRecurs(root.left);
            System.out.println(root.key);
            inorderRecurs(root.right);
        }
    }

    public static void main(String [] args){

        BinaryTree tree = new BinaryTree(1);

        Node nodeLeft1 = new Node(2);
        Node nodeRight1 = new Node(3);

        tree.getNode().left = nodeLeft1;
        tree.getNode().right = nodeRight1;

        Node nodeLeft2 = new Node(4);
        Node nodeLeft3 = new Node(5);

        nodeLeft1.left = nodeLeft2;
        nodeLeft1.right = nodeLeft3;

        Node nodeRight2 = new Node(6);
        Node nodeRight3 = new Node(7);

        nodeRight1.left = nodeRight2;
        nodeRight2.right = nodeRight3;

        Node nodeLeft4 = new Node(8);
        Node nodeLeft5 = new Node(9);

        nodeLeft2.left = nodeLeft4;
        nodeLeft2.right = nodeLeft5;

        tree.inorder();

    }

}