Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Binary trees
• Binary search trees
• Expression trees
• Heaps
Data Structures and Algorithms in Java, Third Edition
Ch06–1
Binary tree
root
nonterminal
node
parent
child
leaf
(terminal node)
Data Structures and Algorithms in Java, Third Edition
Ch06–2
Binary search tree
el
< el
≥ el
Data Structures and Algorithms in Java, Third Edition
Ch06–3
Node implementation
public class BSTNode<T extends
Comparable<? super T>> {
protected T el;
protected BSTNode<T> left, right;
// constructors
}
el
abbreviated as:
Data Structures and Algorithms in Java, Third Edition
el
Ch06–4
Insertion in BST: algorithm
insert(el)
p = root;
prev = null;
while p is not null // find a place for inserting new node;
prev = p;
if element in node p < el
p = p.right;
else p = p.left;
if root is null
// tree is empty;
root becomes a new node with el;
else if element in node prev < el
new node with el is attached to the right of prev;
else new node with el is attached to the left of prev;
Data Structures and Algorithms in Java, Third Edition
Ch06–5
Insertion in the BST: example
20
30
20
null
15
10
25
20
20
20
30
30
25
Data Structures and Algorithms in Java, Third Edition
10
30
25
Ch06–6
Insertion in the BST: example (cont’d)
27
23
20
10
20
30
15
26
25
20
10
30
15
25
27
Data Structures and Algorithms in Java, Third Edition
10
30
15
25
23 27
Ch06–7
Searching in the BST: algorithm
search(el)
p = root;
while p is not null
if element in node p equals el
return element in node p;
else if element in node p < el
p = p.right;
else p = p.left;
return null; // el was not found;
Data Structures and Algorithms in Java, Third Edition
Ch06–8
Searching in the BST: example
28
25
20
20
10
10
30
15
25
success
30
15
25
23 27
23 27
26
26
Data Structures and Algorithms in Java, Third Edition
failure
Ch06–9
Breadth-first traversal: algorithm
public void breadthFirst() {
BSTNode<T> p = root;
Queue<BSTNode<T>> queue
= new Queue<BSTNode<T>>();
if (p != null) {
queue.enqueue(p);
while (!queue.isEmpty()) {
p = queue.dequeue();
visit(p);
if (p.left != null)
queue.enqueue(p.left);
if (p.right != null)
queue.enqueue(p.right);
}
}
}
Breadth-first traversal: example
20
10
30
15
iteration
init
1
queue
p
20
10 30
20
10
25
2
3
30 15
15 25
23 27
4
25
15
5
23 27
25
6
27
23
7
26
27
26
8
Data Structures and Algorithms in Java, Third Edition
30
26
Ch06–11
Depth-first traversals: algorithms
Three tasks:
V - visiting a node
L - traversing the left subtree
R - traversing the right subtree
The three tasks can be executed in 3! = 6 different orders:
preorder
inorder
postorder
left to right
right to left
VLR
LVR
LRV
VRL
RVL
RLV
Data Structures and Algorithms in Java, Third Edition
Ch06–12
Depth-first traversals: implementation
public void preorder() {
preorder(root);
}
protected void preorder(BSTNode<T> p) {
if (p != null) {
visit(p);
preorder(p.left);
preorder(p.right);
}
}
Data Structures and Algorithms in Java, Third Edition
Ch06–13
Depth-first traversals: example
20
10
30
15
25
23 27
preorder
20 10 15 30 25 23 27 26
inorder
10 15 20 23 25 26 27 30
postorder 15 10 23 26 27 25 30 20
26
Data Structures and Algorithms in Java, Third Edition
Ch06–14
Deletion by copying
1. Deleting a leaf
2. Deleting a node with one descendant
Data Structures and Algorithms in Java, Third Edition
Ch06–15
Deletion by copying (cont’d)
3. Deleting a node with two descendants
B
A
A
Data Structures and Algorithms in Java, Third Edition
Ch06–16
Deletion by copying: example
20
20
10
30
15
25
15
10
35
23 27
10
27
15
25
35
27
25
23 26
23 26
26
delete 30
Data Structures and Algorithms in Java, Third Edition
delete 20
Ch06–17
35
Rotations
rotateRight(G,P,C)
if P is not the root of the tree // i.e., if G is not null
1.
grandparent G of child C becomes C’s parent by
replacing P;
2. right subtree of C becomes left subtree of C’s parent P;
3. node C acquires P as its right child;
Data Structures and Algorithms in Java, Third Edition
Ch06–18
Step-by-step right rotation
G
G
G
P
P
C
X
C
Z
Y
X
P
C
Z
Y
X
1. grandparent G of child C becomes
C’s parent by replacing P;
2. right subtree of C becomes
left subtree of C’s parent P;
C
P
X
Y
G
G
C
Z
Z
Y
3. node C acquires P as its right child;
=
P
X
Y
Z
Left rotation
G
G
P
C
C
X
Y
P
Z
Data Structures and Algorithms in Java, Third Edition
X
Z
Y
Ch06–20
Self-adjusting trees
Single rotation: Rotate a child about
parent if an element in the child is
accessed unless it is the root.
Moving to the root: Repeat the
child-parent rotation until the element
being accessed is in the root.
Data Structures and Algorithms in Java, Third Edition
Ch06–21
Single rotation technique: example
8
5
3
8
9
6
6
2 4
5
7
9
7
3
2 4
6
5
3
2 4
5
8
7 9
Data Structures and Algorithms in Java, Third Edition
3
2 4
6
8
7 9
Ch06–22
Expression trees
(5 + 6) * 7
5 + 6*7
+
5
*
+
*
6
preorder
inorder
postorder
7
+5 * 6 7
5+6 * 7
567 * +
5
preorder
inorder
postorder
Data Structures and Algorithms in Java, Third Edition
7
6
* +5 6 7
5+6 * 7
56+ 7 *
Ch06–23
Processing an expression in postfix notation
5 6 7*+
5
6
5
7
6
5
Data Structures and Algorithms in Java, Third Edition
42
5
47
Ch06–24
(max) heaps
1. the value of each node is greater than or equal to
the values stored in each of its children,
2. the tree is perfectly balanced and the leaves in in
the last level are all in the leftmost positions.
Data Structures and Algorithms in Java, Third Edition
Ch06–25
Heaps: counterexamples
20
A binary tree that
violates the first
heap condition:
10
7
6
A binary tree that
violates the
second heap
condition:
Data Structures and Algorithms in Java, Third Edition
15
3
7
18
5
20
10
7
6
15
3
7
18
5
Ch06–26
binary search tree
heap
el
≤ el
el
≤ el
Data Structures and Algorithms in Java, Third Edition
< el
≥ el
Ch06–27
parent
left
child
right
child
0
1
1
3
2
4
2
5
6
3
7
8
….
….
….
i
2∙i +1
2∙i +2
20
10
7
15
3
7
10
6 5
0
1
2
3
4
5
6
7
8
20 10 15
7
3
7
10
6
5
Data Structures and Algorithms in Java, Third Edition
Ch06–28
Heap as a priority queue: enqueuing
heapEnqueue(el)
put el at the end of heap;
while el is not in the root and el > parent(el)
swap el with its parent;
Data Structures and Algorithms in Java, Third Edition
Ch06–29
Series of enqueuings
4
6
empty
7
3
4
4
6
6
4
6
6
6
4 3
4 3
7 3
7
4
4 6
5
6 4
7
6 3
6 3
7 6 3 4
6 4 3 7
6 7 3 4
4
7
4
6 4 3
4
4 5
7 6 3 4 5
7
7
6
3
4 5 4
7 6 3 4 5 4
6
4
4 5 3
7 6 4 4 5 3
Heap as a priority queue: dequeuing
heapDequeue()
extract the element from the root;
put the element from the last leaf in its place;
remove the last leaf;
// both subtrees of the root are heaps
p = the root;
while p is not a leaf and p < any of its children
swap p with the larger child;
Data Structures and Algorithms in Java, Third Edition
Ch06–31
Series of dequeuings
7
3
6
4
6
4
4 5 3
4 5 3
7 6 4 4 5 3
7 6 4 4 5 3
6
5
4
4 3
6 5 4 4 3
4
3 5 4 4
4
3
4
5
4
4 5
4 3
3 6 4 4 5
6 3 4 4 5
6 5 4 4 3
5
4
6
4 5
3
5
6
3
5
4
4
5 3 4 4
Data Structures and Algorithms in Java, Third Edition
4
4
3
5 4 4 3
Ch06–32
Series of dequeuings (cont’d)
5
4
4
3
4
3
4
4
4
4
3
4
3
4
3
5 4 4 3
5 3 4 3
3 4 4
4 3
4 3 4
3
4
3
4 3 4
3
4 3
3
Data Structures and Algorithms in Java, Third Edition
Ch06–33
Organizing arrays as heaps
FloydAlgorithm(data[])
for i = index of the last nonleaf, down to 0
p = data[i];
while p is not a leaf and p < any of its children
swap p with the larger child;
Data Structures and Algorithms in Java, Third Edition
Ch06–34
Organizing arrays as heaps: example
4
6
3
7 5 4
4 6 3 7 5 4
3
6
4
7 5 4
7 5 3
4 6 3 7 5 4
4 6 4 7 5 3
last nonleaf
4
7
7
4
6 5 3
4 7 4 6 5 3
4
7
4
6
4
6 5 3
4 5 3
7 4 4 6 5 3
7 6 4 4 5 3
Data Structures and Algorithms in Java, Third Edition
Ch06–35