This action might not be possible to undo. Are you sure you want to continue?

# Visveshwaraya Technological University-Belgaum

TREAP

A seminar report

submitted by

Manohar.Bhat.B

(USN- 4JC07CS056) in partial fulfilment for the award of the degree of

BACHELOR OF ENGINEERING

in

**COMPUTER SCIENCE & ENGINEERING
**

Under the guidance of

Sri Ramashesha Mudigere Professor

**SRI JAYACHAMARAJENDRA COLLEGE OF ENGINEERING
**

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING (Affiliated to Visvesvaraya Technological University, Belgaum)

MYSORE-570006

Certificate

Certified that this is a bonafide record of the seminar entitled

TREAP

done by the following student

Manohar.Bhat.B

of the VIIIth semester, Computer Science and Engineering in the year 2011 in partial fulfilment of the requirements to the award of Degree of Bachelor of Engineering in Computer Science and Engineering under Visveshwaraya Technological University(VTU).

Seminar Guide Sri Ramashesha Mudigere Professor

External Evaluator Smt. Shilpa B. V Snr. Scale Lecturer

Head of the Department Dr. C. N. Ravikumar

**Date: 10th March 2011
**

2

ACKNOWLEDGEMENT

I express my gratitude to Dr. C. N. Ravikumar, Head of the Department, for providing me with adequate facilities, ways and means by which I was able to complete this seminar. I express my sincere gratitude to him for his constant support and valuable suggestions without which the successful completion of this seminar would not have been possible. I thank Sri Ramashesha Mudigere, my Seminar Guide for their boundless cooperation and helps extended for this seminar. I express my immense pleasure and thankfulness to all the teachers and staff of the Department of Computer Science and Engineering, SJCE for their cooperation and support. Last but not the least, I thank all others, and especially my classmates and my family members who in one way or another helped me in the successful completion of this work.

Manohar.Bhat.B

3

Page Index

Contents 1. Introduction 2. Properties 3. Height of a Treap 4. Data Structure 5. Algorithm Insert Delete Left-Rotate Right-Rotate Search 6. Complexity 7. Input and Output 8. Applications 9. References

Page No 5 6 7 9 8 10 10 11 11 12 12 13 13 13

4

1. Introduction

A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

A tree is

a

widely-used data

structure that

emulates

a

hierarchical tree structure with a set of linked nodes. More specifically tree is an acyclic connected graph where each node has zero or more children nodes and at most one parent node. Furthermore, the children of each node have a specific order.

Different kinds of trees are suited to different kinds of applications, and some are highly specialized to specific tasks. For example, Btrees are particularly well-suited for implementation of databases.

A Cartesian tree is a binary tree derived from a sequence of numbers, it can be uniquely defined from the properties that it is heapordered and that a symmetric (in-order) traversal of the tree returns the original sequence A treap is binary search tree which also obeys the heap property with respect to the priority of nodes. The treap was first described by Cecila R. Aragon and Raimund Seidel in 1989. Its name is a combination of `tree` and `heap`. (Tree + heap = Treap). A treap has a additional attribute called priority i.e., each node has a priority associated with it.

5

Example of Cartesian tree

2. Properties

A treap has properties of both binary search tree and heap.

Each node has utmost two children and one parent (except root). Each node has two parameters – search key and priority. With respect to search keys, it should obey the binary search tree properties. i.e. the key of the parent should be greater than left child and less than that of right child. With respect to priorities, it should obey the heap property.

6

3. Height of Treap

Let h be the black height of a Treap on n nodes. In the worst case the insertion goes only in one direction and height of tree would be `n-1` itself. In the best case treap will be perfectly height balanced and it would be a complete tree. Height of treap in this case is log2(n). Therefore we can say that height satisfies the following log2 n < h < n-1 An example of a treap is shown in the figure

7

8

4. Data Structure

The description of the basic structure node is as follows struct Treap { int key; int priority; treap *left; treap *right; treap *parent; }; The variable ‘key’ holds the value of the node and the variable priority gives us the priority of the node. The pointer *left points to the left child of the node if present or else it points to null. Similarly the pointer *right points to the right child node if present or else it points to null.

5. Algorithm

The various algorithms used are given below.

Treapify

Treapify(root) { if(left!=NULL) Treapify(left) if(right!=NULL) Treapify(right) if(root.priority < max(left.priority,right.priority)) Perform necessary rotations and call procedure on suitable node again }

9

Insertion

Insert (root, node) { if(node.key < root.key) { if(left==NULL) left=node else Insert(left,node) } else { if(right==NULL) right=node else Insert(right,node) } }

Deletion

Delete (root , node) { if (root == NULL ) { Print “node not found” return } else if (root.key == node.key) { remove current node and replace by child having maximum priority Call Treapify (Min priority child) } else { if( node.key < root.key ) Delete (left, node) else Delete (right, node) } }

10

Left Rotate

Left Rotate ( T, x )

{ y right [ x ] right [ x ] left[ y ] if ( left[ y ]!= NULL) then parent[ left[ y ]] x parent[ y ] parent[ x ] if ( parent[ x ] == NULL) then root[ T ] y elseif ( x= = left[ parent[ x ]]) then left[ parent[ x ]] y else right[ parent[ x ]] y left [ y ] x parent [ x ] y }

Right rotate

Right-rotate(T,x) { y left[ x ] left [ x ] right[ y ] if ( right[ y ]!= NULL) then parent[ right[ y ]] x parent[ y ] parent[ x ] if ( parent[ x ]== NULL) then root[ T ] y elseif (x == right[ parent[ x ]]) then right[ parent[ x ]] y else left[ parent[ x ]] y right[ y ] x parent [ x ] y

11

}

Search

Search(T, item) { if (root[ T ]== NULL) return if (info[ root[ T ]]== item) return 0 if (item<info[ root[ T ]]) then Search ( left[root[T]],item ) else Search ( right[root[T]],item ) }

6. Complexity

• Complexity of all the operations performed on treap is directly proportional to the height of the treap. • In worst case, the operations perform comparisons equal to the height of treap. • In worst case, the height of treap will be n-1, and hence in worst case all operations will be in O(n) and treap would behave as a linear array. • Complexity of all the operations performed on treap is directly proportional to the height of the treap. • In worst case, the operations perform comparisons equal to the height of treap. • In worst case, the height of treap will be n-1, and hence in worst case all operations will be in O(n) and treap would behave as a linear array.

12

**7. Input and Output
**

The user has been provided with 2 options to supply the input to the program. The program can either take manual input of each node or the program itself can read from a file containing the input numbers. The output of the program is in the command prompt.

8. Applications

Treaps are used in many sorting and searching algorithms. They are used as in cases where both binary search tree and priority queue is needed. Treap are used in the problem of maintaining sets of items and performing set operations. They are also used for maintaining authorization certificates in public key cryptosystems. union, set intersection and set difference

9. References

Introduction to Algorithms, by Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Design & Analysis of Algorithms by Anany Levitin.

13