Specialized Tree - PowerPoint PPT Presentation

1 / 68
About This Presentation
Title:

Specialized Tree

Description:

Basic Splay Tree Operations DeleteMax Peform a FindMax. Set the root to the post-splay root s left child and delete the post-splay root. – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 69
Provided by: anu129
Category:

less

Transcript and Presenter's Notes

Title: Specialized Tree


1
  • Specialized Tree
  • Structures
  • By
  • Anubhav Singh
  • Sidharth Saboo

2
Agenda
  • Threaded binary trees
  • Splay trees
  • Segment tree
  • R-Tree

3
Threaded Binary Trees
  • Introduction
  • Operations
  • Insertion
  • Deletion
  • Traversal
  • Application

4
TREE
  • A tree is a collection of nodes.
  • Each of these nodes are connected by directed
    edge from root node r.
  • Tree consists of a
  • distinguish node r,
  • called root, and zero
  • or more non-empty
  • (sub)trees T1,T2,T3,.TK.

5
  • BINARY TREE
  • A binary tree is a tree in which no nodes can
    have more than two children.
  • The recursive definition is that a binary tree is
    either empty or consists of a root, a left tree,
    and a right tree.
  • The left and right trees
  • may themselves be empty,
  • thus a node
  • with one child could
  • have a left or right child.

6
THREADED BINARY TREE
  • A binary tree is threaded by
  • Making all right child pointers that would
    normally be null point to the inorder successor
    of the node.
  • All left child pointers that would normally be
    null point to the inorder predecessor of the
    node.
  • class threadedTree
  • Cdata pData
  • threadedTree pLeft, pRight
  • bool leftFlag, rightFlag

7
Representation of Threaded Tree
f
-
f
A
f
f
f
B
f
f
B
f
t
E
t
t
E
t
t
E
t
t
H
t
t
H
t
8
  • Threading Rules
  • A RightChild field at node p is replaced by a
    pointer to the node that would be visited after p
    when traversing the tree in inorder. (i.e., it is
    replaced by the inorder successor of p).
  • A LeftChild link at node p is replaced by a
    pointer to the node that immediately precedes
    node p in inorder (i.e., it is replaced by the
    inorder predecessor of p).

9
  • Other Threaded Tree Structures

34
34
20
48
20
48
14
31
14
31
72
25
25
Right Threaded Binary Tree
Left Threaded Binary Tree
10
Inserting A Node In A Threaded Tree
  • Inserting a node r as the right child of node s
  • If s has an empty right subtree, then the
    insertion is simple.
  • If the right subtree of s is not empty,
  • right subtree is made the right subtree of r
    after insertion.
  • r becomes the inorder predecessor of a node that
    has a LeftThreadTRUE field and consequently
    there is a thread which has to be updated to
    point to r.
  • The node containing this thread was previously
    the inorder successor of s.

11
Insertion of r As Right Child of s in Threaded
Binary Tree
s
r
12
Insertion of r As Right Child of s in Threaded
Binary Tree
s
s
r
t
r
u
after
before
13
Deletion of r As Right Child of s
s
r
Before
After
14
InOrder Traversal Of Threaded BinaryTree
  • B-gtD-gtC-gtF-gtE-gtG-gtA

A
B
s
C
D
E
F
G
15
Application (Timing Diagram Editor)

16
Splay Tree
  • Introduction
  • Operations
  • Splay
  • Insertion
  • Deletion
  • Traversal
  • Application

17
Introduction
  • Splay trees self-adjusting BST.
  • Tree automatically reorganizes itself after each
    operation.
  • For each operation on x, rotate x up to root
    using
  • double rotations.
  • Tree remains "balanced" without explicitly
    storing any balance information.
  • Amortized guarantee any sequence of N operations
    takes O(N log N) time.

18
SPLAY OPERATION
  • Basic operation of splay tree
  • When a node x is accessed, a sequence of splay
    steps are performed to move x to the root of the
    tree.
  • In case x is not present in the tree, the last
    element on the search path for x is moved
    instead.
  • There are 6 types of splay steps, each consisting
    of 1 or 2 rotations.


