Question

In: Computer Science

(Java) Implement a RightThreadTree class as an extension of a BinarySearchTree A right-threaded tree is a...

(Java) Implement a RightThreadTree class as an extension of a BinarySearchTree

A right-threaded tree is a binary search tree in which each right link that would normally be null is a "thread" that links that node to its inorder successor. The thread enables nonrecursive algorithms to be written for search and inorder traversals that are more efficient than recursive ones. Implement a RightThreadTree class as an extension of a BinarySearchTree. You will also need an RTNode that extends the Node class to include a flag that indicates whether a node's right link is a real link or a thread.

Solutions

Expert Solution

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

Related Solutions

Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class...
Please in C++ I don't have the binarySearchTree (Carrano) Implement the code of the BinarySeachTree Class to solve: (Gaddis) Programming Challenger 3. Car Class p. 802 Ch. 13 • Implement a menu of options • Add a motor vehicle to the tree • Remove a motor vehicle to the tree • Search by vehicle model • Vehicle Inventory Updates • Take one of the tours to verify the contents of the tree. Car Class Write a class named Car that...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand...
in JAVA: implement a class called tree (from scratch) please be simple so i can understand thanks! tree(root) node(value, leftchild,rightchild) method: insert(value)
Implement a Binary tree using an array using class.
Implement a Binary tree using an array using class.
How do you implement an AVL tree for strings in Java?
How do you implement an AVL tree for strings in Java?
Develop a Java application to implement a binary tree data structure. A tree data structure starts...
Develop a Java application to implement a binary tree data structure. A tree data structure starts from the top node, called the root. Each node in the tree has a set of children, which are also nodes of the tree and can have children of their own, and so on. This keeps on going till we get to the bottom of the tree, to the “leaf” nodes. Each node in the tree, except for the root, has a parent. A...
In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a...
In C++, Implement the heapafication operation. Do not re-implement the binary tree class. Simply create a funcntion that takes in a binary tree and heapafies it. Meaning it takes in a pointer to a binary tree and converts it into a heap. (You may choose max or min heap).
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
Implement a function to find a node in a binary search tree. Using the following class...
Implement a function to find a node in a binary search tree. Using the following class and function definition. If a node with a matching value is found, return a pointer to it. If no match is found, return nullptr. #include <iostream> class BTNode { public: int item; BTNode *left; BTNode *right; BTNode(int i, BTNode *l=nullptr, BTNode *r=nullptr):item(i),left(l),right(r){} }; BTNode *root = nullptr; BTNode *find(int item) { //implement code here return nullptr; } int main() {    root = new...
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of       ...
JAVA * create Q3 class to implement comparator for a TreeSet. The purpose of        * the comparator is to compare the alphabetic order of integers. With Q3,        * the order of Integer elements will be sort according to the order of        * digit Unicode.        * For example, the value of {11,3,31,2} will return {11,2,3,31}        * NOTE: You don't need to compare each digit one by one. Just compare them System.out.println("Q3...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT