Searching-Symbol Tables - bst ,2-3 search trees and Red-black bst

117 阅读2分钟

binary search tree

Definition. A binary search tree (BST) is a binary tree where each

node has a Comparable key (and an associated value) and satisfies

the restriction that the key in any node is larger than the keys in all

in that node’s right subtree

image.png

deletion in a bst

image.png

image.png

Balanced Search Trees

2-3 search trees

The primary step to get the flexibility that we need to guarantee balance in search trees is to allow the nodes in our trees to hold more than one key. Specifically, referring to the nodes in a standard BST as 2-nodes (they hold

and two keys. Both 2-nodes and 3-nodes have one link for each of the

intervals subtended by its keys.

Definition. A 2-3 search tree is a tree that is either empty or

• A 2-node, with one key (and associated value) and two links, a left link to a 2-3 search tree with smaller keys, and a right link to a 2-3 search tree with larger keys

• A 3-node, with two keys (and associated values) and three links, a left link to a 2-3 search tree with smaller keys, a middle link to a 2-3 search tree with keys between the node’s keys, and a right link to a 2-3 search tree with larger keys

image.png

A perfectly balanced 2-3 search tree is one whose null links are all the same distance from the root. To be concise, we use the term 2-3 tree to refer to a perfectly balanced 2-3 search tree

Insert into a 2-node

It is easy to accomplish this task if the node at which the search terminates is a

2-node: we just replace the node with a 3-node containing its key and the

new key to be inserted. If the node where the search terminates is a 3-node,

we have more work to do.

Insert into a tree consisting of a single 3-node

image.png

Insert into a 3-node whose parent is a 2-node

image.png

Insert into a 3-node whose parent is a 3-node

image.png

Splitting the root

If we have 3-nodes along the whole path from the insertion point to the

root, we end up with a temporary 4-node at the root. In this case we can

proceed in precisely the same way as for insertion into a tree consisting of

a single 3-node. We split the temporary 4-node into three 2-nodes,

increasing the height of the tree by 1. Note that this last transformation

preserves perfect balance because it is performed at the root.

Red-black BSTs

Encoding 3-nodes

The basic idea behind red-black BSTs is to encode 2-3 trees by starting

with standard BSTs (which are made up of 2-nodes) and adding extra

information to encode 3-nodes. We think of the links as being of two

different types: red links, which bind together two 2-nodes to represent 3-

nodes, and black links, which bind together the 2-3 tree. Specifically, we

represent 3-nodes as two 2-nodes connected by a single red link that leans left (one of the 2-nodes is the left child of the other). One advantage of

using such a representation is that it allows us to use our get() code for

standard BST search without modification. Given any 2-3 tree, we can

immediately derive a corresponding BST, just by converting each node as

specified. We refer to BSTs that represent 2-3 trees in this way as red-

black BSTs.

image.png

An equivalent definition

Another way to proceed is to define red-black BSTs as BSTs having red

and black links and satisfying the following three restrictions:

• Red links lean left.

• No node has two red links connected to it.

• The tree has perfect black balance: every path from the root to a null link has the same number of black links.

There is a 1-1 correspondence between red-black BSTs defined in this way and 2-3 trees.

Color representation

we encode the color of links in nodes, by adding a boolean

instance variable color to our Node data type, which is true if the link

from the parent is red and false if it is black.

By convention, null links are black