// If you use TreeSet the data will be inserted in natural sorting order and duplicates are not permitted
Code for inserted data in natural sorting order
package com.awf.org;
import java.util.Set;
import java.util.TreeSet;
public class IntegerAdditionExample {
public static void main (String args[]){
Set<Integer> addSet=new TreeSet<Integer>();
addSet.add(10);
addSet.add(0);
addSet.add(15);
addSet.add(20);
addSet.add(20);
System.out.println("TreeSet contents ::"+addSet);
}
}
In the above output you can see that the data is displayed in ascending order .
======================================================================
If you want to display insertion data in Customized sorting (descending order) you can go for Comparator.
Code for inserted data in Customized sorting order using comparator
package com.awf.org;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class IntegerAdditionExample {
public static void main (String args[]){
Set<Integer> addSet=new TreeSet<Integer>(new MyComparator());
addSet.add(10);
addSet.add(0);
addSet.add(15);
addSet.add(20);
addSet.add(20);
System.out.println("TreeSet contents ::"+addSet);
}
}
class MyComparator implements Comparator<Integer>{
@Override
public int compare(Integer obj1, Integer obj2) {
if(obj1<obj2){
return 1;
}else if (obj1>obj2){
return -1;
}else{
return 0;
}
}
}
It wont compare anything directly it will insert data 10
The data 0 (obj1) is compared with 10 (obj2)
0<10 retrun +1 . It will traverse right side from the root
[10,0]
Now Obj1=15 Obj2=10
Always it will begins with root element (already root element is 10)
1) 15 > 10 it will return -1 . so 15 will be inserted in the left side of the root
now your output is like [15,10,0]
obj1=20 ,obj2=10
First it will compared with root
20 >10 it will come left side, which has already element 15 so 20 is compared with 15
20 >15 so again it will come left side and inserted after 15.
now your output is like [20,15,10,0]
First it will compared with root
20 >10 it will come left side, which has already element 15 so 20 is compared with 15
20 >15 so again left side we have element 20
20 is compared with 20
20==20 so it will return 0 means it wont insert data in tree because duplication is not allowed
Now your output is like [20,15,10,0]
Finally Tree traversal in inorder [left ,root,right]
so finally you will get data in descending order for the above code.
[20,15,10,0]
Code for inserted data in natural sorting order
package com.awf.org;
import java.util.Set;
import java.util.TreeSet;
public class IntegerAdditionExample {
public static void main (String args[]){
Set<Integer> addSet=new TreeSet<Integer>();
addSet.add(10);
addSet.add(0);
addSet.add(15);
addSet.add(20);
addSet.add(20);
System.out.println("TreeSet contents ::"+addSet);
}
}
Output ::
TreeSet contents ::[0, 10, 15, 20]
In the above output you can see that the data is displayed in ascending order .
======================================================================
If you want to display insertion data in Customized sorting (descending order) you can go for Comparator.
Code for inserted data in Customized sorting order using comparator
package com.awf.org;
import java.util.Comparator;
import java.util.Set;
import java.util.TreeSet;
public class IntegerAdditionExample {
public static void main (String args[]){
Set<Integer> addSet=new TreeSet<Integer>(new MyComparator());
addSet.add(10);
addSet.add(0);
addSet.add(15);
addSet.add(20);
addSet.add(20);
System.out.println("TreeSet contents ::"+addSet);
}
}
class MyComparator implements Comparator<Integer>{
@Override
public int compare(Integer obj1, Integer obj2) {
if(obj1<obj2){
return 1;
}else if (obj1>obj2){
return -1;
}else{
return 0;
}
}
}
Output ::
TreeSet contents ::[20,15,10,0]
The Input data order : [10,0,15,20,20]
In Comparator we used the below code
if(obj1<obj2){
return 1; //Insert data in right side of the tree
}else if (obj1>obj2){
return -1; // Insert data in the left side of the tree
}else{
return 0;
}
return 1; //Insert data in right side of the tree
}else if (obj1>obj2){
return -1; // Insert data in the left side of the tree
}else{
return 0;
}
First Iteration :[10,0,15,20,20]
For Tree I used Green for root ,blue for left element and pink for right element
It wont compare anything directly it will insert data 10
Second Iteration :[10,0,15,20,20]
The data 0 (obj1) is compared with 10 (obj2)
[10,0]
Third Iteration :[10,0,15,20,20]
Now Obj1=15 Obj2=10
Always it will begins with root element (already root element is 10)
1) 15 > 10 it will return -1 . so 15 will be inserted in the left side of the root
now your output is like [15,10,0]
Fourth Iteration :[10,0,15,20,20]
obj1=20 ,obj2=10
First it will compared with root
20 >10 it will come left side, which has already element 15 so 20 is compared with 15
20 >15 so again it will come left side and inserted after 15.
now your output is like [20,15,10,0]
Fifth Iteration :[10,0,15,20,20]
obj1=20 ,obj2=10First it will compared with root
20 >10 it will come left side, which has already element 15 so 20 is compared with 15
20 >15 so again left side we have element 20
20 is compared with 20
20==20 so it will return 0 means it wont insert data in tree because duplication is not allowed
Now your output is like [20,15,10,0]
Finally Tree traversal in inorder [left ,root,right]
so finally you will get data in descending order for the above code.
[20,15,10,0]
Regards,
R.Tamilanban M.E CSE
AWF community
Good Analytical skills
ReplyDeleteThanks Tirumala
ReplyDeleteGood concept
ReplyDelete