ALGORITHM:
To print ancestors of a node in a BST , we just add a print function to the searching function of a BST before the recursion.
Steps in a search function of BST:
- if current node is the required node print the node
- if current node is greater than required node goto step 1 for left subtree
- if current node is lesser than required node goto step 1 for right subtree
CODE:
//utility class for node
public class node{
public int data;
public node left,right;
public node(){
data=0;
left=right=null;
}
}
public int data;
public node left,right;
public node(){
data=0;
left=right=null;
}
}
//class to implement the algorithm
import java.util.Scanner;
public class ancestorsNode {
static Scanner in =new Scanner(System.in);
static node inputTree(){
node temp=new node();
System.out.print("Enter the value: ");
temp.data=in.nextInt();
System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
if(in.next().charAt(0)=='y')
temp.left=inputTree();
else
temp.left=null;
System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
if(in.next().charAt(0)=='y')
temp.right=inputTree();
else
temp.right=null;
return temp;
}
static void inorder(node tree){
if(tree==null)
return;
inorder(tree.left);
System.out.print(tree.data+"\t");
inorder(tree.right);
}
public static void printAnc(node tree,int nod){
if(nod==tree.data){
System.out.println(nod);
return;
}
System.out.print(tree.data+"---");
if(nod<tree.data)
printAnc(tree.left,nod);
else
printAnc(tree.right,nod);
}
public static void main(String[] args) {
node tree=new node();
System.out.print("Enter y if tree is null: ");
if(in.next().charAt(0)=='y')
tree=null;
else
tree=inputTree(tree);
System.out.print("Inorder traversal of the tree is:\n");
inorder(tree);
System.out.print("\nEnter the node:");
int nd = in.nextInt();
printAnc(tree,nd);
in.close();
}
}
import java.util.Scanner;
public class ancestorsNode {
static Scanner in =new Scanner(System.in);
static node inputTree(){
node temp=new node();
System.out.print("Enter the value: ");
temp.data=in.nextInt();
System.out.print("Enter y if the node "+temp.data+" has left subtree: ");
if(in.next().charAt(0)=='y')
temp.left=inputTree();
else
temp.left=null;
System.out.print("Enter y if the node "+temp.data+" has right subtree: ");
if(in.next().charAt(0)=='y')
temp.right=inputTree();
else
temp.right=null;
return temp;
}
static void inorder(node tree){
if(tree==null)
return;
inorder(tree.left);
System.out.print(tree.data+"\t");
inorder(tree.right);
}
public static void printAnc(node tree,int nod){
if(nod==tree.data){
System.out.println(nod);
return;
}
System.out.print(tree.data+"---");
if(nod<tree.data)
printAnc(tree.left,nod);
else
printAnc(tree.right,nod);
}
public static void main(String[] args) {
node tree=new node();
System.out.print("Enter y if tree is null: ");
if(in.next().charAt(0)=='y')
tree=null;
else
tree=inputTree(tree);
System.out.print("Inorder traversal of the tree is:\n");
inorder(tree);
System.out.print("\nEnter the node:");
int nd = in.nextInt();
printAnc(tree,nd);
in.close();
}
}
OUTPUT:
Enter y if tree is null: n
Enter the value: 5
Enter y if the node 5 has left subtree: y
Enter the value: 3
Enter y if the node 3 has left subtree: y
Enter the value: 2
Enter y if the node 2 has left subtree: n
Enter y if the node 2 has right subtree: n
Enter y if the node 3 has right subtree: y
Enter the value: 4
Enter y if the node 4 has left subtree: n
Enter y if the node 4 has right subtree: n
Enter y if the node 5 has right subtree: y
Enter the value: 7
Enter y if the node 7 has left subtree: y
Enter the value: 6
Enter y if the node 6 has left subtree: n
Enter y if the node 6 has right subtree: n
Enter y if the node 7 has right subtree: y
Enter the value: 8
Enter y if the node 8 has left subtree: n
Enter y if the node 8 has right subtree: n
Inorder traversal of the tree is:
2 3 4 5 6 7 8
Enter the value: 5
Enter y if the node 5 has left subtree: y
Enter the value: 3
Enter y if the node 3 has left subtree: y
Enter the value: 2
Enter y if the node 2 has left subtree: n
Enter y if the node 2 has right subtree: n
Enter y if the node 3 has right subtree: y
Enter the value: 4
Enter y if the node 4 has left subtree: n
Enter y if the node 4 has right subtree: n
Enter y if the node 5 has right subtree: y
Enter the value: 7
Enter y if the node 7 has left subtree: y
Enter the value: 6
Enter y if the node 6 has left subtree: n
Enter y if the node 6 has right subtree: n
Enter y if the node 7 has right subtree: y
Enter the value: 8
Enter y if the node 8 has left subtree: n
Enter y if the node 8 has right subtree: n
Inorder traversal of the tree is:
2 3 4 5 6 7 8
Enter the node:8
5---7---8
5---7---8
0 comments :
Post a Comment