Implementing Pagoda in Java
In Java, a Pagoda is a priority queue enforced with a modification of a binary tree. The binary tree is constrained to have priority queue ordering which holds that the parent should be larger than its descendants. A detailed analysis shows that the Pagoda provides a highly efficient implementation of priority queue where the efficiency is measured through average runtime.
A Pagoda is quite similar to that of a heap as Pagodas are used for impermeable priority queues and heaps are used for non-merge able priority queues.
The root of the Pagoda points to its children similar to a binary tree. Every other node points back to its parent and all the way down to its leftmost (if it’s a right child) or rightmost (if it’s a left child) descendant leaf. The key operation is merge or meld, which upholds the heap property.
A node is inserted by merging it as a singleton. The root is removed by merging its two children (left and right). Merging is a bottom-up approach, merging the leftmost edge of one with the rightmost edge of the opposite.
There are two pointers Left(Node) and Right(Node), which are defined as follows
- If R is the Root of the tree T — Right(R) points to the bottom of the right branch of the T and Left(R) to the bottom of the left branch of the T.
- LEFT CHILD
- If K is a left-child in T — Left(K) points to the parent of K and Right(K) to the bottom of the right branch of T.
- RIGHT CHILD
- If K is a right-child in T — Right(K) points to the parent of K and Left(K) to the bottom of the left branch of T.
Major Operations in Pagoda is as follows:
- UNION: All elements of pagoda Q’ are added to pagoda Q and Q’ is discarded.
- Q ← [Q+Q’]
- INSERT: The element k is added to the pagoda Q
- Q ← [Q+k]
- DELETE: The element k is removed from the pagoda Q (This operation is meaningful only if k belongs to Q)
Illustration: The difference in the representation of binary tree and Pagoda
The Orange arrows represent Left Child Pointers and the Green arrows represent Right Child Pointers. Notice that the leaf nodes point to themselves as they don’t have branches.
- INSERT – By considering a single key p as a pagoda, we can treat INSERT as a special case of UNION.
- The best and most effective way to insert an item into a Pagoda is to simply append it at the end.
- This guarantees to maintain the complete tree property. But, this would mean violating the heap property i.e. The parent node in the Pagoda is always greater than its children.
- Perform action on Pagoda using methods discussed below
- Deleting a key k is possible in pagodas without the usage of extra links. To delete k, it is enough to find links to the right and left branches of the sub-trees having k as the root. Once these pointers are found, we continue by performing the union of D and G in that order.
- If Root equals NULL, returns true. Else returns false.
- Makes root as null to pagoda empty.
Root Element : 10 Root Element : 30 Root Element : 30 Root Element : 50 Root Element : 50 Root Element : 40 Root Element : 30 Empty status: false Empty status: true