Site icon

Print ancestors of node in a binary tree (java/ recursive/ examples)

Fig 1: Ancestors of node in binary tree

Given input node and its corresponding ancestor nodes are shown in a table.

S. No.Input Node Ancestors
1B A (60 )
2D or E A B (60, 50)
3C A (60)
4F or G A C (60, 90)

Example 1:  Print ancestor of node E (80) in a binary tree using java

We will use pre order traversal to traverse the binary tree.

Fig 2: Ancestor of Node E in a binary tree

Example 2:  Print ancestor of Node G (45) in a binary tree using java

We will analyze the binary tree shown in Fig 3. We will search the node in a binary tree and will print all nodes which we have visited during traversal. The algorithm is quite similar to example 1 algorithm.

Fig 3: Ancestor of Node G in a binary tree

Time complexity of algorithm is O(n).

Program – print ancestor of given node in binary tree (java/ recursive)

1.) PrintAncestorOfNode Class:

  
package org.learn.Question;

public class PrintAncestorOfNode {
	public static boolean printAncestorOfNode(Node root, int data) {
		if(null == root) {
			return false;
		}
		//we found the node
		if(root.data == data) {
			return true;
		}
		boolean bFoundOnLeft = printAncestorOfNode(root.left,data);
		if(bFoundOnLeft) {
			System.out.printf("%d ",root.data);
			return bFoundOnLeft;
		}
		boolean bFoundOnRight = printAncestorOfNode(root.right,data);
		if(bFoundOnRight) {
			System.out.printf("%d ",root.data);
			return bFoundOnRight;
		}
		return false;
	}	
}

2.) Node Class:

package org.learn.Question;

public class Node {
	public int data;
	public Node left;
	public Node right;

	public Node(int num) {
		this.data = num;
		this.left = null;
		this.right = null;
	}

	public Node() {
		this.left = null;
		this.right = null;
	}
	
	public static Node createNode(int number) {
		return new Node(number);
	}
}

3.) App Class: 

package org.learn.Client;

import org.learn.Question.Node;
import org.learn.Question.PrintAncestorOfNode;

public class App {
	public static void main(String[] args) {
		// root level 0
		Node A = Node.createNode(60);
		// Level 1
		Node B = Node.createNode(50);
		Node C = Node.createNode(90);
		// Level 2
		Node D = Node.createNode(25);
		Node E = Node.createNode(80);
		Node F = Node.createNode(75);
		Node G = Node.createNode(45);

		// connect Level 0 and 1
		A.left = B;
		A.right = C;
		// connect level 1 and level 2
		B.left = D;
		B.right = E;
		C.left = F;
		C.right = G;

		int iNode = 50;
		System.out.printf("Ancestor of Node %d is : \n", iNode);
		PrintAncestorOfNode.printAncestorOfNode(A, iNode);

		iNode = 45;
		System.out.printf("\nAncestor of Node %d is : \n", iNode);
		PrintAncestorOfNode.printAncestorOfNode(A, iNode);

		iNode = 25;
		System.out.printf("\nAncestor of Node %d is : \n", iNode);
		PrintAncestorOfNode.printAncestorOfNode(A, iNode);
	}
}

Output – ancestor of given node using recursive algorithm in java

Ancestor of Node 50 is : 
60 
Ancestor of Node 45 is : 
90 60 
Ancestor of Node 25 is : 
50 60 

Download code – print ancestor(s) of node binary tree recursive java

 

Exit mobile version