
Title:
0306 Splay Tree

Description:

We'll that's enough abstract ideas for now without coverage.
¶

So, let's take a concrete look of what a coverage can do for us in practice.

How it can help us do better testing.

What are we going to do here is look at some random open source Python codes

that implements a splay tree and before we go into the details

let me briefly explain what a splay tree is.

And broadly speaking a splay tree is just a kind of binary search tree.

And a binary search tree is a tree where every node has two leaves

and it supports operations such as insert, delete, and lookup.

The main important thing about a binary search tree,

the keys have to support an order in relation.

And so an order in relation is anything that assigns a total ordering to all the keys in the space.

Just a simple example if we're using integers for keys then we can use less than for order in relation.

Or for example, if we're using words as our keys, then we can use dictionary order.

And so again, it doesn't matter what kind of a data type the key is in a binary search tree.

All that really matters is the keys we're going to use to look up

elements in the tree have this order and relation.

The way the binary search tree is going to work is,

we're going to build up a tree under the invariant that the left child of any node

always has a key that's ordered before the key of the parent node

and the right child is always ordered after the parent node using the ordering.

And so, hopefully which we can see now,

is that if we build up some sort of a large tree with this kind of shape,

we have a procedure for fast lookup.

And the way that lookup works is, when we're searching for a particular key

in the binary search tree, we only have to walk one path from the root down to the leaves.

We always know which subtree might contain the key that we're looking for,

and of course, we have to actually go down into that subtree to see if it's there,

but the point is we only have to walk one path to the tree.

And the upside is that generally, operations on this kind of a search tree,

we're going to require a number of tree operations logarithmic in the number of tree nodes.

So for example, we if we have a tree with a million nodes and if that tree that we build ends

up being relatively balanced, then usually, we're going to expect to do about log base two

of a million tree operations as part of a lookup, or insert, or delete

and so we're going to end up doing roughly 20 operations.

So, you can see some speed number of operations is far lower than a number of nodes in a tree

that this is generally considered to be an efficient kind of a data structure.