19
RR Rotation
x lt y lt z
20
LL Rotation
  • z lt y lt x

21
LR Rotation
  • y lt x lt z

22
RL Rotation
  • z lt x lt y

23
L Rotation
  • y lt x

24
R Rotation
  • y gt x

25
Basic Splay Tree Operations
  • INSERTION
  • When an item is inserted, a splay is performed.
  • o As a result, the newly inserted item becomes
    the root of the tree.

26
Basic Splay Tree Operations
  • FIND
  • The last node accessed during the search is
  • splayed.
  • If the search is successful, then the node that
    is
  • found is splayed and becomes the new
    root.
  • If the search is unsuccessful, the last node
    accessed prior to reaching the NULL pointer is
    splayed and becomes the new root.
  • FindMin and FindMax perform a splay after
  • the access.

27
Basic Splay Tree Operations
  • DELETE
  • DeleteMin
  • Perform a FindMin.
  • This brings the minimum to the root, and by the
    binary search tree property, there is no left
    child.
  • Use the right child as the new root and delete
    the node containing the minimum.

28
Basic Splay Tree Operations
  • DeleteMax
  • Peform a FindMax.
  • Set the root to the post-splay roots left child
    and delete the post-splay root.

29
Basic Splay Tree Operations
  • Delete
  • Access the node to be deleted bringing it to the
    root.
  • Delete the root leaving two subtrees L (left) and
    R (right).
  • Find the largest element in L using a DeleteMax
    operation, and move the element to the root thus
    the root of L will have no right child.
  • Make R the right child of Ls root.

30
Basic Splay Tree Operations
Remove From Splay Tree Example
31
Applications
  • Network Router
  • Cache Memory Algorithms

32
SEGMENT TREE
33
Outline
  • Introduction
  • Operations
  • Insertion
  • Allocation
  • Deletion
  • Properties
  • Application

34
Introduction
  • Data Structure designed to handle intervals on
    the real line
  • The extremes of the real line belong to fixed set
    of N abscissae/ ordinates
  • It is a static data structure , one that does not
    support insertions or deletions of abscisae/
    ordinate
  • It is a rooted binary tree.

35
The Segment Tree T (4, 15)
36
Outline
  • Introduction
  • Operations
  • Insertion
  • Allocation
  • Deletion
  • Properties
  • Application

37
Operations
  • Insertion Operation
  • Procedure Insert( b, e v)
  • begin if ( b lt Bv ) and ( E(v)lt e )
  • then allocate b,e to v
  • else begin if ( b lt ( Bv Ev ) / 2 )
  • then INSERT(b,e,LSONv)
  • if (( Bv) Ev ) / 2 lt e )
  • then INSERT(b,e,RSONv)
  • end
  • end

Here b begin e end v root Bv begin of
root Ev end of root
38
Insertion of interval 5, 8 into T 4 , 15
4,15
9,15
4,9
12,15
6,9
9,12
4,6
4,5
5,6
6,7
7,9
8,9
7,8
39
Allocation of an interval to a node
  • Allocation of an interval to a node v depends
    upon the requirement
  • Generally , it is managed using a parameter Cv
    a non negative integer, referred to as
    cardinality
  • Allocation of b, e to v becomes
  • Cv Cv 1
  • In addition, we append to each node the
    identifiers of the intervals

40
Delete Operation
  • Procedure Delete( b, e v)
  • begin if ( b lt Bv ) and ( E(v)lt e )
  • then Cv Cv - 1
  • else begin if ( b lt ( Bv Ev ) / 2 )
  • then DELETE(b,e,LSONv)
  • if (( Bv) Ev ) / 2 lt e )
  • then DELETE(b,e,RSONv)
  • end
  • end

41
Outline
  • Introduction
  • Operations
  • Insertion
  • Allocation
  • Deletion
  • Properties
  • Application

42
Some Properties
  • It is a balanced tree
  • Depth of tree is log2(r-l)
  • Can be built in O(nlogn) time
  • All intervals containing a query interval can be
    reported in O(logn k) time

