HELP/FAQ


Balanced Trees

Chapter 10

Heap

A heap is a binary tree such that

- the data contained in each node is greater than (or equal to) the data in that node’s children.

- the binary tree is complete

Is it a heap?

Is it a heap?

Is it a heap?

Storage of a heap

Use an array to hold the data.

Store the root in position 0.

For any node in position i,

- its left child (if any) is in position 2i + 1

- its right child (if any) is in position 2i + 2

- its parent (if any) is in position (i-1)/2    (integer division)

Basic operations on a heap

Create an empty heap (constructor).

Insert a data element into a heap.

Remove the maximum data element from the heap.

The heap data structure

public class Heap {

    private int[] data;

    private int heapSize;

    private int maxSize;

    public Heap(int maximumSize) {

        if (maximumSize < 1) maxSize = 100;

        else maxSize = maximumSize;

        data = new int[maxSize];

    heapSize = 0;

    }

    public boolean isEmpty() // not

    public boolean isFull()  // shown

    // other methods

}

Inserting into a heap

Place the new element in the first available position in the array.

Compare the new element with its parent. If the new element is greater, than swap it with its parent.

Continue this process until either

- the new element’s parent is greater than or equal to the new element, or

- the new element reaches the root

Inserting into a heap

Inserting into a heap

public void insert(int item) {

    int position;

    if (isFull()) throw new Exception();

    heapSize++;

    data[heapSize-1] = item;

    position = heapSize – 1;

    while (position > 0 && 
        data[position] > data[(position-1)/2])
    {

        swap(position, (position-1)/2);
        position = (position-1) / 2;
    }

}

Removing from a heap

Place the root element in a variable to return later.

Move the last element in the deepest level to the root (reduce the size of the heap by 1).

While the moved element has a value lower than one of its children, swap this value with the highest-valued child.

Return the original root that was saved.

Removing from a heap

Removing from a heap

public int remove() {

    int answer;

    if (isEmpty()) throw new Exception();

    answer = data[0];

    data[0] = data[heapSize–1];

    heapSize--;

    fixheap();

    return answer;

}

Removing from a heap (cont’d)

private void fixheap() {

    int position = 0; 
    int childPos;

    while (position*2 + 1 < heapSize) {

        childPos = position*2 + 1;

        if (childPos < heapSize-1 &&
                    data[childPos+1] > data[childPos])
            childPos++;

        if (data[position]>= data[childPos]) 
            return;

        swap(position, childPos);

        position = childPos;

    }

}

Efficiency of heaps

Assume the heap has N nodes.

Then the heap has approximately log2N levels.

Insert:

Since the insert swaps at most once per level, the order of complexity of insert is O(log N)

Remove:

Since the remove swaps at most once per level, the order of complexity of remove is also O(log N)

Priority Queues

A priority queue PQ is like an ordinary queue except that we can only remove the "maximum" element at any given time (not the "front" element necessarily).

If we use an array for the PQ, enqueue takes O(1) time but dequeue takes O(n) time.

If we use a sorted array for the PQ, enqueue takes O(n) time, while dequeue takes O(1) time.

We can use a heap to implement a priority queue, so that enqueue and dequeue take O(log N) time.

B-trees

A B-tree is a balanced search tree.

A B-tree is designed to work well on magnetic disks and other direct-access secondary storage devices by minimizing disk I/O operations.

A node can hold more than one item.

A node can have more than two children.

B-tree rules

The root may have as few as one element. Every other node has at least MINIMUM elements.

The maximum number of elements in a node is twice the value of MINIMUM.

The elements of each B-tree node are stored in a partially filled array, sorted from the smallest element (in position 0) to the largest element.

B-tree rules (cont’d)

The number of subtrees below a non-leaf node is always one more than the number of elements in the node.

For any non-leaf node, an element at
index i is

(a) greater than all the elements in subtree i of the node, and
(b) less than all the elements in subtree i+1 of the node

Every leaf in a B-tree has the same depth.

General Idea

Sample B-tree
MINIMUM = 2

Searching in a B-tree

Searching for an item in the current node:

- Let k = number of data values in this node

- Let i = the first index such that data[i] >= item. (If every data value in the node is < item, set i = k+1.)

- If item = data[i], return true

- Else if node has no children, return false

- Else call search recursively on subtree[i]

2-3-4 Trees & Red-Black Trees

A 2-3-4 tree is similar to a B-tree where each node can hold 1, 2 or 3 data values.

A red-black tree is a special implementation of a 2-3-4 tree such that all nodes are binary.

Red-black tree rules:

- Every node is colored either red or black.

- The root is black.

- If a node is red, its children must be black.

- Every path from a node to a null link must contain the same number of black nodes.

2-3-4 Trees & Red-Black Trees

Sample Red-Black Tree

 


Course Info | Schedule | Sections | Announcements | Homework | Exams | Help/FAQ | Grades | HOME