SlideShare ist ein Scribd-Unternehmen logo
1 von 56
Advanced Data Structures and
Implementation
•
•
•
•
•
•
•
•
•

Top-Down Splay Trees
Red-Black Trees
Top-Down Red Black Trees
Top-Down Deletion
Deterministic Skip Lists
AA-Trees
Treaps
k-d Trees
Pairing Heaps
Top-Down Splay Tree
• Direct strategy requires traversal from the root
•
•
•

down the tree, and then bottom-up traversal to
implement the splaying tree.
Can implement by storing parent links, or by
storing the access path on a stack.
Both methods require large amount of overhead
and must handle many special cases.
Initial rotations on the initial access path uses
only O(1) extra space, but retains the O(log N)
amortized time bound.
Case 1: Zig
X

L

R

L

R

Y
X

Y
XR
YL

YL

Yr

If Y should become root, then X and
its right sub tree are made left
children of the smallest value in R,
and Y is made root of “center” tree. Y
does not have to be a leaf for the Zig
case to apply.

Yr
XR
Case 2: Zig-Zig
X

L
Y

R

L

R

Z
Y

XR

Z

ZL

X

Zr

YR
YR
ZL

Zr

The value to be splayed is in
the tree rooted at Z. Rotate Y
about X and attach as left
child of smallest value in R

XR
Case 3: Zig-Zag
(Simplified)
X

L
Y

R

X

YL

Z

ZL

R
Y

XR

YL

L

Zr

Z

ZL

XR

Zr

The value to be splayed is in the tree rooted at Z. To make code simpler, the
Zig-Zag rotation is reduced to a single Zig. This results in more iterations in
the splay process.
Splay Trees Implementation
• See page 457 for generic SplayTree class

implementation.
• Page 458 shows Top-Down splaying
method.
• Refer to page 459 for insertion method
into the Top-Down Splay Tree.
• Page 460 shows deletion method from the
tree.
Reassembling the Splay Tree
L

X

X

R
L

XL

R

XR

XL

XR

When the value to be splayed to the root is at the root of the “center”
tree, we have reached the point where we are ready to reassemble the
tree. This is accomplished by a) making XL the right child of the
maximum element in L, b) making XR the left child of the minimum
element in R, and then making L and R the left and right children of X
Operation 1: Zig-Zig
L

R

A

L

Ar

B

Dl
F

H

Gl
G

Fl

X

Hl
H

Gl

Xl

Xr

X

Hl
Xl

Rotate B around
A and make L
child of
minimum
element in R
(which is now
empty)

Ar

G

Fl

Er

F

Br

Er

E

Dl

A

E

Cr

D

B
Cr

D

Br

C

R

C

Xr

L is still empty, and R
is now the tree rooted
at B. Note that R
contains nodes > X but
not in the right subtree
of X.
Operation 2: Zig-Zag
L

R

C
B

Cr

D

Er

F

Br

Dl
Fl

Cr

H

Br

X

Hl
H

A

C

G
Gl

Gl

Xl

Xr

X

Hl
Xl

Just perform
Zig
(simplified
Zig-Zag)

B

Er

F
Ar

G

Fl

R

E
D

A

E

Dl

L

Xr

L was previously
empty and it now
consists of node D and
D’s left subtree

Ar
After X reaches root:
L
D
Dl

Xr

Xl

B

G

A

C
H

F
Fl

R

X

Gl

Cr

E

Hl

Br

Ar

Er

Reassemble – XL becomes right sub
tree of H, XR becomes left sub tree of
E, and then L, R reattached to X

X

B

D
Dl

G
H
Gl

A

C

F
Fl

This
configuration
was achieved
by doing Zig
Zig (of F, G)
followed by a
Zig (node H)

Hl

Cr

E
Xl

Er

Br

Ar

Note that this is not the
same tree as was obtained
by doing BU splaying.
Red-Black Tree
•
•
•
•
•
•
•
•

