How would you store a Binary Tree?

A Binary tree can be stored in 2 possible ways.
1. Nodes and References : You can create a Node class with node reference variables for the left and right child. Any or both can be null as and when required.

class Node{
	int value;
	Node left;
	Node right;

2. Array : You can store binary tree in a breadth first order using an array. The logic for storing data will be as follows. If the node is on index n then the left and right child will be 2n+1 and 2n+2 respectively.

core java 12


Explore Tutu'rself