Anda di halaman 1dari 33

TREAP

MANOHAR.BHAT.B 4JC07CS056 8th Sem C.S SJCE, Mysore

Introduction
What is a Tree ?
Tree is a acyclic connected graph where each node has zero or more children nodes and at most one parent node. In a tree there is only path between any two nodes. Ex: Binary search trees, which are particularly well-suited for sorting and searching algorithms.

Types
Trees
Binary tree :
Binary search tree, Cartesian tree, Top tree.

Self balancing :
AVL tree, Red black tree, Splay tree.

B-trees :
B+ tree, B* tree, 2-3 tree, 2-3-4 tree.

Tries :
Suffix tree, Radix tree.

Space-partitioning trees :
Quad tree, Octree, k-d tree.

Others :
Heaps, Hash tree, Metric tree etc

Cartesian tree
A Cartesian tree is a heap ordered binary tree which is derived from a unique sequence of numbers. The in-order traversal of the tree returns the original sequence of numbers. Heap property : Parent of any non-root node has greater value then its children.

Example of Cartesian tree

TREAP
A treap is a 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.

Why Treap?
BS trees are used to search for a particular key from a given set of keys. Suppose if we know the frequency of searches for different keys, then using a normal BS tree may not take full advantage of this knowledge. In such cases, treap can be used by assigning high priority to most frequently searched keys so that the number of comparisons to search for them is least. Treap is the `ideal tree` for searching when we know the statistics of the searches.

Why treap?
1 : 90 searches, 2 :- 3 searches, 3 :- 7 searches

2 :- 1 comparison 1 and 3 :- 2comparisons Total :- 180+3+14

2 :- 1 comparison 1 and 3 :- 2comparisons Total :- 90+14+9

Properties
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.

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.

Terminology

Example

Height of a Treap
In best case the height of treap with `n` nodes is `floor(log2(n))`. In worst case the height of Treap is `n`. In average case it will be around O(3*c*log2n). where `c` is a positive integer constant.

Treapifying Algorithm
Treapifying is a recursive procedure which follows bottom up approach. Algorithm:
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 }

Rotation
Whenever priority of parent is less than its child, a rotation is made. There are 6 types of rotations possible in a treap. In all cases of rotation, care should taken to see that treap has retained the binary search property even after the rotation. This can be done by carefully attaching the grand child trees in appropriate position.

Rotation - Simple case


Left subtree null and RP > TP - After rotation call Treapify (T)

Rotation - Simple case


Right subtree null and LP > TP - After rotation call Treapify (T)

Rotation
RP > TP > LP - After rotation call Treapify(T)

Rotation
RP > LP > TP - After rotation call Treapify(L)

Rotation
LP > TP > RP - After rotation call Treapify(T)

Rotation
LP > RP > TP After rotation call Treapify(R)

Rotation - Explanation
LP > RP > TP After rotation call Treapify (R)

Here Treapify (R) is called after rotation because l2 which is a grand child of `R` may contain higher priority nodes than `R`.

Treap Insertion
Insertion is similar to the insertion in binary search tree Start at the root and descend until we either find the target or try to descend from a leaf. After insertion of the node in appropriate place, we make rotations so that the Heap property remains consistent in the treap. The rotations are made by following the treapification algorithm. Insertion operation is in O (Height).

Treap Insertion
Algorithm :
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) } }

Treap deletion
Deletion is completely different from that of binary search tree. If the node to be deleted has only one child, then the node is simply replaced by the child. If the node has two childs, then node is replaced by the child having higher priority. Appropriate rotations are made by calling Treapify procedure.

Treap Deletion
Algorithm :
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) }

Treap deletion
else {

if( node.key < root.key ) Delete (left, node) else Delete (right, node)
}

Deletion Example
Delete (T) Assume RP > LP, Call Treapify(L) after rotation

Search
Searching is basically simple. It uses the property of a Binary Tree. Algorithm
Search(root,item) if (root == NULL) return if (root.key== item) return 0 if (item< root.key) then Search ( left, item ) else Search ( right, item )

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
In the best case the height of tree would be minimum [in O(log2n)]. This can be done by carefully choosing the appropriate priority for each node. Hence the best case performance of all operations will in O(log2n).

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 union, set intersection and set difference operations. They are also used for maintaining authorization certificates in public key cryptosystems.

Anda mungkin juga menyukai