Popular alternative to the AVL tree.
Operations take O(log N) time in worst case.
Height is at most 2log(N+1).
A red-black tree is a binary search tree with one extra
attribute for each node: the color, which is either red or
black.
The root is black.
If node is red, its children must be black .
Every path from a node to a null reference must
contain the same number of black nodes .
Basic operations to conform with rules are color changes
and tree rotations.
Theorem 1 – In a red-black tree, at least
half the nodes on any path from the root to
a leaf must be black.
Proof – If there is a red node on the path,
there must be a corresponding black node.
Theorem 2 – In a red-black tree, no path from
any node, N, to a leaf is more than twice as
long as any other path from N to any other
leaf.
Proof: By definition, every path from a node to
any leaf contains the same number of black
nodes. By Theorem1, a least ½ the nodes on
any such path are black. Therefore, there can
no more than twice as many nodes on any
path from N to a leaf as on any other path.
Therefore the length of every path is no more
than twice as long as any other path.
Theorem 3 –
A red-black tree with n internal nodes has
height h <= 2 lg(n + 1).
Proof: Let h be the height of the red-black tree
with root x. By Theorem 1,
bh(x) >= h/2
From Theorem 1, n >= 2bh(x) - 1
Therefore n >= 2 h/2 – 1
n + 1 >= 2h/2
lg(n + 1) >= h/2
2lg(n + 1) >= h
Bottom-Up Insertion
•
•
•
•
•

Cases:
0: X is the root – color it black
1: Both parent and uncle are red – color parent and
uncle black, color grandparent red, point X to
grandparent, check new situation
2 (zig-zag): Parent is red, but uncle is black. X and its
parent are opposite type children – color grandparent
red, color X black, rotate left on parent, rotate right on
grandparent
3 (zig-zig): Parent is red, but uncle is black. X and its
parent are both left or both right children – color parent
black, color grandparent red, rotate right on grandparent
Top-Down Red-Black Trees
• In T-Down insertion, the corrections are done
•
•
•

while traversing down the tree to the insertion
point.
When the actual insertion is done, no further
corrections are needed, so no need to traverse
back up the tree.
So, T-Down insertion can be done iteratively
which is generally faster.
Insertion is always done as a leaf (as in ordinary
BST insertion).
Process
• On the way down, when we see a node X that
•
•

has two red children, we make X red and its two
children black.
If X’s parent is red, we can apply either the
single or double rotation to keep us from having
two consecutive red nodes.
X’s parent and the parent’s sibling cannot both
be red, since their colors would already have
been flipped in that case.
Example: Insert 45
30
15

85

60

20

10

5

70

Two red
children

50
40

65
55

80

90
Example (Cont.)
30
15
10

5

70
85

60

20

flip colors two red nodes

50
40

65
55

80

90
Example (Cont.): Do a single
rotation
30
15
10

5

60
20

70

50
40

55

85

65
80

90
Example (Cont.): Now Insert 45
30
15
10

5

60
20

70

50
40

55
45

85

65
80

90
Note
• Since the parent of the newly inserted node was
•
•

black, we are done.
Had the parent of the inserted node been red,
one more rotation would have had to be
performed.
Although red-black trees have slightly weaker
balancing properties, their performance in
experimentally almost identical to that of AVL
trees.
Implementation of Top-Down RedBlack Trees
• See pages 464-467
Top-Down Deletions
• Recall that in deleting from a binary search tree, the only
•
•
•
•

nodes which are actually removed are leaves or nodes
with exactly one child.
Nodes with two children are never removed. Their
contents are just replaced.
If the node to be deleted is red, there is no problem - just
delete the node.
If the node to be deleted is black, its removal will violate
property.
The solution is to ensure that any node to be deleted is
red.
Deterministic Skip Lists
• A probabilistically balanced
•
•
•

linked list.
Invented in 1986 by William
Pugh.
Definition: Two elements are
linked if there exists at least
one link going from one to
another.
Definition: The gap size
between two elements linked
at height h is equal to the
number of elements of height
h-1 between them.
Skip List
d) xtra pointers every eighth item - full structure

3

6

7

9

12

17

21

19

25

26

NIL

e) skip list - same link distribution, random choice
6
3

7

9

12

17

19

21

25

NIL
26
Search time
• In the deterministic version (a-d):
–
–
–
–

in a, we need to check at most n nodes
in b, at most n/2+1 nodes
in c, at most n/4+2 nodes
in general, at most log N nodes

• Efficient search, but impractical insertion
and deletion.
Levels
• A node with k forward level
•

pointers is called a
level k node.
If every (2i)th node
has a pointer 2i nodes
ahead, they have the
following distribution:

percent

1

50

2

25

3

12.5

…

…
Central idea in skip lists
• Choose levels of nodes randomly, but in

the same proportions (as in e).
• A node’s i th forward pointer, points to the
next node of level i or higher.
• Insertions and deletions require only local
modifications.
• A node’s level never changes after first
being chosen.
Insertion
• To perform insertion, we must make sure

that when a new node of height h is
added, it doesn’t create a gap of four
heights of h node (in 1-2-3 deterministic
skip list).
• See page 269 fig. 12.19
• For implementation of Skip List see pages
472-474.
AA-Trees
• Also known as binary B-tree (BB-tree).
• BB-tree is a red-black tree with one extra condition: any
•

