Binary Tree Java

A binary tree is a tree in which no node can have more than two children. Each node has a parent node and two children. Every binary tree has a root node. The topmost node is called the root node.

Every node contains zero child node, one child, two children but not more than two children.

Let’s see the visual representation of the Binary tree.


 



Types of Binary Trees

  • Strict Binary Tree: Strict Binary Tree each node has contained exactly two children or zero children.
  • Full Binary Tree: Full Binary Tree each node has contained exactly two children and all leaf nodes are at the same level.
  • Complete Binary Tree: Complete Binary Tree all levels are filled up except the last level and the nodes at the last levels are as far left as possible, nodes must always fill the next level from left to right.

 

 Binary Tree


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
public class BinaryTree {

	private TreeNode root;               // instance variable
        // inner class start
	private class TreeNode {             // inner class
		private TreeNode left;
		private TreeNode right;
		private int data;

		public TreeNode(int data) { // inner class Constructor
			this.data = data;
		}
	}
	// inner class end

	public void preOrder(TreeNode root) { // preOrder Tree Traversal
		if (root == null) {
			return;
		}
		System.out.print(root.data + " ");
		preOrder(root.left);
		preOrder(root.right);
	}

	public void createTree() {             // create tree method
		TreeNode first = new TreeNode(12);
		TreeNode second = new TreeNode(8);
		TreeNode third = new TreeNode(9);
		TreeNode fourth = new TreeNode(4);
		TreeNode fifth = new TreeNode(3);

		root = first;
		first.left = second;
		first.right = third;
		second.left = fourth;
		second.right = fifth;
	}

	public static void main(String[] args) {          // main method
		BinaryTree binary = new BinaryTree();     // crate tree object
		binary.createTree();                      // called createTree() method
		binary.preOrder(binary.root);             // called preOrder method passing root

	}

}


Output

12 8 4 3 9 

 

In the above program BinaryTree class is created that contains one instance variable type TreeNode name root that root TreeNode holds all the nodes of a Binary Tree. which contains another inner class name TreeNode. TreeNode inner class contains three private variables. There are Two variables TreeNode type left, right, and the third one is an integer type name data. The inner class also contains one Constructor. There are two methods in BinaryTree class createTree and preOrder. Preorder Traversal first Visit the root then left subtree and right subtree. 

Next Post Previous Post
4 Comments
  • Anonymous
    Anonymous 08 March, 2022

    this article is helpful

    • ProCodePlan
      ProCodePlan 09 March, 2022

      @Anonymous
      thanks

  • Anonymous
    Anonymous 08 March, 2022

    utile

    • ProCodePlan
      ProCodePlan 09 March, 2022

      @Anonymous
      grazie per averti

Add Comment
comment url