//

Trees

A tree is a non-linear Data structure where is data is stored in the form of tree node.

Types of trees

Binary Tree

A binary tree a node can have at most two children.

Binary Search Tree

A binary search tree is like binary tree but its elements on the left child are smaller than the root node and the larger elements are stored on the right side of the tree.

Balanced Tree

Unbalanced Tree

Complete Binary Tree

In Complete Binary Tree (CBT) every level of the tree is fully filled, except for the last level which may or may not be completely filled. The last. the level should be filled from left to right.

Full Binary Tree

A full binary tree is a binary tree where every node has either 0 or 2 children.

Perfect Binary Tree

Perfect binary tree are full and complete. And all the leaf node of this tree are at the same level.

Tree Traversal

Inorder Traversal

void inOrderTraversal(Node root){
        if(root == null){
            return;
        }

        inOrderTraversal(root.left);
        System.out.println(root.data);
        inOrderTraversal(root.right);
    }

Preorder Traversal

void preOrderTraversal(Node root){
        if(root == null){
            return;
        }

        System.out.println(root.data);
        preOrderTraversal(root.left);
        preOrderTraversal(root.right);
    }

Post order traversal

void postOrderTraversal(Node root){
        if(root == null){
            return;
        }

        postOrderTraversal(root.left);
        postOrderTraversal(root.right);
        System.out.println(root.data);
    }

Leave a Reply

Your email address will not be published. Required fields are marked *