node may have at most one red child.
Some conditions to make it simpler (p.475):
- only right child can be red
- code functions recursively
- instead of color store information in small
integer:
- one if the node is a leaf
- the level of its parent, if the node is red
- one less then the level of its parent, if the
node is black
Advantages
• AA-trees simplify algorithms by:

- eliminating half of the restructuring cases
- simplifying deletion by removing an annoying
case
• if an internal node has only one child, that
child must be a red right child
• We can always replace a node with the
smallest child in the right sub tree (it will
either be a leaf or have a red child)
Links in AA-tree
• A horizontal link is a connection between a node
•
•
•
•

and a child with equal levels.
Horizontal links are right references.
There cannot be two consecutive horizontal
links.
Nodes at level 2 or higher must have two
children.
If a node has no right horizontal link, its two
children are at the same level.
Example
70

30
15

5

10

50

20

35

40

60

55

85

65

80

90
Insertion in AA-tree
• A new item is always inserted at the bottom
•
•
•

level.
In the previous example, inserting 2 will create a
horizontal left link.
In the previous example, inserting 45 generates
consecutive right links.
After inserting at the bottom level, we may need
to perform rotations to restore the horizontal link
properties.
skew – remove left horizontal links
P

A

X

B

P

C

A

X

B

C
split – remove consecutive
horizontal links
P
X

A

P

B

G

X

A

G

B
More on skew & split
• skew removes a left horizontal link.
• skew might also create consecutive right

horizontal links.
• First we must apply skew and then use
split, if necessary.
• After a split, the middle node increases a
level, which may create a problem for the
original parent.
Implementation of AA-trees
• Refer to pages 476 – 480 for detailed

implementation techniques.
• See page 477 & 479 for more examples of
left and right rotations of AA-trees.
Treaps
• Binary search tree.
• Like skip list, it uses random numbers and
gives O (log N) expected time for any
input.
• Slower than balanced search tree.
• Although deletion is much slower, it is still
O (log N) expected time.
Definition of a treap
• Each node stores an item, left and right link, and
•

•

a priority that is randomly assigned when the
node is created.
Treap is a binary search tree with the property
that the node priorities satisfy heap order: any
node’s priority must be at least as large as its
parents.
See pages 481-483 for implementation details.
k – d Trees
•
•

•
•

Multidimensional b-tree.
Branching of odd levels is
done with respect to the
first key, and branching
on even levels is done
with respect to the
second key.
Root is arbitrary chosen
to be an odd level.
Can be visually
represented:
Some facts about k-d trees
• Can have any number of dimensions.
• In practice searches tend to be very

efficient.
• For a randomly constructed tree, the
average running time of a partial match
query is O (M+kN^(1-1/k)).
• See pages 484-485 for implementation
details.
Pairing Heaps
• A min (max) pairing heap is a min (max)
tree in which operations are done in a
specified manner. 8

2

1

6

5

4

1

3

3

3

4

2

1

5
Insert
• Create 1-element max tree with new item
and meld with existing max pairing heap.
9

9

7

6

6

3

7

+ insert(2) =

2

7

6

6

3

7
Insert (Cont.)
• Create 1-element max tree with new item
and meld with existing max pairing heap.
14
9

7

6

7

9

+ insert(14) =
7

6

6

6

3

3

7
IncreaseKey (Node, theAmount)
• Since nodes do not have parent fields, we

cannot easily check whether the key in the
Node becomes larger than that in its
parent.
• So, detach the Node from sibling doublylinked list and meld.
IncreaseKey (Node, theAmount)
9

theNode
6

4

3

5

2

4

2

4

1

2

3

6

1

3

• If theNode is not the root,
1

remove sub tree rooted at
theNode from its sibling list.
IncreaseKey (Node, theAmount)
9
18

2

2

6

3
4

3

3

1

5

2

4

1

1

6
IncreaseKey (Node, theAmount)
18
9
2
2

6

3

6
3

4

3

1

5

2

4

1

1
Pairing heaps
• See pages 488 – 491 for implementation
details.

Weitere ähnliche Inhalte

Was ist angesagt? (20)

10 Red-Black Trees
10 Red-Black Trees10 Red-Black Trees
10 Red-Black Trees
 
Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)Balanced Tree(AVL Tree,Red Black Tree)
Balanced Tree(AVL Tree,Red Black Tree)
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red black trees presentation
Red black trees presentationRed black trees presentation
Red black trees presentation
 
Red black tree
Red black treeRed black tree
Red black tree
 
