0% found this document useful (0 votes)
35 views

CSE 326 Lecture 9: Splay Trees and B-Trees What's On Our Plate Today?

As a data type, a tree has a value and children, and the children are themselves trees; the value and children of the tree are interpreted as the value of the root node and the subtrees of the children of the root node. To allow finite trees, one must either allow the list of children to be empty (in which case trees can be required to be non-empty, an "empty tree" instead being represented by a forest of zero tree

Uploaded by

Sakura2709
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

CSE 326 Lecture 9: Splay Trees and B-Trees What's On Our Plate Today?

As a data type, a tree has a value and children, and the children are themselves trees; the value and children of the tree are interpreted as the value of the root node and the subtrees of the children of the root node. To allow finite trees, one must either allow the list of children to be empty (in which case trees can be required to be non-empty, an "empty tree" instead being represented by a forest of zero tree

Uploaded by

Sakura2709
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CSE 326 Lecture 9: Splay Trees and B-Trees

! Whats on our plate today?

" Splaying: Examples and Run Time Analysis " B-Trees


# Insert/Delete Examples and Run Time Analysis

" Introduction to Heaps and Priority Queues


! Covered in Chapters 4 and 6 in the text

R. Rao, CSE 326

Splay Trees Recap


Splay trees are binary search trees that: 1. Are not perfectly balanced all the time 2. Allow each access to a node to balance the tree so that future operations may run faster Main Ideas: - After node X is accessed, perform splaying operations to bring X up to the root using rotations. - Side Effect: Tends to leave the tree more balanced. - Net Result: Can prove that average (amortized) run time = O(log N) per access over a sequence of ADT operations
R. Rao, CSE 326 2

Splay Tree Node and Splay Z/ZZ Operations


1. Nodes must contain a parent pointer.

element left

right

parent

2. When X is accessed, apply one of six rotation operations: Single Rotations (X has a Parent but no Grandparent) zig-left, zig-right Double Rotations (X has both a Parent and a Grandparent) zig-zig-left, zig-zig-right zig-zag-left, zig-zag-right
R. Rao, CSE 326 3

Zig-Right
! Zig-Right is just a single right rotation, as in an AVL tree ! Suppose R was the node that was accessed (e.g. using Find)

Zig-right

! Zig-right moves R to the top


R. Rao, CSE 326

can access R faster next time


4

Zig-Left
! Suppose Q is accessed (e.g. using Find)

Zig-left

! Zig-left is a single left rotation: moves Q to the top


R. Rao, CSE 326 5

Zig-Zig
! Zig-Zig consists of two single rotations of the same type

(assume R is the node that was accessed):

Zig (Zig-right)

Zig (Zig-right)

! Note: Parent-Grandparent rotated first ! Zig-Zig Left is just Zig-Left followed by Zig-Left
R. Rao, CSE 326 6

Zig-Zag
! Zig-Zag consists of two rotations of the opposite type

(assume R is the node that was accessed):

Zig (Zig-left)

Zag (Zig-right)

! Note: R and Parent rotated first, R and Grandparent next ! The other Zig-Zag is just Zig-Right followed by Zig-Left
R. Rao, CSE 326 7

Splay Trees: Example

R. Rao, CSE 326

Splaying during Other Operations


! Splaying can be done not just after Find, but also after other

ADT operations such as Insert/Delete.


! Insert X: After inserting X at a leaf node (as in a regular

BST), splay X up to the root


! Delete X: Do a Find on X and get X up to the root. Delete X

at the root and move the largest item in its left subtree to the root using splaying.
! Note on Find X: If X was not found, splay the leaf node that

the Find ended up with to the root.

R. Rao, CSE 326

Analysis of Splay Trees: Amortization


Examples suggest that splaying causes tree to get balanced. The actual analysis is rather advanced and is in Chapter 11. Result of Analysis: Any sequence of M operations on a splay tree of size N takes O(M log N) time. So, the amortized running time for one operation is O(log N). This guarantees that even if depths of some nodes get very large, you cannot get a long sequence of O(N) operations. Without splaying, total time could be O(MN).
R. Rao, CSE 326 10

Beyond Binary Search Trees: Multi-Way Trees


! B-tree of order 3: Tree has 2 or 3 children per node

13:-

Keys
6:11 17:-

Data

3 4

6 7 8

11 12

13 14

17 18

! Example: Search for 8


R. Rao, CSE 326 11

B-Trees
B-Trees are multi-way search trees commonly used in database systems or other applications where data is stored externally on disks and keeping the tree shallow is important. A B-Tree of order M has the following properties: 1. The root is either a leaf or has between 2 and M children. 2. All nonleaf nodes (except the root) have between !M/2" and M children. 3. All leaves are at the same depth. All data records are stored at the leaves. Leaves store between !L/2" and L data records. L depends on disk block size and data record size (e.g. L = M).
R. Rao, CSE 326 12

B-Tree Details
Each internal node of a B-tree has: " Between !M/2" and M children. " up to M-1 keys k1 # k2 #$%%%$# kM-1

k1

...

ki-1

ki . . .

kM-1

Keys are ordered so that: k1 # k2 #$%%%$# kM-1


R. Rao, CSE 326 13

Properties of B-Trees
k1. . . ki-1 k i . . .k M-1 T 1 ... T i ... T M

Children of each internal node are "between" the items in that node. Suppose subtree Ti is the ith child of the node: All keys in Ti must be between ki-1 and ki
i.e. ki-1

&$Ti #$ki

ki-1 is the smallest key in Ti All keys in first subtree T1 #$k1 All keys in last subtree TM '$kM-1
R. Rao, CSE 326 14

B-trees Example
! B-tree of order 3: also known as 2-3 tree (2 to 3 children)

13:6:11 17:-

- means empty slot

3 4

6 7 8

11 12

13 14

17 18

! Apply B-tree definition for order M = 3 and L = M = 3

" Each node must have at least !M/2" = 2 and at most M = 3 children " Leaves store between !M/2" = 2 and M = 3 data records
15

R. Rao, CSE 326

Inserting Items in B-Trees


! Insert X: Do a Find on X and find appropriate leaf node

" If leaf node is not full, fill in empty slot with X. E.g. Insert 5 in the tree below " If leaf node is full, split leaf node and adjust parents up to root node. E.g. Insert 9 in the tree below
13:6:11 17:-

3 4 R. Rao, CSE 326

6 7 8

11 12 -

13 14 -

17 18 16

Deleting Items in B-Trees


! Delete X: Do a Find on X and delete value from leaf node

" May have to combine leaf nodes and adjust parents up to root node if number of data items falls below !M/2" = 2 E.g. Delete 17 in the tree below
13:6:11 17:-

3 4 -

6 7 8

11 12 -

13 14 -

17 18 -

R. Rao, CSE 326

17

Run Time Analysis of B-Tree Operations


! For a B-Tree of order M

1. Each internal node has up to M-1 keys to search 2. Each internal node has between !M/2" and M children i.e. Depth of B-Tree storing N data items is O(log !M/2" N) (Why? Hint: Draw a B-tree with minimum children at each node. Count its leaves as a function of depth)
! Find: Run time is:

O(log M) to binary search which branch to take at each node Total time to find an item is O(depth*log M) = O(log N)

R. Rao, CSE 326

18

What about Insert/Delete?


! For a B-Tree of order M

Depth of B-Tree storing N items is O(log !M/2" N)


! Insert and Delete: Run time is:

" O(M) to handle splitting or combining keys in nodes " Total time is O(depth*M) = O((log N/log !M/2" )*M) = O((M/log M)*log N)
! Tree in internal memory ! Tree on Disk

M = 3 or 4

M = 32 to 256. Interior and leaf nodes fit on allows very fast access to data in large
19

1 disk block. " Depth = 2 or 3 databases.


R. Rao, CSE 326

Summary of Search Trees


! Problem with Search Trees: Must keep tree balanced to allow

fast access to stored items


! AVL trees: Insert/Delete operations keep tree balanced ! Splay trees: Sequence of operations produces balanced trees ! Multi-way search trees (e.g. B-Trees): More than two children

per node allows shallow trees; all leaves are at the same depth keeping tree balanced at all times

R. Rao, CSE 326

20

Next Class: Heaps on Heaps To Do: Read Chapter 6 Homework # 2 (due this Friday)

R. Rao, CSE 326

21

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy