Title: Specialized Tree
1- Specialized Tree
- Structures
- By
- Anubhav Singh
- Sidharth Saboo
2Agenda
- Threaded binary trees
- Splay trees
- Segment tree
- R-Tree
3Threaded Binary Trees
- Introduction
- Operations
- Insertion
- Deletion
- Traversal
- Application
4TREE
- 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.
6THREADED 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
-
7Representation 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
10Inserting 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.
11Insertion of r As Right Child of s in Threaded
Binary Tree
s
r
12Insertion 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
14InOrder Traversal Of Threaded BinaryTree
- B-gtD-gtC-gtF-gtE-gtG-gtA
A
B
s
C
D
E
F
G
15Application (Timing Diagram Editor)
16Splay Tree
- Introduction
- Operations
- Splay
- Insertion
- Deletion
- Traversal
- Application
17Introduction
- 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.
18SPLAY 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.
19RR Rotation
x lt y lt z
20LL Rotation
21LR Rotation
22RL Rotation
23L Rotation
24R Rotation
25Basic 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.
26Basic 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.
27Basic 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.
28Basic Splay Tree Operations
- DeleteMax
- Peform a FindMax.
- Set the root to the post-splay roots left child
and delete the post-splay root.
29Basic 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.
30Basic Splay Tree Operations
Remove From Splay Tree Example
31Applications
32SEGMENT TREE
33Outline
- Introduction
- Operations
- Insertion
- Allocation
- Deletion
- Properties
- Application
34Introduction
- 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.
35The Segment Tree T (4, 15)
36Outline
- Introduction
- Operations
- Insertion
- Allocation
- Deletion
- Properties
- Application
37Operations
- 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
38Insertion 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
39Allocation 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
40Delete 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
41Outline
- Introduction
- Operations
- Insertion
- Allocation
- Deletion
- Properties
- Application
42Some 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
43Some 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
44Outline
- Introduction
- Operations
- Insertion
- Allocation
- Deletion
- Properties
- Application
45Application
- Merged Using Segment Tree
46R - Tree
47Outline
- Introduction
- Operations
- Insertion
- Deletion
- Search
- Properties
- Application
48Introduction
- 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.
49Example of an R- Tree for 2-D Rectangles
50Motivation
- 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
51Minimum 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)
52Outline
- Introduction
- Operations
- Insertion
- Deletion
- Search
- Properties
- Application
53Operations
- 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
54Insertion 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.
55T 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
56Deletion 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
57Deletion 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
58Search 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
59Search 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
60Outline
- Introduction
- Operations
- Insertion
- Deletion
- Search
- Properties
- Application
61Some 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
62Some 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
63Outline
- Introduction
- Operations
- Insertion
- Deletion
- Search
- Properties
- Application
64View of a memory
courtesy MC2 Development Team
65Zoomed - in View
66 67 68Thank You !!