Red black tree
Red black treeRed black tree
Red black tree
 
Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)Balanced Tree (AVL Tree & Red-Black Tree)
Balanced Tree (AVL Tree & Red-Black Tree)
 
Binary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red BlackBinary Search Trees - AVL and Red Black
Binary Search Trees - AVL and Red Black
 
Lec14
Lec14Lec14
Lec14
 
Red black tree insertion
Red black tree insertionRed black tree insertion
Red black tree insertion
 
Balance tree. Short overview
Balance tree. Short overviewBalance tree. Short overview
Balance tree. Short overview
 
Trees in data structure
Trees in data structureTrees in data structure
Trees in data structure
 
Data Structure: TREES
Data Structure: TREESData Structure: TREES
Data Structure: TREES
 
AVL Tree
AVL TreeAVL Tree
AVL Tree
 
Avl trees
Avl treesAvl trees
Avl trees
 
lecture 14
lecture 14lecture 14
lecture 14
 
Trees
TreesTrees
Trees
 
Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures Trees, Binary Search Tree, AVL Tree in Data Structures
Trees, Binary Search Tree, AVL Tree in Data Structures
 
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
358 33 powerpoint-slides_11-efficient-binary-trees_chapter-11
 
Red black trees
Red black treesRed black trees
Red black trees
 

Andere mochten auch

Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)nikhilarora2211
 
Cloud Computing Principles and Paradigms: 2 migration into a cloud
Cloud Computing Principles and Paradigms: 2 migration into a cloudCloud Computing Principles and Paradigms: 2 migration into a cloud
Cloud Computing Principles and Paradigms: 2 migration into a cloudMajid Hajibaba
 
cloud computing, Principle and Paradigms: 1 introdution
cloud computing, Principle and Paradigms: 1 introdutioncloud computing, Principle and Paradigms: 1 introdution
cloud computing, Principle and Paradigms: 1 introdutionMajid Hajibaba
 
Cloud Computing Principles and Paradigms: 3 enriching the integration as a se...
Cloud Computing Principles and Paradigms: 3 enriching the integration as a se...Cloud Computing Principles and Paradigms: 3 enriching the integration as a se...
Cloud Computing Principles and Paradigms: 3 enriching the integration as a se...Majid Hajibaba
 
Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...
Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...
Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...Majid Hajibaba
 

Andere mochten auch (6)

Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)Splay trees by NIKHIL ARORA (www.internetnotes.in)
Splay trees by NIKHIL ARORA (www.internetnotes.in)
 
Cloud Computing Principles and Paradigms: 2 migration into a cloud
Cloud Computing Principles and Paradigms: 2 migration into a cloudCloud Computing Principles and Paradigms: 2 migration into a cloud
Cloud Computing Principles and Paradigms: 2 migration into a cloud
 
cloud computing, Principle and Paradigms: 1 introdution
cloud computing, Principle and Paradigms: 1 introdutioncloud computing, Principle and Paradigms: 1 introdution
cloud computing, Principle and Paradigms: 1 introdution
 
Cloud Computing Principles and Paradigms: 3 enriching the integration as a se...
Cloud Computing Principles and Paradigms: 3 enriching the integration as a se...Cloud Computing Principles and Paradigms: 3 enriching the integration as a se...
Cloud Computing Principles and Paradigms: 3 enriching the integration as a se...
 
Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...
Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...
Cloud Computing Principles and Paradigms: 5 virtual machines provisioning and...
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 

Ähnlich wie Advanced data structures and implementation

anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdfanastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdfAayushAdhikari27
 
Red black tree
Red black treeRed black tree
Red black treeRajendran
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search TreeAdityaK92
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.pptplagcheck
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binarySSE_AndyLi
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptSeethaDinesh
 
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptUnit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptSheba41
 
trees in data structure
trees in data structure trees in data structure
trees in data structure shameen khan
 
lecture 13
lecture 13lecture 13
lecture 13sajinsc
 

Ähnlich wie Advanced data structures and implementation (20)

anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdfanastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
anastasio-red-black-trees-1-1-091222204455-phpapp02.pdf
 
Red black trees
Red black treesRed black trees
Red black trees
 
16 rbtrees
16 rbtrees16 rbtrees
16 rbtrees
 
BINARY SEARCH TREE
BINARY SEARCH TREEBINARY SEARCH TREE
BINARY SEARCH TREE
 
Red black tree
Red black treeRed black tree
Red black tree
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Red blacktrees
Red blacktreesRed blacktrees
Red blacktrees
 
rbtrees.ppt
rbtrees.pptrbtrees.ppt
rbtrees.ppt
 
