Some basic terms used in tree data structure are:
There are two main ways to implement trees: using an array and using linked lists.
class BinaryTree {
int[] tree;
int size;
public BinaryTree(int size) {
this.size = size;
this.tree = new int[size + 1]; // add 1 for 1-indexing
}
// insert a new node into the tree
public void insert(int value) {
if (size == tree.length - 1) {
System.out.println("Tree is full.");
return;
}
size++;
tree[size] = value;
}
// get the value of the parent node of a given index
public int parent(int index) {
return tree[index / 2];
}
// get the value of the left child node of a given index
public int leftChild(int index) {
return tree[2 * index];
}
// get the value of the right child node of a given index
public int rightChild(int index) {
return tree[2 * index + 1];
}
}
Time Complexity:O(1)
Space Complexity: O(n)
public class TreeNode {
int data;
TreeNode leftChild;
TreeNode rightChild;
public TreeNode(int data) {
this.data = data;
this.leftChild = null;
this.rightChild = null;
}
}
public class BinaryTree {
TreeNode root;
public BinaryTree() {
root = null;
}
public void insert(int data) {
TreeNode newNode = new TreeNode(data);
if (root == null) {
root = newNode;
} else {
TreeNode current = root;
TreeNode parent;
while (true) {
parent = current;
if (data < current.data) {
current = current.leftChild;
if (current == null) {
parent.leftChild = newNode;
return;
}
} else {
current = current.rightChild;
if (current == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
public void printInorderTraversal(TreeNode node) {
if (node != null) {
printInorderTraversal(node.leftChild);
System.out.print(node.data + " ");
printInorderTraversal(node.rightChild);
}
}
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
binaryTree.insert(50);
binaryTree.insert(30);
binaryTree.insert(70);
binaryTree.insert(20);
binaryTree.insert(40);
binaryTree.insert(60);
binaryTree.insert(80);
System.out.print("Inorder Traversal: ");
binaryTree.printInorderTraversal(binaryTree.root);
}
}
Time Complexity: O(log n) to O(n) for insertion and O(n) for inorder traversal
Space Complexity: O(n)
In tree data structure, there are two main traversal techniques:
There are three types of depth-first traversal:
public void inorderTraversal(Node node) {
if (node == null) {
return;
}
inorderTraversal(node.left);
System.out.print(node.data + " ");
inorderTraversal(node.right);
}
public void printPreorder(Node node) {
if (node == null)
return;
System.out.print(node.data + " ");
printPreorder(node.left);
printPreorder(node.right);
}
void postorder(Node node) {
if (node == null)
return;
// Traverse left subtree
postorder(node.left);
// Traverse right subtree
postorder(node.right);
// Visit root node
System.out.print(node.data + " ");
}
import java.util.LinkedList;
import java.util.Queue;
class Node {
int value;
Node left, right;
Node(int value) {
this.value = value;
this.left = this.right = null;
}
}
public class BFS {
Node root;
BFS() {
root = null;
}
void printLevelOrder() {
if (root == null)
return;
Queue<Node> queue = new LinkedList<Node>();
queue.add(root);
while (!queue.isEmpty()) {
Node node = queue.poll();
System.out.print(node.value + " ");
if (node.left != null)
queue.add(node.left);
if (node.right != null)
queue.add(node.right);
}
}
public static void main(String[] args) {
BFS tree = new BFS();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
System.out.println("BFS Traversal of Binary Tree:");
tree.printLevelOrder();
}
}
Time Complexity: O(n)
Space Complexity: O(n)
Note: also read about Problems based on Linked List
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…