The basic concept of binary tree: binary tree is defined recursively, and its nodes are divided into left and right subtrees. Logically, binary tree consists of five forms: 1) empty binary tree 2) binary tree with only one root node 3) left subtree 4) right subtree 5) complete binary tree
Creation of Binary Tree
private static class Node{ private char val; private Node left; private Node right; private Node(char val) { this.val=val; } } public static Node buildTree(){ Node a=new Node('A'); Node b=new Node('B'); Node c=new Node('C'); Node d=new Node('D'); Node e=new Node('E'); Node f=new Node('F'); Node g=new Node('G'); Node h=new Node('H'); a.left=b; a.right=c; b.left=d;b.right=e; c.left=f;c.right=g; e.right=h; return a; }
Front, middle and post-order traversal of binary trees
Preorder traversal: If the binary tree is empty, return null; otherwise
1) Access to the root node
2) Preorder traversal of left subtrees
2) Preorder traversal of right subtrees
Intermediate traversal: If the binary tree is empty, it returns null; otherwise, it returns null.
1) Mid-order traversal of left subtrees
2) Access to the root node
3) Mid-order traversal of right subtrees
Post-order traversal: If the binary tree is empty, it returns null; otherwise, it returns null.
1) Post-order traversal of left subtrees
2) Follow-up traversal of right subtree
3) Access to the root node
public static void preOrderTraversal(Node root) { //Line traversal //Preorder traversal of root + left subtree + right subtree if(root==null) { return ; } System.out.println(root); preOrderTraversal(root.left); preOrderTraversal(root.right); } public static void inOrderTraversal(Node root) { //Sequential traversal //Mid-order traversal of left subtree + root + right subtree if(root==null) { return ; } inOrderTraversal(root.left); System.out.println(root); inOrderTraversal(root.right); } public static void postOrderTraversal(Node root) { //Follow-up traversal of binary trees if(root==null) { return; } postOrderTraversal(root.left); postOrderTraversal(root.right); System.out.println(root); }
Complete code:
public class BineryTree { private static class Node{ private char val; private Node left; private Node right; private Node(char val) { this.val=val; } } public static Node buildTree(){ Node a=new Node('A'); Node b=new Node('B'); Node c=new Node('C'); Node d=new Node('D'); Node e=new Node('E'); Node f=new Node('F'); Node g=new Node('G'); Node h=new Node('H'); a.left=b; a.right=c; b.left=d;b.right=e; c.left=f;c.right=g; e.right=h; return a; } public static void preOrderTraversal(Node root) { //Line traversal //Preorder traversal of root + left subtree + right subtree if(root==null) { return ; } System.out.println(root); preOrderTraversal(root.left); preOrderTraversal(root.right); } public static void inOrderTraversal(Node root) { //Sequential traversal //Mid-order traversal of left subtree + root + right subtree if(root==null) { return ; } inOrderTraversal(root.left); System.out.println(root); inOrderTraversal(root.right); } public static void postOrderTraversal(Node root) { //Follow-up traversal of binary trees if(root==null) { return; } postOrderTraversal(root.left); postOrderTraversal(root.right); System.out.println(root); public static void main(String[] args) { Node root=buildTree(); System.out.println("Succcess"); preOrderTraversal(root); System.out.println("===================="); inOrderTraversal(root); System.out.println("====================="); postOrderTraversal(root); System.out.println("====================="); }