15-btrees.ppt
15-btrees.ppt15-btrees.ppt
15-btrees.ppt
 
7 chapter4 trees_binary
7 chapter4 trees_binary7 chapter4 trees_binary
7 chapter4 trees_binary
 
Binary tree
Binary treeBinary tree
Binary tree
 
Red-black trees
Red-black treesRed-black trees
Red-black trees
 
BINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.pptBINARY TREE REPRESENTATION.ppt
BINARY TREE REPRESENTATION.ppt
 
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.pptUnit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
Unit 2 ADvanced Data Sturctures and Algorithms Red-black_trees.ppt
 
Cse 225 rbt_red_black_tree
Cse 225 rbt_red_black_treeCse 225 rbt_red_black_tree
Cse 225 rbt_red_black_tree
 
lecture14.ppt
lecture14.pptlecture14.ppt
lecture14.ppt
 
trees in data structure
trees in data structure trees in data structure
trees in data structure
 
7.tree
7.tree7.tree
7.tree
 
lecture 13
lecture 13lecture 13
lecture 13
 
Tree traversal techniques
Tree traversal techniquesTree traversal techniques
Tree traversal techniques
 

Mehr von Umang Gupta

23 network security threats pkg
23 network security threats pkg23 network security threats pkg
23 network security threats pkgUmang Gupta
 
Chapter8 27 nov_2010
Chapter8 27 nov_2010Chapter8 27 nov_2010
Chapter8 27 nov_2010Umang Gupta
 
Lecture43 network security
Lecture43 network securityLecture43 network security
Lecture43 network securityUmang Gupta
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sqlUmang Gupta
 
Graph theory narsingh deo
Graph theory narsingh deoGraph theory narsingh deo
Graph theory narsingh deoUmang Gupta
 
Computer organiztion6
Computer organiztion6Computer organiztion6
Computer organiztion6Umang Gupta
 
Computer organiztion4
Computer organiztion4Computer organiztion4
Computer organiztion4Umang Gupta
 
Computer organiztion3
Computer organiztion3Computer organiztion3
Computer organiztion3Umang Gupta
 
Computer organiztion2
Computer organiztion2Computer organiztion2
Computer organiztion2Umang Gupta
 
Computer organiztion1
Computer organiztion1Computer organiztion1
Computer organiztion1Umang Gupta
 
Computer organiztion5
Computer organiztion5Computer organiztion5
Computer organiztion5Umang Gupta
 
Angle modulation
Angle modulationAngle modulation
Angle modulationUmang Gupta
 
periodic functions and Fourier series
periodic functions and Fourier seriesperiodic functions and Fourier series
periodic functions and Fourier seriesUmang Gupta
 
fourier transforms
fourier transformsfourier transforms
fourier transformsUmang Gupta
 
Communication systems
Communication systemsCommunication systems
Communication systemsUmang Gupta
 

Mehr von Umang Gupta (17)

23 network security threats pkg
23 network security threats pkg23 network security threats pkg
23 network security threats pkg
 
Lect13 security
Lect13   securityLect13   security
Lect13 security
 
Chapter8 27 nov_2010
Chapter8 27 nov_2010Chapter8 27 nov_2010
Chapter8 27 nov_2010
 
Lecture43 network security
Lecture43 network securityLecture43 network security
Lecture43 network security
 
11. transaction sql
11. transaction sql11. transaction sql
11. transaction sql
 
Graph theory narsingh deo
Graph theory narsingh deoGraph theory narsingh deo
Graph theory narsingh deo
 
Computer organiztion6
Computer organiztion6Computer organiztion6
Computer organiztion6
 
Computer organiztion4
Computer organiztion4Computer organiztion4
Computer organiztion4
 
Computer organiztion3
Computer organiztion3Computer organiztion3
Computer organiztion3
 
Computer organiztion2
Computer organiztion2Computer organiztion2
Computer organiztion2
 
Computer organiztion1
Computer organiztion1Computer organiztion1
Computer organiztion1
 
Computer organiztion5
Computer organiztion5Computer organiztion5
Computer organiztion5
 
Angle modulation
Angle modulationAngle modulation
Angle modulation
 
periodic functions and Fourier series
periodic functions and Fourier seriesperiodic functions and Fourier series
periodic functions and Fourier series
 
fourier transforms
fourier transformsfourier transforms
fourier transforms
 
Basic antenas
Basic antenasBasic antenas
Basic antenas
 
Communication systems
Communication systemsCommunication systems
Communication systems
 

