Lecture 6 continued

Search Tables

An ordered map implemented by means of a sorted list. Stores key-value pairs

Search tables performance:

Binary search trees

search tree image 1 search tree image 2
Alt text Alt text

Search for key kk

Intermediate step result, no node
Alt text Alt text

Insertion

Example: insert 5

Alt text

Deletion

remove(k)

Example: remove 4

Alt text

Example: remove 3

Alt text

Example: What steps would be involved in deleting the node with key=58?

before after
Alt text Alt text

Performance

What is I want to store duplicate keys?

BST vs Binary Search in Arrays

AVL Trees

Balanced binary search tree

Alt text

Height-Balance property: for every internal node v, the heights of the children of v differ by at most 1

Insertion (AVL)

Alt text

Alt text

Tri-node restructuring

Before after
Alt text Alt text

Tri-node Restructuring (Case 2)

Step 1 Step 2 Step 3
Alt text Alt text Alt text

There are all mirror symmetric cases:

Alt text

Removal

Alt text

Balancing after a removal

Alt text

tri-node restructuring

AVL Tree Performance

Operation performance
Space O(n)O(n)
Restructuring O(1)O(1) - using a linked-structure
Searching O(logn)O(\log n)
Height O(logn)O(\log n) - no restructures needed
Insertion O(logn)O(\log n)
Removal O(logn)O(\log n) - restructuring up the tree, maintaining heights is O(logn)O(\log n)

Splay Tree

Rotations

right rotation

Right rotation left rotation
Alt text Alt text

Splaying

Method Splay Node
Search for k If key found, use that node, If key not found, use parent of ending external node
Insert(k,v) Use new node containing the entry inserted
Remove item with key k Use parent of the internal node that was actually removed, from the tree (the parent of the node that the removed, item was swapped with)

How do we splay?

Example combinations

Zag Zig zig-zig zag-zag NOTE: Zig-zag
Alt text Alt text Alt text Alt text Alt text

Ultimate table:

Alt text

Insertions with Splaying

Insert 2:

Alt text

Insert 4:

Alt text

Splaying costs

Key Ideas

Red-Black Trees

B-Trees (B+ and other variants)