Professional Documents
Culture Documents
Chapter 21
Chapter 21
21 - 2
• Given a linked list, determine if it has a cycle in it.
Source: https://www.tutorialspoint.com/data_structures_algorithms/heap_data_structure.htm
21 - 12
Maxheap
Source: https://en.wikipedia.org/wiki/Heap_(data_structure)
21 - 13
• Two minheaps containing the same data:
21 - 19
– Can you find the path from the root to the parent of
the insertion position?
– Idea: Use of the number of nodes – numNodes
– Given numNodes, how to know whether the new
node will become the left child or the right child?
– Given id (i.e., position), how to get the id of the
parent node? 0
1 2
3 4 5 6
7 21 - 20
int position = numNodes; // new position
String s; 0
1 2
s = position + "";
while (position > 0) {
3 4 5 6
if (???)
position = position / 2;
7
else
??? 1:
0,1
s = position + "," + s;
5:
} 0,2,5
String positions[] = s.split(","); 6:
0,2,6
// Move from the root through positions[] 7:
0,1,3,7
...
21 - 21
+ addElement()
// When there is no node
if (numNodes == 0) {
root <- new node;
numNodes++;
return;
}
21 - 22
// Find the place where the new node goes to
// Actually the parent node
if (numNodes == 0)
return null;
// If there is only one, then remove the root and return its content
if (numNodes == 1) {
tmpRoot = root;
root = null;
numNodes = 0;
return ???
}
21 - 29
// Find the last node
NodePriorityQueue<T> node;
int position = numNodes-1;
1: 0
String s = position + "";
0,1
while (position > 0) { 5: 1 2
if (???) 0,2,5
position = position / 2; 6: 3 4 5 6
else 0,2,6
??? 7:
s = position + "," + s; 0,1,3,7 7
}
String positions[] = s.split(",");
if (???)
node.getParent().setLeft(null);
else
???
numNodes--;
root = node;
root.setParent(null);
root.setLeft(???);
???;
if (tmpRoot.getLeft() != null)
tmpRoot.getLeft().setParent(root);
???
21 - 31
// Adjust the heap
node = root;
while(true) {
if (node.getLeft() == null && node.getRight() == null) break;
else if (node.getLeft() != null && node.getRight() == null) {
if (node.getPriority() > node.getLeft()).getPriority()) swapChildParent(node.getLeft(), node);
else break;
}
else if (node.getLeft() == null && node.getRight() != null) {
if (node.getPriority() > node.getRight()).getPriority()) swapChildParent(node.getRight(), node);
else break;
}
else {
if (node.getLeft().getPriority() <= node.getRight().getPriority()) {
if (node.getPriority() > node.getLeft().getPriority()) swapChildParent(node.getLeft(), node);
else break;
}
else if (node.getLeft().getPriority() > node.getRight().getPriority()) {
if (node.getPriority() > node.getRight().getPriority()) swapChildParent(node.getRight(), node);
else break;
}
else break;
}
}
19 - 35
NodeBinaryTree.class
NodeBinaryTree<T>
- int id
- T content
- NodeBinaryTree<T> parent, left, right
+ NodeBinaryTree(T content)
+ NodeBinaryTree(int id, T content)
+ getId(): int
+ getContent(): T
+ getParent(): NodeBinaryTree<T>
+ setParent(NodeBinaryTree<T> parent): void
+ getLeft(): NodeBinaryTree<T> parent
+ setLeft(NodeBinaryTree<T> left): void
+ getRight(): NodeBinaryTree<T> id,
+ setRight(NodeBinaryTree<T> right): void
cont
ent
left right
19 - 36
NodePriorityQueue.class
NodePriorityQueue<T> extends
NodeBinaryTree<T>
- int priority
+ NodePriorityQueue(T content)
+ getPriority(): int
+ setPriority(int priority): void
19 - 37
PriorityQueue.class
PriorityQueue<T>
- NodePriorityQueue<T> root;
- int numNodes;
+ PriorityQueue(T content)
+ addElement(int priority, T element): void
+ removeMin(): T
+ findMin(): T
+ getRoot(): NodePriorityQueue<T>
+ isEmpty(): boolean
+ size(): int
- swapChildParent(NodePriorityQueue<T> child,
NodePriorityQueue<T> parent): void
19 - 38
+ addElement()
// When there is no node
if (numNodes == 0) {
root <- new node;
numNodes++;
return;
}
21 - 39
// Find the place where the new node goes to
// Actually the parent node
if (numNodes == 0)
return null;
// If there is only one, then remove the root and return its content
if (numNodes == 1) {
tmpRoot = root;
root = null;
numNodes = 0;
return ???
}
21 - 42
// Find the last node
NodePriorityQueue<T> node;
int position = numNodes-1;
1: 0
String s = position + "";
0,1
while (position > 0) { 5: 1 2
if (???) 0,2,5
position = position / 2; 6: 3 4 5 6
else 0,2,6
??? 7:
s = position + "," + s; 0,1,3,7 7
}
String positions[] = s.split(",");
if (???)
node.getParent().setLeft(null);
else
???
numNodes--;
root = node;
root.setParent(null);
root.setLeft(???);
???
if (tmpRoot.getLeft() != null)
tmpRoot.getLeft().setParent(root);
???
21 - 44
// Adjust the heap
node = root;
while(true) {
if (node.getLeft() == null && node.getRight() == null) break;
else if (node.getLeft() != null && node.getRight() == null) {
if (node.getPriority() > node.getLeft()).getPriority()) swapChildParent(node.getLeft(), node);
else break;
}
else if (node.getLeft() == null && node.getRight() != null) {
if (node.getPriority() > node.getRight()).getPriority()) swapChildParent(node.getRight(), node);
else break;
}
else {
if (node.getLeft().getPriority() <= node.getRight().getPriority()) {
if (node.getPriority() > node.getLeft().getPriority()) swapChildParent(node.getLeft(), node);
else break;
}
else if (node.getLeft().getPriority() > node.getRight().getPriority()) {
if (node.getPriority() > node.getRight().getPriority()) swapChildParent(node.getRight(), node);
else break;
}
else break;
}
}
// number of data
12
// values and priorities at the same time (strings)
45
30
60
82
22
27
35
40
51
75
55
87
21 - 46
queue = new PriorityQueue<String>();
count = 0;
while(count < numElements) {
line = bufferedReader.readLine();
line = line.trim();
if (line.length() == 0)
continue;
if (line.charAt(0) == '/' && line.charAt(1) == '/')
continue;
???(Integer.parseInt(line), line);
count++;
}
displayBFS(0);
21 - 47
static void remove()
{
if (queue.size() > 0)
{
String s = "Min";
for (int col = 0; col < s.length(); col++)
board.cellContent(2, col, "" + s.charAt(col));
displayBFS(0);
}
}
21 - 48
Run Time (Big O Notation)
• Heap implemented using links:
– addElement:
O(log n)
– removeMin:
O(log n)