Kürzlich hochgeladen

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfSpandanaRallapalli
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Mark Reed
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxChelloAnnAsuncion2
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPCeline George
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designMIPLM
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxnelietumpap1
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...Postal Advocate Inc.
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxMaryGraceBautista27
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxCarlos105
 

Kürzlich hochgeladen (20)

Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
ACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdfACC 2024 Chronicles. Cardiology. Exam.pdf
ACC 2024 Chronicles. Cardiology. Exam.pdf
 
Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)Influencing policy (training slides from Fast Track Impact)
Influencing policy (training slides from Fast Track Impact)
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptxGrade 9 Q4-MELC1-Active and Passive Voice.pptx
Grade 9 Q4-MELC1-Active and Passive Voice.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
How to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERPHow to do quick user assign in kanban in Odoo 17 ERP
How to do quick user assign in kanban in Odoo 17 ERP
 
Keynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-designKeynote by Prof. Wurzer at Nordex about IP-design
Keynote by Prof. Wurzer at Nordex about IP-design
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
Q4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptxQ4 English4 Week3 PPT Melcnmg-based.pptx
Q4 English4 Week3 PPT Melcnmg-based.pptx
 
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
USPS® Forced Meter Migration - How to Know if Your Postage Meter Will Soon be...
 
Raw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptxRaw materials used in Herbal Cosmetics.pptx
Raw materials used in Herbal Cosmetics.pptx
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Science 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptxScience 7 Quarter 4 Module 2: Natural Resources.pptx
Science 7 Quarter 4 Module 2: Natural Resources.pptx
 
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptxBarangay Council for the Protection of Children (BCPC) Orientation.pptx
Barangay Council for the Protection of Children (BCPC) Orientation.pptx
 

