Professional Documents
Culture Documents
BT BST Heap Java
BT BST Heap Java
// DSutil.java
import java.util.*;
// Elem.java
// Elem interface. This is just an Object with support for a key field.
interface Elem { // Interface for generic element type
public abstract int key(); // Key used for search and ordering
} // interface Elem
// IElem.java
// Sample implementation for Elem interface: a record w/ just an int field
public class IElem implements Elem {
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
Binary Trees
A binary tree is made up of a finite set of nodes that is either empty or consists
of a node called the root together with two binary trees, called the left and
right subtrees, which are disjoint from each other and from the root.
Full binary tree: Each node is either a leaf or internal node with exactly two
non-empty children.
Complete binary tree: is a binary tree in which every level, except possibly the
last, is completely filled, and all nodes are as far left as possible.
Traversals
Any process for visiting the nodes in some order is called a traversal.
Any traversal that lists every node in the tree exactly once is called an
enumeration of the tree’s nodes.
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
// BinNode.java
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
// Main.java
public class Main {
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
Binary Search Tree (BST)
Lists have a major problem:
either insert/delete, on the one hand, or search, on the other, must be O(n)
time.
How can we make both update and search efficient ?
Answer: Use a new data structure...BST
BST Property
1. Key elements in the left subtree of a node with value K have key
values < K
2. Key elements in the right subtree of a node with value K have key
values >= K
printhelp(rt.left(), level+1);
printhelp(rt.right(), level+1);
}
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
public Elem find(Elem key)
{ return findhelp(root, key); }
// First find where the key “val” would have been if it were in
// the tree:
// 1) a leaf node or
// 2) an internal node with one child
// Then add a new node with key “val”.
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
// Routines to get and remove the node with the smallest key.
// A node with the minimum key value will always be positioned as
// a left leaf of the BST, even in case of keys having duplicate
// values.
private Elem getmin(BinNode rt) {
if (rt.left() == null)
return (Elem)rt.element();
else return getmin(rt.left());
}
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
// Removing an arbitrary node R from the BST requires that:
// (1) we first find R
// (2) we remove it from the tree taking care of the following cases:
// -- If R has no children then the pointer of Parent(R) is set to NULL
// -- If R has one child then the pointer of Parent(R) is set to R’s child
// -- If R has two children:
// Find a value in one of the two subtree that can replace R
// preserving the BST property...that is substitute R with
// the least value of the right subtree (which is the In-Order Successor)
// (in such a way we pick up a value that is less than others on the
// right and is also greater than all nodes on the left of the tree)
// we’ll make use of the previous method : BinNode deletemin(BinNode rt)
// (Preferred if the tree contains duplicates because of the convention
// about the insertion of duplicates)
// OR
//the greatest value of the left subtree (In-Order Predecessor)
// The method returns a subtree identical to the old one except that it
// has been modified deleting a node with the minimum key
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
Complete Binary Tree
A CBT is a binary tree in which every level, except possibly the last, is
completely filled, and all nodes are as far left as possible.
Since a complete binary tree is so limited in its shape (there is only one
possible shape for n nodes), it is reasonable to expect that space efficiency can
be achieved with an array representation.
[0]
[1] [2]
Position 0 1 2 3 4 5 6 7 8 9 10 11
Parent -- 0 0 1 1 2 2 3 3 4 4 5
Left Child 1 3 5 7 9 11 -- -- -- -- -- --
Right Child 2 4 6 8 10 -- -- -- -- -- -- --
Left Sibling -- -- 1 -- 3 -- 5 -- 7 -- 9 --
Right Sibling -- 2 -- 4 -- 6 -- 8 -- 10 -- --
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
HEAP
Definition:
Complete binary tree with the heap property:
• Max-heap: every node store a value that is greater than or equal to the
values of either of its children.
• Min-heap: every node store a value that is less than or equal to the values
of either of its children.
Heap representation:
normally, the array-based “complete binary tree” (CBT) representation.
[0]
[1] [2]
[7] [8]
Obs.: In a CBT the leafs are positioned in the second half of the array.
In the above example with n = 9 the interval of the array positions storing a
leaf value is [n/2, n-1] = [4,8].
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
public class MaxHeap {
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
// Inserting a value in a Max-Heap
// X = 15
}
}
}
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
// Removing the max value in a Max-Heap
// The procedure starts by swapping it with the last element on the last
// level. So, if we have the same max-heap as before, we remove the 11 and
// replace it with the 4
//
// Now the heap property is violated since 8 is greater than 4.
// The operation that restores the property is called sift-down.
// In this case, swapping the two elements 4 and 8, is enough to restore
// the heap property and we need not swap elements further.
//
// In general, the wrong node is swapped with its larger child in
// a max-heap (in a min-heap it would be swapped with its smaller child),
// until it satisfies the heap property in its new position.
//
// Note that the down-heap operation (without the preceding swap) can be
// used in general to modify the value in any position ...siftdown(pos).
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
// Building a Heap
// sift up
while (pos != 0 && Heap[pos].key() > Heap[parent(pos)].key()) {
DSutil.swap(Heap, pos, parent(pos));
pos = parent(pos);
}
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer
HeapSort
• Strategy
– Build an Heap with the array elements
– Removes the heap's root (the largest element) by exchanging it with the heap’s last
element
– Transforms the resulting semi-heap back into a heap
• Efficiency
– Compared to mergesort
• Both heapsort and mergesort are O(n log n)
in both the worst and average cases
• Advantage over mergesort
– Heapsort does not require a second array
– Compared to quicksort
• Quicksort performs better in the average case
(empirically: for a constant factor)
// Source code examples based on "A Practical Introduction to Data Structures and Algorithm
// Analysis" by Clifford A. Shaffer, Prentice Hall, 1998. Copyright 1998 by Clifford A. Shaffer