0% found this document useful (0 votes)
61 views7 pages

Implementation of Leftist Heap: Priority Queue Binary Heap Leaf Heap

1. The document describes a Java program to implement a leftist heap with insert and delete operations. 2. A leftist heap is a type of binary heap where each node has an s-value representing distance to the nearest leaf, and attempts to be unbalanced by having the right descendant of each node have a lower s-value. 3. The program uses classes like LeftistTree and Node to represent the heap, and functions like insert(), remove(), display() to perform operations on the heap in O(log n) time by merging two trees.

Uploaded by

Arockiaruby Ruby
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views7 pages

Implementation of Leftist Heap: Priority Queue Binary Heap Leaf Heap

1. The document describes a Java program to implement a leftist heap with insert and delete operations. 2. A leftist heap is a type of binary heap where each node has an s-value representing distance to the nearest leaf, and attempts to be unbalanced by having the right descendant of each node have a lower s-value. 3. The program uses classes like LeftistTree and Node to represent the heap, and functions like insert(), remove(), display() to perform operations on the heap in O(log n) time by merging two trees.

Uploaded by

Arockiaruby Ruby
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 7

Ex No:5 14.11.

11

IMPLEMENTATION OF LEFTIST HEAP

AIM:
To write a java program to implement the leftist heap with insert and delete operations.

PROBLEM DESCRIPTION:
A leftist tree or leftist heap is a priority queue implemented with a variant of a binary heap. Every node has an s-value which is the distance to the nearest leaf. In contrast to a binary heap, a leftist tree attempts to be very unbalanced. In addition to the heap property, leftist trees are maintained so the right descendant of each node has the lower s-value. hen inserting a new node into a tree, a new one-node tree is created and merged into the e!isting tree. To delete a minimum item, we remove the root and the left and right sub-trees are then merged. "oth these operations ta#e $ %log n& time

ALGORITHM:
1. 'tart the program by defining functions. 2. (eep the minimum pointer to the root element. 3. The insert and delete operations are performed with the help of combining two trees. 4. The insert operations are performed by combining the two leftist trees. 5. The delete)in%& is used to delete the minimum element in the heap. 6. After the insert and delete operations leftist heap elements are displayed. 7. 'top the program.

CLASS DIAGRAM :

LEFTIST TREE node root int newelt int ch int cont

void insert%& void remove%& void display%&

PROGRAM
import java.io.*+ class node , public int data+ public node -.,/.+ public int npl+ 0 class minleftist , node root 1 null+ public void insert%& , int newelt12+ try , 'ystem.out.println%3Enter the element43&+ 5ataInput'tream din1new 5ataInput'tream%'ystem.in&+ newelt1Integer.parseInt%din.read-ine%&&+ 0 catch%E!ception e&,0 node temp 1 new node%&+ temp.data1newelt+ temp.-.1temp./.1null+

temp.npl16+ if%root11null& root1temp+ else root1meld%root,temp&+ 0 public node meld%node a, node b& , if%a.data 7 b.data& , node t+ t1a+ a1b+ b1t+ 0 if%a./.11null& a./.1b+ else a./.1meld%a./.,b&+ if%%a.-.11null& 88 %a.-..npl 9 a./..npl&& , node t1new node%&+ t1a.-.+ a.-.1a./.+ a./.1t+ 0 if%a./.11null& a.npl16+ else a.npl1a./..npl:6+ return a+ 0 public void remove%& , 'ystem.out.println%35eleted element is 3:root.data:3;n3&+ root1meld%root.-.,root./.&+ 0 public void display%& , if%root11null& 'ystem.out.println%3E)<T=3&+ else , 'ystem.out.println%3;nIn $rder3&+ dispin%root&+ 0

0 public void dispin%node currentnode& , if%currentnode>1null& , dispin%currentnode.-.&+ 'ystem.out.println%currentnode.data:3 3:3npl is 3:currentnode.npl&+ dispin%currentnode./.&+ 0 0 0+ class -eftistTree , public static void main%'tring args? @&throws I$E!ception , int ch12,cont12+ minleftist m 1 new minleftist%&+ do , 'ystem.out.println%3-EATI'T T/EE 6. Insert B. 5elete3&+ 5ataInput'tream din 1 new 5ataInput'tream%'ystem.in&+ try , ch1Integer.parseInt%din.read-ine%&&+ 0 catch%E!ception e&,0 if%ch116& , m.insert%&+ m.display%&+ 0 else if%ch11B& , m.remove%&+ m.display%&+ 0 else , 'ystem.out.println%3Enter the correct choice3&+ 0 'ystem.out.println%3press 6 to continue43&+ try , cont1Integer.parseInt%din.read-ine%&&+ 0

catch%E!ception e&,0 0while%cont116&+ 0 0

OUTPUT:

CONCLUSION Thus the java program to implement -eftist heap has been e!ecuted and output is verified successfully.

You might also like