A tree is a hierarchical data structure consisting of nodes connected by edges. It has the following properties:
Practical use cases of tree data structures:
Important Concepts in Tree Data Structure
Understanding different tree traversal methods is crucial:
import java.util.LinkedList;import java.util.Queue;class TreeNode { int val; TreeNode left; TreeNode right; public TreeNode(int val) { this.val = val; this.left = null; this.right = null; }}public class BinaryTree { TreeNode root; public BinaryTree() { root = null; } public void setRoot(int val) { root = new TreeNode(val); } public void addNode(int parentVal, int childVal, String position) { TreeNode parent = findNode(root, parentVal); if (parent != null) { if (position.equalsIgnoreCase("left")) { if (parent.left == null) { parent.left = new TreeNode(childVal); } else if (parent.left.val == childVal) { // Handle duplicate: add as left child of the existing node TreeNode newNode = new TreeNode(childVal); newNode.left = parent.left; parent.left = newNode; } else { // Recursively add to the left subtree addNode(parent.left.val, childVal, "left"); } } else if (position.equalsIgnoreCase("right")) { if (parent.right == null) { parent.right = new TreeNode(childVal); } else if (parent.right.val == childVal) { // Handle duplicate: add as left child of the existing node TreeNode newNode = new TreeNode(childVal); newNode.left = parent.right; parent.right = newNode; } else { // Recursively add to the right subtree addNode(parent.right.val, childVal, "right"); } } } else { System.out.println("Parent node not found"); } } private TreeNode findNode(TreeNode current, int val) { if (current == null || current.val == val) { return current; } TreeNode left = findNode(current.left, val); if (left != null) { return left; } return findNode(current.right, val); } public void levelOrderTraversal() { if (root == null) return; Queue<TreeNode> queue = new LinkedList<>(); queue.add(root); while (!queue.isEmpty()) { TreeNode current = queue.poll(); System.out.print(current.val + " "); if (current.left != null) queue.add(current.left); if (current.right != null) queue.add(current.right); } } public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.setRoot(1); tree.addNode(1, 2, "left"); tree.addNode(1, 3, "right"); tree.addNode(2, 4, "left"); tree.addNode(2, 5, "right"); tree.addNode(3, 6, "left"); tree.addNode(3, 7, "right"); // Adding a duplicate value tree.addNode(2, 5, "right"); System.out.println("Level Order Traversal:"); tree.levelOrderTraversal(); }}Level Order Traversal:
1 2 3 4 5 6 7 5 This implementation provides the following features:
setRoot(int val): Sets the root of the tree.addLeft(int parentVal, int childVal): Adds a left child to a node with the given parent value.addRight(int parentVal, int childVal): Adds a right child to a node with the given parent value.findNode(TreeNode current, int val): A helper method to find a node with a given value.levelOrderTraversal(): Performs a level order traversal of the tree.xxxxxxxxxximport java.util.LinkedList;import java.util.Queue;// Tree Node classclass TreeNode { int data; TreeNode left, right; public TreeNode(int item) { data = item; left = right = null; }}// Binary Tree classpublic class BinaryTree { TreeNode root; // Function to print level order traversal of tree void printLevelOrder() { Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.add(root); while (!queue.isEmpty()) { // Poll removes the present head. TreeNode tempNode = queue.poll(); System.out.print(tempNode.data + " "); // Enqueue left child if (tempNode.left != null) { queue.add(tempNode.left); } // Enqueue right child if (tempNode.right != null) { queue.add(tempNode.right); } } } public static void main(String args[]) { BinaryTree tree = new BinaryTree(); // Creating a sample tree: // 1 // / \ // 2 3 // / \ / \ // 4 5 6 7 tree.root = new TreeNode(1); tree.root.left = new TreeNode(2); tree.root.right = new TreeNode(3); tree.root.left.left = new TreeNode(4); tree.root.left.right = new TreeNode(5); tree.root.right.left = new TreeNode(6); tree.root.right.right = new TreeNode(7); System.out.println("Level order traversal of binary tree is - "); tree.printLevelOrder(); }}Level order traversal of binary tree is -
1 2 3 4 5 6 7