You are on page 1of 37

CSD201 – Data Structures and Algorithms (In

C++)

Chapter6: Binary trees

http://www.fpt.edu.vn/ 1
Chapter topics
• Binary search trees
• Expression trees
• Heaps

http://www.fpt.edu.vn/ 2
Binary tree

root

nonterminal node
parent

child

leaf
(terminal node)

http://www.fpt.edu.vn/ 3
Binary search tree

el

< el ≥ el

http://www.fpt.edu.vn/ 4
Node implementation

public class BSTNode<T extends


Comparable<? super T>> {
protected T el;
protected BSTNode<T> left, right;
// constructors
}

el abbreviated as: el

http://www.fpt.edu.vn/ 5
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;

http://www.fpt.edu.vn/ 6
Insertion in the BST: example
20 30

null 20

25 10 15

20 20 20

30 30 10 30

25 25

http://www.fpt.edu.vn/ 7
Insertion in the BST: example (continued)

27 23 26

20 20 20

10 30 10 30 10 30

15 25 15 25 15 25

27 23 27

http://www.fpt.edu.vn/ 8
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;

http://www.fpt.edu.vn/ 9
Searching in the BST: example

25 28

20 20

10 30 10 30

15 25 success 15 25

23 27 23 27
failure

26 26

http://www.fpt.edu.vn/ 10
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);
}
}
}

http://www.fpt.edu.vn/ 11
Breadth-first traversal: example

iteration queue p
20
init 20
10 30
1 10 30 20

2 30 15 10
15 25
3 15 25 30
23 27 4 25 15
5 23 27 25
26 6 27 23
7 26 27
8 26

http://www.fpt.edu.vn/ 12
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:

left to right right to left


preorder VLR VRL
inorder LVR RVL
postorder LRV RLV

http://www.fpt.edu.vn/ 13
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);
}
}

http://www.fpt.edu.vn/ 14
Depth-first traversals: example

20

10 30
preorder 20 10 15 30 25 23 27 26
15 25
inorder 10 15 20 23 25 26 27 30

postorder 15 10 23 26 27 25 30 20
23 27

26

http://www.fpt.edu.vn/ 15
Deletion by copying
1. Deleting a leaf

2. Deleting a node with one descendant

http://www.fpt.edu.vn/ 16
Deletion by copying (continued)
3. Deleting a node with two descendants

B A

http://www.fpt.edu.vn/ 17
Deletion by copying: example

20 20 15

10 30 10 27 10 27

15 25 35 15 25 35 25 35

23 27 23 26 23 26

26

delete 30 delete 20

http://www.fpt.edu.vn/ 18
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;

http://www.fpt.edu.vn/ 19
Step-by-step right rotation
G G G

P P P
C C C
Z Z Z

X Y X Y X Y

1. grandparent G of child C 2. right subtree of C becomes


becomes G left subtree of C’s parent P;
G C’s parent by replacing P;
C
P
= P
C X
Z

X Y Y Z

http://www.fpt.edu.vn/ 20
Left rotation

G G

P C

C P
X Z

Y Z X Y

http://www.fpt.edu.vn/ 21
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.

http://www.fpt.edu.vn/ 22
Single rotation technique: example

8 8
5 9 6 9
3 6 5 7
2 4 7 3
2 4

6 5
5 8 3 6
3 7 9 2 4 8
2 4 7 9
http://www.fpt.edu.vn/ 23
Expression trees

5+6∙7 (5 + 6) ∙ 7
+ *
5 * + 7
6 7 5 6
preorder +5 * 6 7 preorder * + 5 67
inorder 5+ 6 * 7 inorder 5+ 6 * 7
postorder 56 7 * + postorder 56 + 7*

http://www.fpt.edu.vn/ 24
Processing an expression in postfix notation

5 6 7 * +

7
6 6 42
5 5 5 5 47

http://www.fpt.edu.vn/ 25
Heaps
• The value of each node is greater than or equal to the values
stored in each of its children,
• The tree is perfectly balanced and the leaves in in the last
level are all in the leftmost positions.

http://www.fpt.edu.vn/ 26
Heaps: counter examples

20
A binary tree that 10 15
violates the first heap 7 3 7 18
condition:
6 5

20
A binary tree that 10 15
violates the second heap
condition: 7 3 7 18
6 5

http://www.fpt.edu.vn/ 27
Heap and binary search tree

Heap Binary search tree

el el

≤ el ≤ el < el ≥ el

http://www.fpt.edu.vn/ 28
Heap and binary search tree
(continued)
parent left right
20 child child
10 15 0 1 2
7 3 7 10 1 3 4
2 5 6
6 5
3 7 8
…. …. ….
i 2∙i +1 2∙i +2

0 1 2 3 4 5 6 7 8
20 10 15 7 3 7 10 6 5
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;

http://www.fpt.edu.vn/ 30
Series of enqueuings
4 6 3 7

empty 4 4 6 6 6 6
6 4 4 3 4 3 7 3
7 4

4 4 6 6 4 6 4 3 6 4 3 7 6 7 3 4

5 4

7 7 7 7
6 3 6 3 6 3 6 4
4 4 5 4 5 4 4 5 3

7 6 3 4 7 6 3 4 5 7 6 3 4 5 4 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;

32
http://www.fpt.edu.vn/ 32
Series of dequeuings

7 3 6 6
6 4 6 4 6 4 3 4 5 4
4 5 3 4 5 3 4 5 4 5 4 3
7 6 4 4 5 3 7 6 4 4 5 3 3 6 4 4 5 6 3 4 4 5 6 5 4 4 3

3 5 5
5 4 5 4 3 4 4 4
4 3 4 4 3
6 5 4 4 3 3 5 4 4 5 3 4 4 5 4 4 3

http://www.fpt.edu.vn/ 33
Series of dequeuings (continued)

5 3 4
4 4 4 4 4 4 3 4 3 4
3 3
5 3 4 4 5 3 4 4 3 4 4 4 3 4 4 3 4

4 3
3 3

4 3 4 3 3

http://www.fpt.edu.vn/ 34
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 ;

http://www.fpt.edu.vn/ 35
Organizing arrays as heaps: example
4
6 3 3 6 4
7 5 4 7 5 4 7 5 3
4 6 3 7 5 4 4 6 3 7 5 4 4 6 4 7 5 3

last
nonleaf
4 7 7
7 4 4 4 6 4
6 5 3 6 5 3 4 5 3
4 7 4 6 5 3 7 4 4 6 5 3 7 6 4 4 5 3

http://www.fpt.edu.vn/ 36
Q&A

Thank you for your listening!

http://www.fpt.edu.vn/ 37

You might also like