Question
Create a UML Diagram for the following Code import java.util.Scanner; import java.util.Random; class treapNode { treapNode left, right; int priority, element; public treapNode() { this.element
Create a UML Diagram for the following Code
import java.util.Scanner;
import java.util.Random;
class treapNode
{
treapNode left, right;
int priority, element;
public treapNode()
{
this.element = 0;
this.left = this;
this.right = this;
this.priority = Integer.MAX_VALUE;
}
public treapNode(int ele)
{
this(ele, null, null);
}
public treapNode(int ele, treapNode left, treapNode right)
{
this.element = ele;
this.left = left;
this.right = right;
this.priority = new Random().nextInt( );
}
}
class treap
{
private treapNode root;
private static treapNode nil = new treapNode();
public treap()
{
root = nil;
}
// Functions to insert elements of array in tree
public void add(int X)
{
root = add(X, root);
}
private treapNode add(int X, treapNode T)
{
if (T == nil)
return new treapNode(X, nil, nil);
else if (X < T.element)
{
T.left = add(X, T.left);
if (T.left.priority < T.priority)
{
treapNode L = T.left;
T.left = L.right;
L.right = T;
return L;
}
}
else if (X > T.element)
{
T.right = add(X, T.right);
if (T.right.priority < T.priority)
{
treapNode R = T.right;
T.right = R.left;
R.left = T;
return R;
}
}
return T;
}
// Functions to search for an element in trip
public boolean search_element(int val)
{
return search_element(root, val);
}
private boolean search_element(treapNode r, int val)
{
boolean found = false;
while ((r != nil) && !found)
{
int rval = r.element;
if (val < rval)
r = r.left;
else if (val > rval)
r = r.right;
else
{
found = true;
break;
}
found = search_element(r, val);
}
return found;
}
public void inorder()
{
inorder(root);
}
private void inorder(treapNode r) // Inorder traversal java function
{
if (r != nil)
{
inorder(r.left);
System.out.print(r.element +" ");
inorder(r.right);
}
}
// Function for preorder traversal
public void preorder()
{
preorder(root);
}
private void preorder(treapNode r) // Preorder traversal java function
{
if (r != nil)
{
System.out.print(r.element +" ");
preorder(r.left);
preorder(r.right);
}
}
public void postorder() // Post order traversal java function
{
postorder(root);
}
private void postorder(treapNode r)
{
if (r != nil)
{
postorder(r.left);
postorder(r.right);
System.out.print(r.element +" ");
}
}
}
class Main
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
// Creating object of treap class
treap tr = new treap();
int[] arr = new int[]{ 2,4,6,8,10,12,14,16,18}; //Sorted Array
for(int i=0; i { tr.add(arr[i]); // Adding sorted array elements to treap } System.out.print(" Postorder traversal of treap is: "); tr.postorder(); System.out.print(" Preorder traversal of treap is: "); tr.preorder(); System.out.print(" Inorder traversal of treap is: "); tr.inorder(); System.out.println(" Enter the element you want to search: "); System.out.println("Search result : "+ tr.search_element( scan.nextInt())); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started