Java Program to Implement Multi-Threaded Version of Binary Search Tree

«
»
This is a Java Program to implement Threaded Binary Tree. A threaded binary tree makes it possible to traverse the values in the binary tree via a linear traversal that is more rapid than a recursive in-order traversal. It is also possible to discover the parent of a node from a threaded binary tree, without explicit use of parent pointers or a stack, albeit slowly. This can be useful where stack space is limited, or where a stack of parent pointers is unavailable (for finding the parent pointer via DFS).

Here is the source code of the Java Program to Implement Multi-Threaded Version of Binary Search Tree. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. //This is a java program to search an element in a multi-threaded version of binary search tree
  2. import java.util.Scanner;
  3.  
  4. class TBSTNode
  5. {
  6.     int ele;
  7.     TBSTNode left, right;
  8.     boolean leftThread, rightThread;
  9.  
  10.     public TBSTNode(int ele)
  11.     {
  12.         this(ele, null, null, true, true);
  13.     }
  14.  
  15.     public TBSTNode(boolean leftThread, boolean rightThread)
  16.     {
  17.         this.ele = Integer.MAX_VALUE;
  18.         this.left = this;
  19.         this.right = this;
  20.         this.leftThread = leftThread;
  21.         this.rightThread = rightThread;
  22.     }
  23.  
  24.     public TBSTNode(int ele, TBSTNode left, TBSTNode right, boolean leftThread, boolean rightThread)
  25.     {
  26.         this.ele = ele;
  27.         this.left = left;
  28.         this.right = right;
  29.         this.leftThread = leftThread;
  30.         this.rightThread = rightThread;
  31.     }
  32. }
  33.  
  34. class ThreadedBinarySearchTree
  35. {
  36.     private TBSTNode root;
  37.     public ThreadedBinarySearchTree () 
  38.     {
  39.         root = new TBSTNode(true, false);      
  40.     }
  41.  
  42.     public void clear()
  43.     {
  44.         root = new TBSTNode(true, false);  
  45.     }
  46.  
  47.     public void insert(int ele) 
  48.     {
  49.         TBSTNode ptr = findNode(root, ele);
  50.  
  51.         if (ptr == null)
  52.             return;         
  53.  
  54.         if (ptr.ele < ele) 
  55.         { 
  56.             TBSTNode nptr = new TBSTNode(ele, ptr, ptr.right, true, true);            
  57.             ptr.right = nptr;
  58.             ptr.rightThread = false;
  59.         }
  60.         else
  61.         {
  62.             TBSTNode nptr = new TBSTNode(ele, ptr.left, ptr, true, true);         
  63.             ptr.left = nptr;
  64.             ptr.leftThread = false;
  65.         }
  66.     }
  67.  
  68.     public TBSTNode findNode(TBSTNode r, int ele)
  69.     {
  70.         if (r.ele < ele)
  71.         {
  72.             if (r.rightThread)
  73.                 return r;
  74.             return findNode(r.right, ele);
  75.         }
  76.         else if (r.ele > ele)
  77.         {
  78.             if (r.leftThread)
  79.                 return r;
  80.             return findNode(r.left, ele);
  81.         }
  82.         else
  83.             return null;        
  84.     }
  85.  
  86.     public boolean search(int ele) 
  87.     {
  88.         return findNode(root, ele) == null;
  89.     }
  90.  
  91.     public void inOrder() 
  92.     {
  93.         TBSTNode temp = root;
  94.         for (;;)
  95.         {
  96.             temp = insucc(temp);
  97.             if (temp == root)
  98.                 break;
  99.             System.out.print(temp.ele +" ");
  100.         }
  101.     } 
  102.  
  103.     public TBSTNode insucc(TBSTNode tree)
  104.     {
  105.         TBSTNode temp;
  106.         temp = tree.right;
  107.         if (!tree.rightThread)
  108.             while (!temp.leftThread)
  109.                 temp = temp.left;
  110.         return temp;
  111.     }
  112. }
  113.  
  114. public class Threaded_BST
  115. {
  116.     public static void main(String[] args)
  117.     {                 
  118.         Scanner scan = new Scanner(System.in);
  119.  
  120.         ThreadedBinarySearchTree tbst = new ThreadedBinarySearchTree(); 
  121.         System.out.println("Multi-threaded Binary Search Tree \n");          
  122.         char ch;
  123.  
  124.         do    
  125.         {
  126.             System.out.println("\nThreaded Binary Search Tree Operations\n");
  127.             System.out.println("1. insert ");
  128.             System.out.println("2. search");
  129.             System.out.println("3. clear"); 
  130.  
  131.             int choice = scan.nextInt();            
  132.             switch (choice)
  133.             {
  134.             case 1 : 
  135.                 System.out.println("Enter integer element to insert");
  136.                 tbst.insert( scan.nextInt() );                     
  137.                 break;                          
  138.             case 2 : 
  139.                 System.out.println("Enter integer element to search");
  140.                 System.out.println("Search result : "+ tbst.search( scan.nextInt() ));
  141.                 break;       
  142.             case 3 : 
  143.                 System.out.println("\nTree Cleared\n");
  144.                 tbst.clear();
  145.                 break;           
  146.             default : 
  147.                 System.out.println("Wrong Entry \n ");
  148.                 break;   
  149.             }
  150.  
  151.             System.out.print("\nTree = ");
  152.             tbst.inOrder();            
  153.             System.out.println();
  154.  
  155.             System.out.println("\nDo you want to continue (Type y or n) \n");
  156.             ch = scan.next().charAt(0);                        
  157.         } while (ch == 'Y'|| ch == 'y');
  158.         scan.close();
  159.     }
  160. }

Output:

advertisement
$ javac Threaded_BST.java
$ java Threaded_BST
 
Multi-threaded Binary Search Tree 
 
Threaded Binary Search Tree Operations
 
1. insert 
2. search
3. clear
1
Enter integer element to insert
23
 
Tree = 23 
 
Do you want to continue (Type y or n) 
 
y
 
Threaded Binary Search Tree Operations
 
1. insert 
2. search
3. clear
1
Enter integer element to insert
867
 
Tree = 23 867 
 
Do you want to continue (Type y or n) 
 
y
 
Threaded Binary Search Tree Operations
 
1. insert 
2. search
3. clear
1
Enter integer element to insert
3
 
Tree = 3 23 867 
 
Do you want to continue (Type y or n) 
 
y
 
Threaded Binary Search Tree Operations
 
1. insert 
2. search
3. clear
2
Enter integer element to search
45
Search result : false
 
Tree = 3 23 867 
 
Do you want to continue (Type y or n)

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Reference Books in Java Programming, Data Structures and Algorithms.

Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!
advertisement
advertisement
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He is Linux Kernel Developer & SAN Architect and is passionate about competency developments in these areas. He lives in Bangalore and delivers focused training sessions to IT professionals in Linux Kernel, Linux Debugging, Linux Device Drivers, Linux Networking, Linux Storage, Advanced C Programming, SAN Storage Technologies, SCSI Internals & Storage Protocols such as iSCSI & Fiber Channel. Stay connected with him @ LinkedIn | Youtube | Instagram | Facebook | Twitter