Docsity

Dynamic Sets and Binary Search Trees, Papers of Algorithms and Programming

Toggle sidebar
bg1
1
CS231 Algorithms Handout #19
Prof Lyn Turbak March 8, 2001
Wellesley College
DYNAMIC SETS
Reading: CLR Chapters 11 & 13
---------------------------------------------------------------------------------------------------------------------
Terminology
We will be studying collections of objects. In this context, an object is a record with a key field
and satellite data fields. Every object is identified by a pointer; when we say that a function
takes an object as an argument or returns one as a result, it is manipulating a pointer to the
object. In most data structures, some of the satellite data fields are pointers to other objects in the
data structure. The null pointer, or nil, is a distinguished pointer that stands for the absence of an
object; it is used to represent empty lists or trees. We use the term leaf to refer to an empty tree
and fringe node to refer to a tree node whose children are all leaves.
On the next page are pictures showing objects in the context of some of the data structures we
will be studying. Each data structure is assumed to be represented by an entry point record with
fields that refer to objects as well as auxiliary information. For example, an array has an
elements field and a length field; linked and doubly-linked lists have a head field that points to
the first object in the list; and trees have a root field that points to the root of the tree.
We will assume that new objects are created as follows:
new Array(n)
Creates a new array with n slots indexed from 1 to n.
new ListNode()
Creates a new list node with fields key, data, and next.
new DoublyLinkedListNode()
Creates a new doubly-linked list node with fields key, data, next, and prev.
new BinaryTreeNode()
Creates a new binary tree node with fields key, data, left, right, and parent.
---------------------------------------------------------------------------------------------------------------------
pf3
pf4
pf5
pf8

Partial preview of the text

Download Dynamic Sets and Binary Search Trees and more Papers Algorithms and Programming in PDF only on Docsity!

CS231 Algorithms Handout

Prof Lyn Turbak March 8, 2001

Wellesley College

DYNAMIC SETS

Reading: CLR Chapters 11 & 13

Terminology

We will be studying collections of objects. In this context, an object is a record with a key field

and satellite data fields. Every object is identified by a pointer ; when we say that a function

takes an object as an argument or returns one as a result, it is manipulating a pointer to the

object. In most data structures, some of the satellite data fields are pointers to other objects in the

data structure. The null pointer, or nil , is a distinguished pointer that stands for the absence of an

object; it is used to represent empty lists or trees. We use the term leaf to refer to an empty tree

and fringe node to refer to a tree node whose children are all leaves.

On the next page are pictures showing objects in the context of some of the data structures we

will be studying. Each data structure is assumed to be represented by an entry point record with

fields that refer to objects as well as auxiliary information. For example, an array has an

elements field and a length field; linked and doubly-linked lists have a head field that points to

the first object in the list; and trees have a root field that points to the root of the tree.

We will assume that new objects are created as follows:

new Array(n)

Creates a new array with n slots indexed from 1 to n.

new ListNode()

Creates a new list node with fields key, data, and next.

new DoublyLinkedListNode()

Creates a new doubly-linked list node with fields key, data, next, and prev.

new BinaryTreeNode()

Creates a new binary tree node with fields key, data, left, right, and parent.

Array Linked List

key

other data

pointer = (array, index)

key

other data

pointer

next

Doubly Linked List

key

other data

pointer

prev next

Binary Tree

key

other data

pointer

left right

parent

Asymptotic Worst-Case Running Times for Simple Implementations of Dynamic Sets

In the following table, use n to refer to the number of elements in the set and h to refer to the

height of the tree-based representations.

Search Insert Delete Maximum Minimum Successor Predecessor

Unsorted Array

Sorted Array

Unsorted

Linked List

Sorted

Linked List

Unsorted

Doubly-Linked

List

Sorted

Doubly-Linked

List

Binary Tree

Binary

Search Tree

Binary Search Trees

A binary search tree (BST) is a binary tree satisfying the binary search tree property :

If left-obj is in the left subtree of obj, key[left-obj] ≤ key[obj]

If right-obj is in the right subtree of obj, key[right-obj] ≥ key[obj]

There are many binary search trees corresponding to a given set of elements.

E.g., what are the binary search trees for {A, B, C}?

The objects in a binary search tree can be enumerated in sorted order by inorder traversal of the

tree starting at its root:

Inorder-Traverse(node, function) if node ≠ nil then Inorder-Traverse(left[node], function) function(node) Inorder-Traverse(right[node], function)

This gives rise to the Tree-Sort sorting algorithm:

Step 1: Insert all objects into a binary search tree.

Step 2: Enumerate the elements of the binary search tree via an in-order traversal.

BST Operations II

BST-Insert(tree, node) previous <- nil current <- root[tree] while current ≠ nil do previous <- current if key[node] < key[current] then current <- left[current] else current <- right[current] {current is now nil} parent[node] <- previous if previous = nil then root(tree) <- node else if key[node] < key[previous] then left[previous] <- node else right[previous] <- node

{Different from CLR version. Assumes there is a dummy header node at root.} BST-Delete(tree, node) if (left[node] = nil) or (right[node] = nil) {Case 1: at least one child of node is nil} then if node = left[parent[node]] then left[parent[node]] <- Single-Child(node) else right[parent[node]] <- Single-Child(node) {Case 2: both children of node are non-nil.} else succ <- BST-Successor(tree, node) {Delete succ from current position. Guaranteed to use Case 1.} BST-delete(tree, succ) {Splice succ into position of node} parent[left[node]] <- succ left[succ] <- left[node] parent[right[node]] <- succ right[succ] <- right[node] parent[succ] <- parent[node] if node = left[parent[node]] then left[parent[node]] <- succ else right[parent[node]] <- succ

{If one child is nil, returns the other one. Returns nil if both children are nil.} Single-Child(node) if left[node] = nil then return right[node] else return left[node]

Notes

All seven BST operations have Θ(h) worst-case running times, where h is the height of the tree.

What is the relationship between the height h and the number of elements n?

Best case:

Worst case:

Average case:

For the average case, assume that trees are formed by sequentially inserting the elements of a

randomly permuted array of n objects into an initially empty BST. Note that there is a close

correspondence between the resulting tree and the partitionings of quick sort.