43
Some Properties
  • Extremely versatile data structures
  • Used extensively in computational geometry
    problems and geographic information systems
  • Example Used by MC2 developers in isothetic
    polygon merging algorithm

44
Outline
  • Introduction
  • Operations
  • Insertion
  • Allocation
  • Deletion
  • Properties
  • Application

45
Application
  • Merged Using Segment Tree

46
R - Tree
47
Outline
  • Introduction
  • Operations
  • Insertion
  • Deletion
  • Search
  • Properties
  • Application

48
Introduction
  • Tree data structures similar to B-trees
  • Height Balanced Tree
  • Leaf nodes contain pointers to data objects
    containing spatial data. (MBR)
  • Rectangles at each node may be overlapping.

49
Example of an R- Tree for 2-D Rectangles
50
Motivation
  • Masks used in the fabrication of integrated
    circuits are frequently expressed as collection
    of rectangles with sides parallel to orthogonal
    directions
  • The number of such rectangles is huge requiring
    efficient data structures to store them
  • R Tree is one such data structure

51
Minimum Bounding Rectangle
  • Objects are added to an MBR that will lead to
    the smallest increase in its size
  • In R Tree Objects (shapes, lines and points)
    are grouped using the minimum bounding rectangle
    (MBR)
  • MBR is represented by (min(x) min(y)) and max(x)
    max(y)

52
Outline
  • Introduction
  • Operations
  • Insertion
  • Deletion
  • Search
  • Properties
  • Application

53
Operations
  • Insertion Operation
  • To Insert a new index entry E into an R tree
  • Step 1 Find position for new record Invoke
    ChooseLeaf to select a leaf node L in which to
    place E.
  • Step 2 Add record to leaf node If L has room,
    insert E. Otherwise invoke SplitNode to obtain L
    and LL containing E and all old entries of L

54
Insertion Operation
  • Step 3 Propagate changes upwards Invoke
    AdjustTree on L, also passing LL if split was
    performed
  • Step 4 Grow Tree taller If node split
    propagation caused the root to split, create a
    new root whose children are the resulting nodes.

55
T 2
R 4
T 1
R 2
R 1
R 3
R 5
Rectangular Data Objects
R Tree Insertion
R 1
R 2
R 3
T 1
T 2
R 1
R 2
R 3
R 4

R 5
56
Deletion Operation
  • To Remove index record E from R Tree
  • Step 1 Find node containing record Invoke
    FindLeaf to locate the leaf node L containing E.
    Stop if the record was not found.
  • Step 2 Delete record Remove E from L
  • Step 3 Propagate changes Invoke CondenseTree,
    passing L

57
Deletion Operation
  • Step 4 Shorten Tree If the root node has only
    one child after the tree has been adjusted, make
    the child the new root

58
Search Operation
  • To find all index records whose rectangles
    overlap a search rectangle S in a given R
    Tree with root node T
  • Step 1 Search subtrees if T is not a leaf,
    check each entry E to determine whether E
    overlaps S. For all overlapping entries, invoke
    Search on the tree whose root node is pointed by
    child pointer of E

59
Search Operation
  • Step 2 Search leaf node If T is a leaf, check
    all entries E to determine whether E overlaps S.
    If so, E is a qualifying record

60
Outline
  • Introduction
  • Operations
  • Insertion
  • Deletion
  • Search
  • Properties
  • Application

61
Some Properties
  • To split nodes when they become too full,
    different algorithms are used resulting in the
    "Quadratic" and "Linear" R-tree sub-types.
  • Not the best worst case performance but generally
    perform well with real-world data
  • Easy to add to any relational database system

62
Some Properties
  • Extensively used for spatial access methods i.e.,
    for indexing multi-dimensional information for
    example, the (X, Y) coordinates of geographical
    data.
  • Real world example Used by Interras MC2 to
    store pins of a memory

63
Outline
  • Introduction
  • Operations
  • Insertion
  • Deletion
  • Search
  • Properties
  • Application

64
View of a memory
courtesy MC2 Development Team
65
Zoomed - in View
66
  • Overlapped Geometries

67
  • A Single Geometry

68
Thank You !!
Write a Comment
User Comments (0)
About PowerShow.com