Advanced data structures and implementation

  • 1. Advanced Data Structures and Implementation • • • • • • • • • Top-Down Splay Trees Red-Black Trees Top-Down Red Black Trees Top-Down Deletion Deterministic Skip Lists AA-Trees Treaps k-d Trees Pairing Heaps
  • 2. Top-Down Splay Tree • Direct strategy requires traversal from the root • • • down the tree, and then bottom-up traversal to implement the splaying tree. Can implement by storing parent links, or by storing the access path on a stack. Both methods require large amount of overhead and must handle many special cases. Initial rotations on the initial access path uses only O(1) extra space, but retains the O(log N) amortized time bound.
  • 3.
  • 4. Case 1: Zig X L R L R Y X Y XR YL YL Yr If Y should become root, then X and its right sub tree are made left children of the smallest value in R, and Y is made root of “center” tree. Y does not have to be a leaf for the Zig case to apply. Yr XR
  • 5.
  • 6. Case 2: Zig-Zig X L Y R L R Z Y XR Z ZL X Zr YR YR ZL Zr The value to be splayed is in the tree rooted at Z. Rotate Y about X and attach as left child of smallest value in R XR
  • 7.
  • 8. Case 3: Zig-Zag (Simplified) X L Y R X YL Z ZL R Y XR YL L Zr Z ZL XR Zr The value to be splayed is in the tree rooted at Z. To make code simpler, the Zig-Zag rotation is reduced to a single Zig. This results in more iterations in the splay process.
  • 9. Splay Trees Implementation • See page 457 for generic SplayTree class implementation. • Page 458 shows Top-Down splaying method. • Refer to page 459 for insertion method into the Top-Down Splay Tree. • Page 460 shows deletion method from the tree.
  • 10. Reassembling the Splay Tree L X X R L XL R XR XL XR When the value to be splayed to the root is at the root of the “center” tree, we have reached the point where we are ready to reassemble the tree. This is accomplished by a) making XL the right child of the maximum element in L, b) making XR the left child of the minimum element in R, and then making L and R the left and right children of X
  • 11.
  • 12. Operation 1: Zig-Zig L R A L Ar B Dl F H Gl G Fl X Hl H Gl Xl Xr X Hl Xl Rotate B around A and make L child of minimum element in R (which is now empty) Ar G Fl Er F Br Er E Dl A E Cr D B Cr D Br C R C Xr L is still empty, and R is now the tree rooted at B. Note that R contains nodes > X but not in the right subtree of X.
  • 13. Operation 2: Zig-Zag L R C B Cr D Er F Br Dl Fl Cr H Br X Hl H A C G Gl Gl Xl Xr X Hl Xl Just perform Zig (simplified Zig-Zag) B Er F Ar G Fl R E D A E Dl L Xr L was previously empty and it now consists of node D and D’s left subtree Ar
  • 14. After X reaches root: L D Dl Xr Xl B G A C H F Fl R X Gl Cr E Hl Br Ar Er Reassemble – XL becomes right sub tree of H, XR becomes left sub tree of E, and then L, R reattached to X X B D Dl G H Gl A C F Fl This configuration was achieved by doing Zig Zig (of F, G) followed by a Zig (node H) Hl Cr E Xl Er Br Ar Note that this is not the same tree as was obtained by doing BU splaying.
  • 15.
  • 16. Red-Black Tree • • • • • • • • Popular alternative to the AVL tree. Operations take O(log N) time in worst case. Height is at most 2log(N+1). A red-black tree is a binary search tree with one extra attribute for each node: the color, which is either red or black. The root is black. If node is red, its children must be black . Every path from a node to a null reference must contain the same number of black nodes . Basic operations to conform with rules are color changes and tree rotations.
  • 17. Theorem 1 – In a red-black tree, at least half the nodes on any path from the root to a leaf must be black. Proof – If there is a red node on the path, there must be a corresponding black node.
  • 18. Theorem 2 – In a red-black tree, no path from any node, N, to a leaf is more than twice as long as any other path from N to any other leaf. Proof: By definition, every path from a node to any leaf contains the same number of black nodes. By Theorem1, a least ½ the nodes on any such path are black. Therefore, there can no more than twice as many nodes on any path from N to a leaf as on any other path. Therefore the length of every path is no more than twice as long as any other path.
  • 19. Theorem 3 – A red-black tree with n internal nodes has height h <= 2 lg(n + 1). Proof: Let h be the height of the red-black tree with root x. By Theorem 1, bh(x) >= h/2 From Theorem 1, n >= 2bh(x) - 1 Therefore n >= 2 h/2 – 1 n + 1 >= 2h/2 lg(n + 1) >= h/2 2lg(n + 1) >= h
  • 20. Bottom-Up Insertion • • • • • Cases: 0: X is the root – color it black 1: Both parent and uncle are red – color parent and uncle black, color grandparent red, point X to grandparent, check new situation 2 (zig-zag): Parent is red, but uncle is black. X and its parent are opposite type children – color grandparent red, color X black, rotate left on parent, rotate right on grandparent 3 (zig-zig): Parent is red, but uncle is black. X and its parent are both left or both right children – color parent black, color grandparent red, rotate right on grandparent
  • 21. Top-Down Red-Black Trees • In T-Down insertion, the corrections are done • • • while traversing down the tree to the insertion point. When the actual insertion is done, no further corrections are needed, so no need to traverse back up the tree. So, T-Down insertion can be done iteratively which is generally faster. Insertion is always done as a leaf (as in ordinary BST insertion).
  • 22. Process • On the way down, when we see a node X that • • has two red children, we make X red and its two children black. If X’s parent is red, we can apply either the single or double rotation to keep us from having two consecutive red nodes. X’s parent and the parent’s sibling cannot both be red, since their colors would already have been flipped in that case.
  • 23. Example: Insert 45 30 15 85 60 20 10 5 70 Two red children 50 40 65 55 80 90
  • 24. Example (Cont.) 30 15 10 5 70 85 60 20 flip colors two red nodes 50 40 65 55 80 90
  • 25. Example (Cont.): Do a single rotation 30 15 10 5 60 20 70 50 40 55 85 65 80 90
  • 26. Example (Cont.): Now Insert 45 30 15 10 5 60 20 70 50 40 55 45 85 65 80 90
  • 27. Note • Since the parent of the newly inserted node was • • black, we are done. Had the parent of the inserted node been red, one more rotation would have had to be performed. Although red-black trees have slightly weaker balancing properties, their performance in experimentally almost identical to that of AVL trees.
  • 28. Implementation of Top-Down RedBlack Trees • See pages 464-467
  • 29. Top-Down Deletions • Recall that in deleting from a binary search tree, the only • • • • nodes which are actually removed are leaves or nodes with exactly one child. Nodes with two children are never removed. Their contents are just replaced. If the node to be deleted is red, there is no problem - just delete the node. If the node to be deleted is black, its removal will violate property. The solution is to ensure that any node to be deleted is red.
  • 30. Deterministic Skip Lists • A probabilistically balanced • • • linked list. Invented in 1986 by William Pugh. Definition: Two elements are linked if there exists at least one link going from one to another. Definition: The gap size between two elements linked at height h is equal to the number of elements of height h-1 between them.
  • 31. Skip List d) xtra pointers every eighth item - full structure 3 6 7 9 12 17 21 19 25 26 NIL e) skip list - same link distribution, random choice 6 3 7 9 12 17 19 21 25 NIL 26
  • 32. Search time • In the deterministic version (a-d): – – – – in a, we need to check at most n nodes in b, at most n/2+1 nodes in c, at most n/4+2 nodes in general, at most log N nodes • Efficient search, but impractical insertion and deletion.
  • 33. Levels • A node with k forward level • pointers is called a level k node. If every (2i)th node has a pointer 2i nodes ahead, they have the following distribution: percent 1 50 2 25 3 12.5 … …
  • 34. Central idea in skip lists • Choose levels of nodes randomly, but in the same proportions (as in e). • A node’s i th forward pointer, points to the next node of level i or higher. • Insertions and deletions require only local modifications. • A node’s level never changes after first being chosen.
  • 35. Insertion • To perform insertion, we must make sure that when a new node of height h is added, it doesn’t create a gap of four heights of h node (in 1-2-3 deterministic skip list). • See page 269 fig. 12.19 • For implementation of Skip List see pages 472-474.
  • 36. AA-Trees • Also known as binary B-tree (BB-tree). • BB-tree is a red-black tree with one extra condition: any • node may have at most one red child. Some conditions to make it simpler (p.475): - only right child can be red - code functions recursively - instead of color store information in small integer: - one if the node is a leaf - the level of its parent, if the node is red - one less then the level of its parent, if the node is black
  • 37. Advantages • AA-trees simplify algorithms by: - eliminating half of the restructuring cases - simplifying deletion by removing an annoying case • if an internal node has only one child, that child must be a red right child • We can always replace a node with the smallest child in the right sub tree (it will either be a leaf or have a red child)
  • 38. Links in AA-tree • A horizontal link is a connection between a node • • • • and a child with equal levels. Horizontal links are right references. There cannot be two consecutive horizontal links. Nodes at level 2 or higher must have two children. If a node has no right horizontal link, its two children are at the same level.
  • 40. Insertion in AA-tree • A new item is always inserted at the bottom • • • level. In the previous example, inserting 2 will create a horizontal left link. In the previous example, inserting 45 generates consecutive right links. After inserting at the bottom level, we may need to perform rotations to restore the horizontal link properties.
  • 41. skew – remove left horizontal links P A X B P C A X B C
  • 42. split – remove consecutive horizontal links P X A P B G X A G B
  • 43. More on skew & split • skew removes a left horizontal link. • skew might also create consecutive right horizontal links. • First we must apply skew and then use split, if necessary. • After a split, the middle node increases a level, which may create a problem for the original parent.
  • 44. Implementation of AA-trees • Refer to pages 476 – 480 for detailed implementation techniques. • See page 477 & 479 for more examples of left and right rotations of AA-trees.
  • 45. Treaps • Binary search tree. • Like skip list, it uses random numbers and gives O (log N) expected time for any input. • Slower than balanced search tree. • Although deletion is much slower, it is still O (log N) expected time.
  • 46. Definition of a treap • Each node stores an item, left and right link, and • • a priority that is randomly assigned when the node is created. Treap is a binary search tree with the property that the node priorities satisfy heap order: any node’s priority must be at least as large as its parents. See pages 481-483 for implementation details.
  • 47. k – d Trees • • • • Multidimensional b-tree. Branching of odd levels is done with respect to the first key, and branching on even levels is done with respect to the second key. Root is arbitrary chosen to be an odd level. Can be visually represented:
  • 48. Some facts about k-d trees • Can have any number of dimensions. • In practice searches tend to be very efficient. • For a randomly constructed tree, the average running time of a partial match query is O (M+kN^(1-1/k)). • See pages 484-485 for implementation details.
  • 49. Pairing Heaps • A min (max) pairing heap is a min (max) tree in which operations are done in a specified manner. 8 2 1 6 5 4 1 3 3 3 4 2 1 5
  • 50. Insert • Create 1-element max tree with new item and meld with existing max pairing heap. 9 9 7 6 6 3 7 + insert(2) = 2 7 6 6 3 7
  • 51. Insert (Cont.) • Create 1-element max tree with new item and meld with existing max pairing heap. 14 9 7 6 7 9 + insert(14) = 7 6 6 6 3 3 7
  • 52. IncreaseKey (Node, theAmount) • Since nodes do not have parent fields, we cannot easily check whether the key in the Node becomes larger than that in its parent. • So, detach the Node from sibling doublylinked list and meld.
  • 53. IncreaseKey (Node, theAmount) 9 theNode 6 4 3 5 2 4 2 4 1 2 3 6 1 3 • If theNode is not the root, 1 remove sub tree rooted at theNode from its sibling list.
  • 56. Pairing heaps • See pages 488 – 491 for implementation details.